|
@@ -1,111 +0,0 @@
|
|
|
-package api
|
|
|
-
|
|
|
-import (
|
|
|
- "encoding/json"
|
|
|
- "errors"
|
|
|
- "fmt"
|
|
|
- "os"
|
|
|
- "time"
|
|
|
-
|
|
|
- "moutai/db/model"
|
|
|
- "moutai/db/repo"
|
|
|
-
|
|
|
- "github.com/gin-gonic/gin"
|
|
|
- "go.mongodb.org/mongo-driver/bson"
|
|
|
- "go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
-)
|
|
|
-
|
|
|
-func CreateMaterial(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
-
|
|
|
- material := &model.Material{}
|
|
|
- err := c.ShouldBindJSON(&material)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- material.CreateTime = time.Now()
|
|
|
- material.UpdateTime = time.Now()
|
|
|
- return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionMaterials, &material)
|
|
|
-}
|
|
|
-
|
|
|
-func DeleteMaterial(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.CollectionMaterials, _id)
|
|
|
-}
|
|
|
-
|
|
|
-func MaterialList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
- page, size, query := UtilQueryPageSize(c)
|
|
|
-
|
|
|
- name := c.Query("name")
|
|
|
- if v, ok := query["name"]; ok {
|
|
|
- name = v.(string)
|
|
|
- if len(name) > 0 {
|
|
|
- delete(query, "name")
|
|
|
- query["name"] = bson.M{"$regex": name, "$options": "$i"}
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
- CollectName: repo.CollectionMaterials,
|
|
|
- Page: page,
|
|
|
- Size: size,
|
|
|
- Query: query,
|
|
|
- })
|
|
|
-
|
|
|
- return result, err
|
|
|
-}
|
|
|
-
|
|
|
-func UpdateMaterial(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
-
|
|
|
- material := &model.Material{}
|
|
|
- err := c.ShouldBindJSON(&material)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- if material.Id.IsZero() {
|
|
|
- return nil, errors.New("id错误")
|
|
|
- }
|
|
|
-
|
|
|
- material.UpdateTime = time.Now()
|
|
|
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMaterials, material.Id.Hex(), material)
|
|
|
-}
|
|
|
-
|
|
|
-func MaterialInsert(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
- // 读取json文件
|
|
|
- jsonData, err := os.ReadFile("api/datalist/1背景.json")
|
|
|
- if err != nil {
|
|
|
- return nil, fmt.Errorf("failed to read JSON file: %v", err)
|
|
|
- }
|
|
|
-
|
|
|
- var jsonMaterials []*model.Material
|
|
|
- if err := json.Unmarshal(jsonData, &jsonMaterials); err != nil {
|
|
|
- return nil, fmt.Errorf("failed to parse JSON: %v", err)
|
|
|
- }
|
|
|
- count := 0
|
|
|
- // 转换为model.Material结构并插入数据库
|
|
|
- for _, m := range jsonMaterials {
|
|
|
- material := model.Material{
|
|
|
- Id: primitive.NewObjectID(),
|
|
|
- Name: m.Name,
|
|
|
- Desc: m.Desc,
|
|
|
- Type: 1, // 背景类型
|
|
|
- Image: m.Image,
|
|
|
- CreateTime: m.CreateTime,
|
|
|
- UpdateTime: m.UpdateTime,
|
|
|
- }
|
|
|
- id, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionMaterials, &material)
|
|
|
- if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
- } else {
|
|
|
- fmt.Println(id)
|
|
|
- count++
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return nil, nil
|
|
|
-}
|