|
@@ -1,11 +1,17 @@
|
|
|
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
|
|
@@ -16,7 +22,8 @@ func SchematicsRoutes() *chi.Mux {
|
|
|
router.Delete("/{schematicId}", DeleteSchematicEndpoint)
|
|
|
router.Put("/{schematicId}", UpdateSchematicEndpoint)
|
|
|
router.Get("/{schematicId}", GetSchematicHandler)
|
|
|
- router.Get("/{schematicId}/files/{fileId}", GetSchematicFileHandler)
|
|
|
+ router.Get("/files/{fileId}", GetSchematicFileHandler)
|
|
|
+ router.Post("/files", PostFileEndpoint)
|
|
|
return router
|
|
|
}
|
|
|
|
|
@@ -45,18 +52,83 @@ func GetSchematicFileHandler(response http.ResponseWriter, req *http.Request) {
|
|
|
|
|
|
// GetTenantsHandler gets all tenants
|
|
|
func GetSchematicsEndpoint(response http.ResponseWriter, req *http.Request) {
|
|
|
- /*
|
|
|
- var tenants []dto.Tenant
|
|
|
- err := dao.Get().All("tenants", &tenants)
|
|
|
+ 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
|
|
|
}
|
|
|
- */
|
|
|
- render.JSON(response, req, "tenants")
|
|
|
+ }
|
|
|
+
|
|
|
+ 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) {
|