package api import ( "cmf/db/model" "cmf/db/repo" "cmf/log" "errors" "time" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" ) func FinishArtwork(r *GinRouter) { r.POST("/finish/create", CreateFinishArtwork) r.POST("/finish/delete/:id", DeleteFinishArtwork) r.GET("/finish/list", FinishArtworkList) r.GET("/finish/detail/:id", FinishArtworkDetail) r.POST("/finish/update", UpdateFinishArtwork) } func CreateFinishArtwork(c *gin.Context, apictx *ApiSession) (interface{}, error) { var finish model.FinishArtwork err := c.ShouldBindJSON(&finish) if err != nil { log.Error(err) return nil, err } finish.CreateTime = time.Now() finish.UpdateTime = time.Now() return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionFinishArtwork, &finish) } func DeleteFinishArtwork(c *gin.Context, apictx *ApiSession) (interface{}, error) { _id := c.Param("id") id, _ := primitive.ObjectIDFromHex(_id) if id.IsZero() { return nil, errors.New("id错误") } return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionFinishArtwork, _id) } func FinishArtworkList(c *gin.Context, apictx *ApiSession) (interface{}, error) { page, size, query := UtilQueryPageSize(c) return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{ CollectName: repo.CollectionFinishArtwork, Page: page, Size: size, Query: query, }) } func FinishArtworkDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) { _id := c.Param("id") id, _ := primitive.ObjectIDFromHex(_id) if id.IsZero() { return nil, errors.New("id错误") } cate := &model.FinishArtwork{} found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{ CollectName: repo.CollectionFinishArtwork, Query: repo.Map{"_id": id}, }, cate) if err != nil { log.Error(err) return nil, err } if !found { return nil, errors.New("未找到该数据") } return cate, nil } func UpdateFinishArtwork(c *gin.Context, apictx *ApiSession) (interface{}, error) { var cate model.FinishArtwork err := c.ShouldBindJSON(&cate) if err != nil { log.Error(err) return nil, err } if cate.Id.IsZero() { return nil, errors.New("id错误") } return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionFinishArtwork, cate.Id.Hex(), &cate) }