package api import ( "bufio" "fmt" "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.Delete("/{schematicId}", DeleteSchematicEndpoint) router.Put("/{schematicId}", UpdateSchematicEndpoint) router.Get("/{schematicId}", GetSchematicHandler) router.Get("/files/{fileId}", GetSchematicFileHandler) router.Post("/files", PostFileEndpoint) return router } // GetTenantHandler gets a tenant func GetSchematicHandler(response http.ResponseWriter, req *http.Request) { schematicID := chi.URLParam(req, "schematicId") schematic, err := dao.GetSchematic(schematicID) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } render.JSON(response, req, schematic) } // GetTenantHandler gets a tenant func GetSchematicFileHandler(response http.ResponseWriter, req *http.Request) { //schematicID := chi.URLParam(req, "schematicId") fileID := chi.URLParam(req, "fileId") err := dao.GetFile(fileID, response) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } //render.JSON(response, req, schematic) } // GetTenantsHandler 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) schematics, err := dao.GetSchematics(query, offset, limit) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } render.JSON(response, req, schematics) } 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 } schematic.CreatedAt = time.Now() schematic.LastModifiedAt = time.Now() id, err := dao.CreateSchematic(schematic) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } schematic, err = dao.GetSchematic(id) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } render.Status(req, http.StatusCreated) render.JSON(response, req, schematic) } func PostFileEndpoint(response http.ResponseWriter, req *http.Request) { req.ParseForm() f, fileHeader, err := req.FormFile("file") if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } //mimeType := fileHeader.Header.Get("Content-type") filename := fileHeader.Filename reader := bufio.NewReader(f) fileid, err := dao.AddFile(filename, reader) if err != nil { fmt.Printf("%v\n", err) } else { fmt.Printf("fileid: %s\n", fileid) } location := fmt.Sprintf("/api/v1/schematics/files/%s", fileid) response.Header().Add("Location", location) render.Status(req, http.StatusCreated) render.JSON(response, req, fileid) } func DeleteSchematicEndpoint(response http.ResponseWriter, req *http.Request) { /* schematicID := chi.URLParam(req, "schematicId") if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } */ render.JSON(response, req, "tenant") } func UpdateSchematicEndpoint(response http.ResponseWriter, req *http.Request) { /* schematicID := chi.URLParam(req, "schematicId") err := dao.Get().One("tenants", chi.URLParam(req, "schematicId"), &tenant) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } */ render.JSON(response, req, "tenant") }