schematicapi.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package api
  2. import (
  3. "log"
  4. "net/http"
  5. "strconv"
  6. "time"
  7. "github.com/go-chi/chi"
  8. "github.com/go-chi/render"
  9. "github.com/willie68/schematic-service-go/dao"
  10. "github.com/willie68/schematic-service-go/model"
  11. )
  12. //SchematicsRoutes getting all routes for the config endpoint
  13. func SchematicsRoutes() *chi.Mux {
  14. router := chi.NewRouter()
  15. router.Post("/", PostSchematicEndpoint)
  16. router.Get("/", GetSchematicsEndpoint)
  17. router.Get("/count", GetSchematicsCountEndpoint)
  18. router.Delete("/{schematicId}", DeleteSchematicEndpoint)
  19. router.Put("/{schematicId}", UpdateSchematicEndpoint)
  20. router.Get("/{schematicId}", GetSchematicHandler)
  21. return router
  22. }
  23. // GetSchematicHandler gets a tenant
  24. func GetSchematicHandler(response http.ResponseWriter, req *http.Request) {
  25. schematicID := chi.URLParam(req, "schematicId")
  26. schematic, err := dao.GetStorage().GetSchematic(schematicID)
  27. if err != nil {
  28. if err == dao.ErrNoDocument {
  29. Msg(response, http.StatusNotFound, err.Error())
  30. return
  31. }
  32. Msg(response, http.StatusBadRequest, err.Error())
  33. return
  34. }
  35. owner, _, _ := req.BasicAuth()
  36. if schematic.PrivateFile && schematic.Owner != owner {
  37. Msg(response, http.StatusForbidden, "you don't have the permission to see this file")
  38. return
  39. }
  40. render.JSON(response, req, schematic)
  41. }
  42. // GetSchematicsEndpoint gets all tenants
  43. func GetSchematicsEndpoint(response http.ResponseWriter, req *http.Request) {
  44. offset := 0
  45. limit := 10
  46. query := req.URL.Query().Get("query")
  47. offsetStr := req.URL.Query().Get("offset")
  48. limitStr := req.URL.Query().Get("limit")
  49. var err error
  50. if offsetStr != "" {
  51. offset, err = strconv.Atoi(offsetStr)
  52. if err != nil {
  53. Msg(response, http.StatusBadRequest, err.Error())
  54. return
  55. }
  56. }
  57. if limitStr != "" {
  58. limit, err = strconv.Atoi(limitStr)
  59. if err != nil {
  60. Msg(response, http.StatusBadRequest, err.Error())
  61. return
  62. }
  63. }
  64. log.Printf("query: %s, offset: %d, limit: %d\n", query, offset, limit)
  65. owner, _, _ := req.BasicAuth()
  66. n, schematics, err := dao.GetStorage().GetSchematics(query, offset, limit, owner)
  67. if err != nil {
  68. Msg(response, http.StatusBadRequest, err.Error())
  69. return
  70. }
  71. m := make(map[string]interface{})
  72. m["data"] = schematics
  73. m["found"] = n
  74. m["count"] = len(schematics)
  75. m["query"] = query
  76. m["offset"] = offset
  77. m["limit"] = limit
  78. render.JSON(response, req, m)
  79. }
  80. // GetSchematicsCountEndpoint gets all tenants
  81. func GetSchematicsCountEndpoint(response http.ResponseWriter, req *http.Request) {
  82. offset := 0
  83. limit := 10
  84. query := req.URL.Query().Get("query")
  85. offsetStr := req.URL.Query().Get("offset")
  86. limitStr := req.URL.Query().Get("limit")
  87. var err error
  88. if offsetStr != "" {
  89. offset, err = strconv.Atoi(offsetStr)
  90. if err != nil {
  91. Msg(response, http.StatusBadRequest, err.Error())
  92. return
  93. }
  94. }
  95. if limitStr != "" {
  96. limit, err = strconv.Atoi(limitStr)
  97. if err != nil {
  98. Msg(response, http.StatusBadRequest, err.Error())
  99. return
  100. }
  101. }
  102. log.Printf("query: %s, offset: %d, limit: %d\n", query, offset, limit)
  103. owner, _, _ := req.BasicAuth()
  104. n, err := dao.GetStorage().GetSchematicsCount(query, owner)
  105. if err != nil {
  106. Msg(response, http.StatusBadRequest, err.Error())
  107. return
  108. }
  109. m := make(map[string]interface{})
  110. m["found"] = n
  111. m["query"] = query
  112. m["offset"] = offset
  113. m["limit"] = limit
  114. render.JSON(response, req, m)
  115. }
  116. func PostSchematicEndpoint(response http.ResponseWriter, req *http.Request) {
  117. var schematic model.Schematic
  118. err := render.DefaultDecoder(req, &schematic)
  119. if err != nil {
  120. Msg(response, http.StatusBadRequest, err.Error())
  121. return
  122. }
  123. username, _, _ := req.BasicAuth()
  124. if username != "" {
  125. schematic.Owner = username
  126. }
  127. schematic.CreatedAt = time.Now()
  128. schematic.LastModifiedAt = time.Now()
  129. id, err := dao.GetStorage().CreateSchematic(schematic)
  130. if err != nil {
  131. Msg(response, http.StatusBadRequest, err.Error())
  132. return
  133. }
  134. schematic, err = dao.GetStorage().GetSchematic(id)
  135. if err != nil {
  136. Msg(response, http.StatusBadRequest, err.Error())
  137. return
  138. }
  139. render.Status(req, http.StatusCreated)
  140. render.JSON(response, req, schematic)
  141. }
  142. func DeleteSchematicEndpoint(response http.ResponseWriter, req *http.Request) {
  143. schematicID := chi.URLParam(req, "schematicId")
  144. schematic, err := dao.GetStorage().GetSchematic(schematicID)
  145. if err != nil {
  146. if err == dao.ErrNoDocument {
  147. Msg(response, http.StatusNotFound, err.Error())
  148. return
  149. }
  150. Msg(response, http.StatusBadRequest, err.Error())
  151. return
  152. }
  153. owner, _, _ := req.BasicAuth()
  154. if schematic.PrivateFile && schematic.Owner != owner {
  155. Msg(response, http.StatusForbidden, "you don't have the permission to see this file")
  156. return
  157. }
  158. dao.GetStorage().DeleteSchematic(schematic.ID.Hex())
  159. render.JSON(response, req, schematic)
  160. }
  161. func UpdateSchematicEndpoint(response http.ResponseWriter, req *http.Request) {
  162. var schematic model.Schematic
  163. err := render.DefaultDecoder(req, &schematic)
  164. if err != nil {
  165. Msg(response, http.StatusBadRequest, err.Error())
  166. return
  167. }
  168. username, _, _ := req.BasicAuth()
  169. if username != "" {
  170. schematic.Owner = username
  171. }
  172. schematic.CreatedAt = time.Now()
  173. schematic.LastModifiedAt = time.Now()
  174. id, err := dao.GetStorage().UpdateSchematic(schematic)
  175. if err != nil {
  176. Msg(response, http.StatusBadRequest, err.Error())
  177. return
  178. }
  179. schematic, err = dao.GetStorage().GetSchematic(id)
  180. if err != nil {
  181. Msg(response, http.StatusBadRequest, err.Error())
  182. return
  183. }
  184. render.Status(req, http.StatusCreated)
  185. render.JSON(response, req, schematic)
  186. }