finishArtwork.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/primitive"
  10. )
  11. func FinishArtwork(r *GinRouter) {
  12. r.POST("/finish/create", CreateFinishArtwork)
  13. r.POST("/finish/delete/:id", DeleteFinishArtwork)
  14. r.GET("/finish/list", FinishArtworkList)
  15. r.GET("/finish/detail/:id", FinishArtworkDetail)
  16. r.POST("/finish/update", UpdateFinishArtwork)
  17. }
  18. func CreateFinishArtwork(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  19. var finish model.FinishArtwork
  20. err := c.ShouldBindJSON(&finish)
  21. if err != nil {
  22. log.Error(err)
  23. return nil, err
  24. }
  25. finish.CreateTime = time.Now()
  26. finish.UpdateTime = time.Now()
  27. return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionFinishArtwork, &finish)
  28. }
  29. func DeleteFinishArtwork(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  30. _id := c.Param("id")
  31. id, _ := primitive.ObjectIDFromHex(_id)
  32. if id.IsZero() {
  33. return nil, errors.New("id错误")
  34. }
  35. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionFinishArtwork, _id)
  36. }
  37. func FinishArtworkList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  38. page, size, query := UtilQueryPageSize(c)
  39. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  40. CollectName: repo.CollectionFinishArtwork,
  41. Page: page,
  42. Size: size,
  43. Query: query,
  44. })
  45. }
  46. func FinishArtworkDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  47. _id := c.Param("id")
  48. id, _ := primitive.ObjectIDFromHex(_id)
  49. if id.IsZero() {
  50. return nil, errors.New("id错误")
  51. }
  52. cate := &model.FinishArtwork{}
  53. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  54. CollectName: repo.CollectionFinishArtwork,
  55. Query: repo.Map{"_id": id},
  56. }, cate)
  57. if err != nil {
  58. log.Error(err)
  59. return nil, err
  60. }
  61. if !found {
  62. return nil, errors.New("未找到该数据")
  63. }
  64. return cate, nil
  65. }
  66. func UpdateFinishArtwork(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  67. var cate model.FinishArtwork
  68. err := c.ShouldBindJSON(&cate)
  69. if err != nil {
  70. log.Error(err)
  71. return nil, err
  72. }
  73. if cate.Id.IsZero() {
  74. return nil, errors.New("id错误")
  75. }
  76. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionFinishArtwork, cate.Id.Hex(), &cate)
  77. }