123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 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")
- }
|