case.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package api
  2. import (
  3. "cmf/db/model"
  4. "cmf/db/repo"
  5. "cmf/log"
  6. "errors"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "go.mongodb.org/mongo-driver/bson/primitive"
  11. )
  12. //cmf 个人场景案例
  13. func MyCaseRoute(r *GinRouter) {
  14. r.POSTJWT("/mycase/create", CreateMyCase)
  15. r.POSTJWT("/mycase/delete/:id", DeleteMyCase)
  16. r.GETJWT("/mycase/list", MyCaseList)
  17. r.GETJWT("/mycase/detail/:id", MyCaseDetail)
  18. r.POSTJWT("/mycase/update", UpdateMyCase)
  19. }
  20. func MyMatLib(r *GinRouter) {
  21. r.POSTJWT("/mymat/create", CreateMyMat)
  22. r.POSTJWT("/mymat/delete/:id", DeleteMyMat)
  23. r.GETJWT("/mymat/list", MatMyList)
  24. r.GETJWT("/mymat/detail/:id", MatMyDetail)
  25. r.POSTJWT("/mymat/update", UpdateMyMat)
  26. }
  27. func CreateMyMat(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  28. var data model.MyMatLib
  29. err := c.ShouldBindJSON(&data)
  30. if err != nil {
  31. log.Error(err)
  32. return nil, err
  33. }
  34. data.CreateTime = time.Now()
  35. data.UpdateTime = time.Now()
  36. data.UserId = apictx.User.Id
  37. data.UserName = apictx.User.Name
  38. return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionMyMat, &data)
  39. }
  40. func CreateMyCase(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  41. var obj model.MyCase
  42. err := c.ShouldBindJSON(&obj)
  43. if err != nil {
  44. log.Error(err)
  45. return nil, err
  46. }
  47. obj.CreateTime = time.Now()
  48. obj.UpdateTime = time.Now()
  49. obj.UserId = apictx.User.Id
  50. obj.UserName = apictx.User.Name
  51. return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionMyCase, &obj)
  52. }
  53. func DeleteMyMat(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  54. _id := c.Param("id")
  55. id, _ := primitive.ObjectIDFromHex(_id)
  56. if id.IsZero() {
  57. return nil, errors.New("id错误")
  58. }
  59. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionMyMat, _id)
  60. }
  61. func DeleteMyCase(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  62. _id := c.Param("id")
  63. id, _ := primitive.ObjectIDFromHex(_id)
  64. if id.IsZero() {
  65. return nil, errors.New("id错误")
  66. }
  67. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionMyCase, _id)
  68. }
  69. func MatMyList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  70. page, size, query := UtilQueryPageSize(c)
  71. if apictx.User != nil {
  72. query["userId"] = apictx.User.Id
  73. }
  74. catsConf, err := GetMatLibCategory(apictx)
  75. if err != nil {
  76. return nil, err
  77. }
  78. err = ParseCategories(query, apictx.CreateRepoCtx(), catsConf)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  83. CollectName: repo.CollectionMyMat,
  84. Page: page,
  85. Size: size,
  86. Query: query,
  87. Project: []string{"_id", "name", "cover", "categories", "createTime", "updateTime", "userName"},
  88. Sort: bson.M{"createTime": -1},
  89. })
  90. }
  91. func MyCaseList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  92. page, size, query := UtilQueryPageSize(c)
  93. catsConf, err := GetModelLibCategory(apictx)
  94. if err != nil {
  95. return nil, err
  96. }
  97. err = ParseCategories(query, apictx.CreateRepoCtx(), catsConf)
  98. if err != nil {
  99. return nil, err
  100. }
  101. if apictx.User != nil {
  102. query["userId"] = apictx.User.Id
  103. }
  104. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  105. CollectName: repo.CollectionMyCase,
  106. Page: page,
  107. Size: size,
  108. Query: query,
  109. Project: []string{"_id", "name", "cover", "categories", "createTime", "updateTime", "userName"},
  110. Sort: bson.M{"createTime": -1},
  111. })
  112. }
  113. func MatMyDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  114. _id := c.Param("id")
  115. id, _ := primitive.ObjectIDFromHex(_id)
  116. if id.IsZero() {
  117. return nil, errors.New("id错误")
  118. }
  119. box := &model.MyMatLib{}
  120. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  121. CollectName: repo.CollectionMyMat,
  122. Query: repo.Map{"_id": id},
  123. }, box)
  124. if err != nil {
  125. log.Error(err)
  126. return nil, err
  127. }
  128. if !found {
  129. return nil, errors.New("未找到该数据")
  130. }
  131. return box, nil
  132. }
  133. func MyCaseDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  134. _id := c.Param("id")
  135. id, _ := primitive.ObjectIDFromHex(_id)
  136. if id.IsZero() {
  137. return nil, errors.New("id错误")
  138. }
  139. box := &model.MyCase{}
  140. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  141. CollectName: repo.CollectionMyCase,
  142. Query: repo.Map{"_id": id},
  143. }, box)
  144. if err != nil {
  145. log.Error(err)
  146. return nil, err
  147. }
  148. if !found {
  149. return nil, errors.New("未找到该数据")
  150. }
  151. return box, nil
  152. }
  153. func UpdateMyMat(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  154. var box model.MyMatLib
  155. err := c.ShouldBindJSON(&box)
  156. if err != nil {
  157. log.Error(err)
  158. return nil, err
  159. }
  160. if box.Id.IsZero() {
  161. return nil, errors.New("id错误")
  162. }
  163. box.UpdateTime = time.Now()
  164. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMyMat, box.Id.Hex(), &box)
  165. }
  166. func UpdateMyCase(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  167. var box model.MyCase
  168. err := c.ShouldBindJSON(&box)
  169. if err != nil {
  170. log.Error(err)
  171. return nil, err
  172. }
  173. if box.Id.IsZero() {
  174. return nil, errors.New("id错误")
  175. }
  176. box.UpdateTime = time.Now()
  177. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMyCase, box.Id.Hex(), &box)
  178. }