|
@@ -0,0 +1,106 @@
|
|
|
+package api
|
|
|
+
|
|
|
+import (
|
|
|
+ "cmf/db/model"
|
|
|
+ "cmf/db/repo"
|
|
|
+ "cmf/log"
|
|
|
+ "errors"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "go.mongodb.org/mongo-driver/bson"
|
|
|
+ "go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
+)
|
|
|
+
|
|
|
+//cmf 个人场景案例
|
|
|
+
|
|
|
+func MyCaseRoute(r *GinRouter) {
|
|
|
+ r.POSTJWT("/mycase/create", CreateMyCase)
|
|
|
+ r.POSTJWT("/mycase/delete/:id", DeleteMyCase)
|
|
|
+ r.GETJWT("/mycase/list", MyCaseList)
|
|
|
+ r.GETJWT("/mycase/detail/:id", MyCaseDetail)
|
|
|
+ r.POSTJWT("/mycase/update", UpdateMyCase)
|
|
|
+}
|
|
|
+
|
|
|
+func CreateMyCase(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ var obj model.MyCase
|
|
|
+ err := c.ShouldBindJSON(&obj)
|
|
|
+ if err != nil {
|
|
|
+ log.Error(err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ obj.CreateTime = time.Now()
|
|
|
+ obj.UpdateTime = time.Now()
|
|
|
+ obj.UserId = apictx.User.Id
|
|
|
+ obj.UserName = apictx.User.DisplayName
|
|
|
+
|
|
|
+ return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionMyCase, &obj)
|
|
|
+}
|
|
|
+
|
|
|
+func DeleteMyCase(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.CollectionMyCase, _id)
|
|
|
+}
|
|
|
+
|
|
|
+func MyCaseList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+
|
|
|
+ page, size, query := UtilQueryPageSize(c)
|
|
|
+ // catsConf, err := GetModelLibCategory(apictx)
|
|
|
+ // if err != nil {
|
|
|
+ // return nil, err
|
|
|
+ // }
|
|
|
+ // err = ParseCategories(query, apictx.CreateRepoCtx(), catsConf)
|
|
|
+ // if err != nil {
|
|
|
+ // return nil, err
|
|
|
+ // }
|
|
|
+ query["userId"] = apictx.User.Id
|
|
|
+
|
|
|
+ return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
+ CollectName: repo.CollectionMyCase,
|
|
|
+ Page: page,
|
|
|
+ Size: size,
|
|
|
+ Query: query,
|
|
|
+ Project: []string{"_id", "name", "cover", "createTime", "updateTime", "userName"},
|
|
|
+ Sort: bson.M{"createTime": -1},
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func MyCaseDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ _id := c.Param("id")
|
|
|
+ id, _ := primitive.ObjectIDFromHex(_id)
|
|
|
+ if id.IsZero() {
|
|
|
+ return nil, errors.New("id错误")
|
|
|
+ }
|
|
|
+ box := &model.MyCase{}
|
|
|
+ found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ CollectName: repo.CollectionMyCase,
|
|
|
+ Query: repo.Map{"_id": id},
|
|
|
+ }, box)
|
|
|
+ if err != nil {
|
|
|
+ log.Error(err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if !found {
|
|
|
+ return nil, errors.New("未找到该数据")
|
|
|
+ }
|
|
|
+ return box, nil
|
|
|
+}
|
|
|
+
|
|
|
+func UpdateMyCase(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ var box model.MyCase
|
|
|
+ err := c.ShouldBindJSON(&box)
|
|
|
+ if err != nil {
|
|
|
+ log.Error(err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ if box.Id.IsZero() {
|
|
|
+ return nil, errors.New("id错误")
|
|
|
+ }
|
|
|
+ return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMyCase, box.Id.Hex(), &box)
|
|
|
+}
|