package api import ( "fmt" "net/http" "strconv" "github.com/go-chi/chi" "github.com/go-chi/render" "github.com/willie68/schematic-service-go/dao" ) //EffectTypesRoutes getting all routes for the config endpoint func EffectTypesRoutes() *chi.Mux { router := chi.NewRouter() router.Get("/", GetEffectTypesEndpoint) router.Post("/", PostEffectTypeEndpoint) router.Get("/{typeId}", GetEffectTypeEndpoint) router.Put("/{typeId}", PutEffectTypeEndpoint) router.Delete("/{typeId}", DeleteEffectTypeEndpoint) return router } //EffectsRoutes getting all routes for the config endpoint func EffectsRoutes() *chi.Mux { router := chi.NewRouter() router.Get("/", GetEffectsEndpoint) router.Get("/count", GetEffectsCountEndpoint) router.Post("/", PostEffectEndpoint) router.Get("/{effectId}", GetEffectEndpoint) router.Put("/{effectId}", PutEffectEndpoint) router.Delete("/{effectId}", DeleteEffectEndpoint) return router } //GetEffectTypesEndpoint getting a list of effect entries func GetEffectTypesEndpoint(response http.ResponseWriter, req *http.Request) { effectTypes, err := dao.GetStorage().GetEffectTypes() if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } m := make(map[string]interface{}) m["found"] = len(effectTypes) m["data"] = effectTypes render.JSON(response, req, m) } //PostEffectTypeEndpoint creating an effect entry func PostEffectTypeEndpoint(response http.ResponseWriter, req *http.Request) { render.Status(req, http.StatusNotImplemented) render.JSON(response, req, "POST: not im plemented yet") } //GetEffectTypeEndpoint getting one effect entry func GetEffectTypeEndpoint(response http.ResponseWriter, req *http.Request) { typeID := chi.URLParam(req, "typeId") fmt.Printf("typeid: %s\n", typeID) render.Status(req, http.StatusNotImplemented) render.JSON(response, req, "GET: not im plemented yet") } //PutEffectTypeEndpoint changes an effect entry func PutEffectTypeEndpoint(response http.ResponseWriter, req *http.Request) { typeID := chi.URLParam(req, "typeId") fmt.Printf("typeid: %s\n", typeID) render.Status(req, http.StatusNotImplemented) render.JSON(response, req, "PUT: not im plemented yet") } //DeleteEffectTypeEndpoint delete an effect entry func DeleteEffectTypeEndpoint(response http.ResponseWriter, req *http.Request) { typeID := chi.URLParam(req, "typeId") fmt.Printf("typeid: %s\n", typeID) render.Status(req, http.StatusNotImplemented) render.JSON(response, req, "DELETE: not im plemented yet") } //GetEffectsCountEndpoint getting a list of effect entries func GetEffectsCountEndpoint(response http.ResponseWriter, req *http.Request) { query := req.URL.Query().Get("query") n, err := dao.GetStorage().GetEffectsCount(query) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } m := make(map[string]interface{}) m["found"] = n render.JSON(response, req, m) } //GetEffectsEndpoint getting a list of effect entries func GetEffectsEndpoint(response http.ResponseWriter, req *http.Request) { offset := 0 limit := 100 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 } } n, effects, err := dao.GetStorage().GetEffects(query, offset, limit) if err != nil { Msg(response, http.StatusBadRequest, err.Error()) return } m := make(map[string]interface{}) m["found"] = len(effects) m["count"] = n m["data"] = effects render.JSON(response, req, m) } //PostEffectEndpoint creating an effect entry func PostEffectEndpoint(response http.ResponseWriter, req *http.Request) { render.Status(req, http.StatusNotImplemented) render.JSON(response, req, "POST: not im plemented yet") } //GetEffectEndpoint getting one effect entry func GetEffectEndpoint(response http.ResponseWriter, req *http.Request) { effectID := chi.URLParam(req, "effectId") effect, err := dao.GetStorage().GetEffect(effectID) if err != nil { if err == dao.ErrNoDocument { Msg(response, http.StatusNotFound, err.Error()) return } Msg(response, http.StatusBadRequest, err.Error()) return } render.JSON(response, req, effect) } //PutEffectEndpoint changes an effect entry func PutEffectEndpoint(response http.ResponseWriter, req *http.Request) { typeID := chi.URLParam(req, "effectId") fmt.Printf("effectId: %s\n", typeID) render.Status(req, http.StatusNotImplemented) render.JSON(response, req, "PUT: not im plemented yet") } //DeleteEffectEndpoint delete an effect entry func DeleteEffectEndpoint(response http.ResponseWriter, req *http.Request) { typeID := chi.URLParam(req, "effectId") fmt.Printf("effectId: %s\n", typeID) render.Status(req, http.StatusNotImplemented) render.JSON(response, req, "DELETE: not im plemented yet") }