Browse Source

添加我的案例和我的材料

infish2018 3 months ago
parent
commit
efd48b8700
5 changed files with 113 additions and 1 deletions
  1. 84 0
      src/api/case.go
  2. 1 0
      src/api/router.go
  3. 17 0
      src/db/model/MatLib.go
  4. 1 0
      src/db/repo/repo.go
  5. 10 1
      src/test/mycase.http

+ 84 - 0
src/api/case.go

@@ -22,6 +22,29 @@ func MyCaseRoute(r *GinRouter) {
 	r.POSTJWT("/mycase/update", UpdateMyCase)
 }
 
+func MyMatLib(r *GinRouter) {
+	r.POST("/mymat/create", CreateMyMat)
+	r.POST("/mymat/delete/:id", DeleteMyMat)
+	r.GET("/mymat/list", MatMyList)
+	r.GET("/mymat/detail/:id", MatMyDetail)
+	r.POST("/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.DisplayName
+	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)
@@ -38,6 +61,15 @@ func CreateMyCase(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	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)
@@ -47,6 +79,21 @@ func DeleteMyCase(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionMyCase, _id)
 }
 
+func MatMyList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	page, size, query := UtilQueryPageSize(c)
+	query["userId"] = apictx.User.Id
+
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
+		CollectName: repo.CollectionMyMat,
+		Page:        page,
+		Size:        size,
+		Query:       query,
+		Project:     []string{"_id", "name", "cover", "createTime", "updateTime", "userName"},
+		Sort:        bson.M{"createTime": -1},
+	})
+}
+
 func MyCaseList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 	page, size, query := UtilQueryPageSize(c)
@@ -70,6 +117,28 @@ func MyCaseList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	})
 }
 
+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)
@@ -92,6 +161,20 @@ func MyCaseDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	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)
@@ -102,5 +185,6 @@ func UpdateMyCase(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	if box.Id.IsZero() {
 		return nil, errors.New("id错误")
 	}
+	box.UpdateTime = time.Now()
 	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMyCase, box.Id.Hex(), &box)
 }

+ 1 - 0
src/api/router.go

@@ -45,6 +45,7 @@ func RegRouters(svc *Service) {
 	Shape(r)
 	Finish(r)
 	MyCaseRoute(r)
+	MyMatLib(r)
 
 	r.GET("/printr", Printr)
 }

+ 17 - 0
src/db/model/MatLib.go

@@ -40,6 +40,23 @@ type MatLib struct {
 	UpdateTime time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
 }
 
+type MyMatLib struct {
+	Id         primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
+	UserId     string             `bson:"userId,omitempty" json:"userId"`
+	UserName   string             `bson:"userName,omitempty" json:"userName"`
+	Finishes   []FinishArtwork    `bson:"finishes,omitempty" json:"finishes"`
+	ShapeIds   []string           `bson:"shapeIds,omitempty" json:"shapeIds"` //支持成型工艺
+	Name       string             `bson:"name,omitempty" json:"name"`
+	CusNum     string             `bson:"cusNum,omitempty" json:"cusNum"`
+	Cover      string             `bson:"cover,omitempty" json:"cover"`
+	Order      int32              `bson:"order,omitempty" json:"order"`
+	DotQueen   string             `bson:"dotqueen,omitempty" json:"dotqueen"`
+	Blend      string             `bson:"blend,omitempty" json:"blend"`
+	Categories []string           `bson:"categories,omitempty" json:"categories"`
+	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
+	UpdateTime time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
+}
+
 type Shape struct {
 	Id         primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
 	Name       string             `bson:"name,omitempty" json:"name"`

+ 1 - 0
src/db/repo/repo.go

@@ -27,6 +27,7 @@ const (
 	CollectionModelLib   = "models"
 	CollectionMyCase     = "mycases"
 	CollectionMat        = "mats"
+	CollectionMyMat      = "mymats"
 	CollectionShape      = "Shapes"
 	CollectionFinish     = "finishes"
 )

+ 10 - 1
src/test/mycase.http

@@ -16,6 +16,12 @@ Accept: application/json
     "name": "Example Resource"
 }
 
+### 创建
+GET {{host}}/cmf/mycase/detail/675bf452ee774f698700c69d
+Authorization: Bearer {{token}}
+Content-Type: application/json
+Accept: application/json
+
 ### 删除
 POST {{host}}/cmf/mycase/delete/675ba045a41ed7ac89d2070c
 Authorization: Bearer {{token}}
@@ -31,4 +37,7 @@ Accept: application/json
 {
     "_id": "675ba12e66f107cc28e7860f",
     "name": "new name"
-}
+}
+
+### code 换token
+GET {{host}}/cmf/getToken?code=03bd2449093ca29a6484