package api import ( "log" "net/http" "strconv" "time" "github.com/go-chi/chi" "github.com/go-chi/render" "github.com/willie68/schematic-service-go/dao" "github.com/willie68/schematic-service-go/model" ) //SchematicsRoutes getting all routes for the config endpoint func SchematicsRoutes() *chi.Mux { router := chi.NewRouter() router.Post("/", PostSchematicEndpoint) router.Get("/", GetSchematicsEndpoint) router.Get("/count", GetSchematicsCountEndpoint) router.Delete("/{schematicId}", DeleteSchematicEndpoint) router.Put("/{schematicId}", UpdateSchematicEndpoint) router.Get("/{schematicId}", GetSchematicHandler) return router } // GetSchematicHandler gets a tenant func GetSchematicHandler(response http.ResponseWriter, req *http.Request) { schematicID := chi.URLParam(req, "schematicId") schematic, err := dao.GetStorage().GetSchematic(schematicID) if err != nil { if err == dao.ErrNoDocument { Msg(response, http.StatusNotFound, err.Error()) return } Msg(response, http.StatusBadRequest, err.Error()) return } owner, _, _ := req.BasicAuth() if schematic.PrivateFile && schematic.Owner != owner { Msg(response, http.StatusForbidden, "you don't have the permission to see this file") return } render.JSON(response, req, schematic) } // GetSchematicsEndpoint gets all tenants func GetSchematicsEndpoint(response http.ResponseWriter, req *http.Request) { offset := 0 limit := 10 query := req.URL.Query().Get("query") offsetStr := req.URL.Query().Get("offset") limitStr := req.URL.Query().Get("limit") var err error if offsetStr != "" { offset, err = strconv.Atoi(offsetStr) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } } if limitStr != "" { limit, err = strconv.Atoi(limitStr) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } } log.Printf("query: %s, offset: %d, limit: %d\n", query, offset, limit) owner, _, _ := req.BasicAuth() n, schematics, err := dao.GetStorage().GetSchematics(query, offset, limit, owner) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } m := make(map[string]interface{}) m["data"] = schematics m["found"] = n m["count"] = len(schematics) m["query"] = query m["offset"] = offset m["limit"] = limit render.JSON(response, req, m) } // GetSchematicsCountEndpoint gets all tenants func GetSchematicsCountEndpoint(response http.ResponseWriter, req *http.Request) { offset := 0 limit := 10 query := req.URL.Query().Get("query") offsetStr := req.URL.Query().Get("offset") limitStr := req.URL.Query().Get("limit") var err error if offsetStr != "" { offset, err = strconv.Atoi(offsetStr) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } } if limitStr != "" { limit, err = strconv.Atoi(limitStr) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } } log.Printf("query: %s, offset: %d, limit: %d\n", query, offset, limit) owner, _, _ := req.BasicAuth() n, err := dao.GetStorage().GetSchematicsCount(query, owner) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } m := make(map[string]interface{}) m["found"] = n m["query"] = query m["offset"] = offset m["limit"] = limit render.JSON(response, req, m) } func PostSchematicEndpoint(response http.ResponseWriter, req *http.Request) { var schematic model.Schematic err := render.DefaultDecoder(req, &schematic) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } username, _, _ := req.BasicAuth() if username != "" { schematic.Owner = username } schematic.CreatedAt = time.Now() schematic.LastModifiedAt = time.Now() id, err := dao.GetStorage().CreateSchematic(schematic) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } schematic, err = dao.GetStorage().GetSchematic(id) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } render.Status(req, http.StatusCreated) render.JSON(response, req, schematic) } func DeleteSchematicEndpoint(response http.ResponseWriter, req *http.Request) { schematicID := chi.URLParam(req, "schematicId") schematic, err := dao.GetStorage().GetSchematic(schematicID) if err != nil { if err == dao.ErrNoDocument { Msg(response, http.StatusNotFound, err.Error()) return } Msg(response, http.StatusBadRequest, err.Error()) return } owner, _, _ := req.BasicAuth() if schematic.PrivateFile && schematic.Owner != owner { Msg(response, http.StatusForbidden, "you don't have the permission to see this file") return } dao.GetStorage().DeleteSchematic(schematic.ID.Hex()) render.JSON(response, req, schematic) } func UpdateSchematicEndpoint(response http.ResponseWriter, req *http.Request) { var schematic model.Schematic err := render.DefaultDecoder(req, &schematic) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } username, _, _ := req.BasicAuth() if username != "" { schematic.Owner = username } schematic.CreatedAt = time.Now() schematic.LastModifiedAt = time.Now() id, err := dao.GetStorage().UpdateSchematic(schematic) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } schematic, err = dao.GetStorage().GetSchematic(id) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } render.Status(req, http.StatusCreated) render.JSON(response, req, schematic) }