123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 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 Shape(r *GinRouter) {
- r.POST("/shape/create", CreateShape)
- r.POST("/shape/delete/:id", DeleteShape)
- r.GET("/shape/list", ShapeList)
- r.GET("/shape/detail/:id", ShapeDetail)
- r.POST("/shape/update", UpdateShape)
- }
- func CreateShape(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var shape model.Shape
- err := c.ShouldBindJSON(&shape)
- if err != nil {
- log.Error(err)
- return nil, err
- }
- shape.CreateTime = time.Now()
- shape.UpdateTime = time.Now()
- return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionShape, &shape)
- }
- func DeleteShape(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.CollectionShape, _id)
- }
- func ShapeList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- page, size, query := UtilQueryPageSize(c)
- return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
- CollectName: repo.CollectionShape,
- Page: page,
- Size: size,
- Query: query,
- })
- }
- func ShapeDetail(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.Shape{}
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionShape,
- 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 UpdateShape(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var cate model.Shape
- 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.CollectionShape, cate.Id.Hex(), &cate)
- }
|