effectsapi.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package api
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "github.com/go-chi/chi"
  7. "github.com/go-chi/render"
  8. "github.com/willie68/schematic-service-go/dao"
  9. )
  10. //EffectTypesRoutes getting all routes for the config endpoint
  11. func EffectTypesRoutes() *chi.Mux {
  12. router := chi.NewRouter()
  13. router.Get("/", GetEffectTypesEndpoint)
  14. router.Post("/", PostEffectTypeEndpoint)
  15. router.Get("/{typeId}", GetEffectTypeEndpoint)
  16. router.Put("/{typeId}", PutEffectTypeEndpoint)
  17. router.Delete("/{typeId}", DeleteEffectTypeEndpoint)
  18. return router
  19. }
  20. //EffectsRoutes getting all routes for the config endpoint
  21. func EffectsRoutes() *chi.Mux {
  22. router := chi.NewRouter()
  23. router.Get("/", GetEffectsEndpoint)
  24. router.Get("/count", GetEffectsCountEndpoint)
  25. router.Post("/", PostEffectEndpoint)
  26. router.Get("/{effectId}", GetEffectEndpoint)
  27. router.Put("/{effectId}", PutEffectEndpoint)
  28. router.Delete("/{effectId}", DeleteEffectEndpoint)
  29. return router
  30. }
  31. //GetEffectTypesEndpoint getting a list of effect entries
  32. func GetEffectTypesEndpoint(response http.ResponseWriter, req *http.Request) {
  33. effectTypes, err := dao.GetStorage().GetEffectTypes()
  34. if err != nil {
  35. Msg(response, http.StatusBadRequest, err.Error())
  36. return
  37. }
  38. m := make(map[string]interface{})
  39. m["found"] = len(effectTypes)
  40. m["data"] = effectTypes
  41. render.JSON(response, req, m)
  42. }
  43. //PostEffectTypeEndpoint creating an effect entry
  44. func PostEffectTypeEndpoint(response http.ResponseWriter, req *http.Request) {
  45. render.Status(req, http.StatusNotImplemented)
  46. render.JSON(response, req, "POST: not im plemented yet")
  47. }
  48. //GetEffectTypeEndpoint getting one effect entry
  49. func GetEffectTypeEndpoint(response http.ResponseWriter, req *http.Request) {
  50. typeID := chi.URLParam(req, "typeId")
  51. fmt.Printf("typeid: %s\n", typeID)
  52. render.Status(req, http.StatusNotImplemented)
  53. render.JSON(response, req, "GET: not im plemented yet")
  54. }
  55. //PutEffectTypeEndpoint changes an effect entry
  56. func PutEffectTypeEndpoint(response http.ResponseWriter, req *http.Request) {
  57. typeID := chi.URLParam(req, "typeId")
  58. fmt.Printf("typeid: %s\n", typeID)
  59. render.Status(req, http.StatusNotImplemented)
  60. render.JSON(response, req, "PUT: not im plemented yet")
  61. }
  62. //DeleteEffectTypeEndpoint delete an effect entry
  63. func DeleteEffectTypeEndpoint(response http.ResponseWriter, req *http.Request) {
  64. typeID := chi.URLParam(req, "typeId")
  65. fmt.Printf("typeid: %s\n", typeID)
  66. render.Status(req, http.StatusNotImplemented)
  67. render.JSON(response, req, "DELETE: not im plemented yet")
  68. }
  69. //GetEffectsCountEndpoint getting a list of effect entries
  70. func GetEffectsCountEndpoint(response http.ResponseWriter, req *http.Request) {
  71. query := req.URL.Query().Get("query")
  72. n, err := dao.GetStorage().GetEffectsCount(query)
  73. if err != nil {
  74. Msg(response, http.StatusBadRequest, err.Error())
  75. return
  76. }
  77. m := make(map[string]interface{})
  78. m["found"] = n
  79. render.JSON(response, req, m)
  80. }
  81. //GetEffectsEndpoint getting a list of effect entries
  82. func GetEffectsEndpoint(response http.ResponseWriter, req *http.Request) {
  83. offset := 0
  84. limit := 100
  85. query := req.URL.Query().Get("query")
  86. offsetStr := req.URL.Query().Get("offset")
  87. limitStr := req.URL.Query().Get("limit")
  88. var err error
  89. if offsetStr != "" {
  90. offset, err = strconv.Atoi(offsetStr)
  91. if err != nil {
  92. Msg(response, http.StatusBadRequest, err.Error())
  93. return
  94. }
  95. }
  96. if limitStr != "" {
  97. limit, err = strconv.Atoi(limitStr)
  98. if err != nil {
  99. Msg(response, http.StatusBadRequest, err.Error())
  100. return
  101. }
  102. }
  103. n, effects, err := dao.GetStorage().GetEffects(query, offset, limit)
  104. if err != nil {
  105. Msg(response, http.StatusBadRequest, err.Error())
  106. return
  107. }
  108. m := make(map[string]interface{})
  109. m["found"] = len(effects)
  110. m["count"] = n
  111. m["data"] = effects
  112. render.JSON(response, req, m)
  113. }
  114. //PostEffectEndpoint creating an effect entry
  115. func PostEffectEndpoint(response http.ResponseWriter, req *http.Request) {
  116. render.Status(req, http.StatusNotImplemented)
  117. render.JSON(response, req, "POST: not im plemented yet")
  118. }
  119. //GetEffectEndpoint getting one effect entry
  120. func GetEffectEndpoint(response http.ResponseWriter, req *http.Request) {
  121. effectID := chi.URLParam(req, "effectId")
  122. effect, err := dao.GetStorage().GetEffect(effectID)
  123. if err != nil {
  124. if err == dao.ErrNoDocument {
  125. Msg(response, http.StatusNotFound, err.Error())
  126. return
  127. }
  128. Msg(response, http.StatusBadRequest, err.Error())
  129. return
  130. }
  131. render.JSON(response, req, effect)
  132. }
  133. //PutEffectEndpoint changes an effect entry
  134. func PutEffectEndpoint(response http.ResponseWriter, req *http.Request) {
  135. typeID := chi.URLParam(req, "effectId")
  136. fmt.Printf("effectId: %s\n", typeID)
  137. render.Status(req, http.StatusNotImplemented)
  138. render.JSON(response, req, "PUT: not im plemented yet")
  139. }
  140. //DeleteEffectEndpoint delete an effect entry
  141. func DeleteEffectEndpoint(response http.ResponseWriter, req *http.Request) {
  142. typeID := chi.URLParam(req, "effectId")
  143. fmt.Printf("effectId: %s\n", typeID)
  144. render.Status(req, http.StatusNotImplemented)
  145. render.JSON(response, req, "DELETE: not im plemented yet")
  146. }