123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- 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)
- }
|