123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- 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 MyMatLib(r *GinRouter) {
- r.POSTJWT("/mymat/create", CreateMyMat)
- r.POSTJWT("/mymat/delete/:id", DeleteMyMat)
- r.GETJWT("/mymat/list", MatMyList)
- r.GETJWT("/mymat/detail/:id", MatMyDetail)
- r.POSTJWT("/mymat/update", UpdateMyMat)
- }
- func CreateMyMat(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var data model.MyMatLib
- err := c.ShouldBindJSON(&data)
- if err != nil {
- log.Error(err)
- return nil, err
- }
- data.CreateTime = time.Now()
- data.UpdateTime = time.Now()
- data.UserId = apictx.User.Id
- data.UserName = apictx.User.Name
- return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionMyMat, &data)
- }
- 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.Name
- return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionMyCase, &obj)
- }
- func DeleteMyMat(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.CollectionMyMat, _id)
- }
- 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 MatMyList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- page, size, query := UtilQueryPageSize(c)
- if apictx.User != nil {
- query["userId"] = apictx.User.Id
- }
- catsConf, err := GetMatLibCategory(apictx)
- if err != nil {
- return nil, err
- }
- err = ParseCategories(query, apictx.CreateRepoCtx(), catsConf)
- if err != nil {
- return nil, err
- }
- return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
- CollectName: repo.CollectionMyMat,
- Page: page,
- Size: size,
- Query: query,
- Project: []string{"_id", "name", "cover", "categories", "createTime", "updateTime", "userName"},
- Sort: bson.M{"createTime": -1},
- })
- }
- 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
- }
- if apictx.User != nil {
- 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", "categories", "createTime", "updateTime", "userName"},
- Sort: bson.M{"createTime": -1},
- })
- }
- func MatMyDetail(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.MyMatLib{}
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionMyMat,
- 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 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 UpdateMyMat(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var box model.MyMatLib
- err := c.ShouldBindJSON(&box)
- if err != nil {
- log.Error(err)
- return nil, err
- }
- if box.Id.IsZero() {
- return nil, errors.New("id错误")
- }
- box.UpdateTime = time.Now()
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMyMat, box.Id.Hex(), &box)
- }
- 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错误")
- }
- box.UpdateTime = time.Now()
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMyCase, box.Id.Hex(), &box)
- }
|