From 8b5bd6d9cc2fd2f0f4ef345b320d710207c66050 Mon Sep 17 00:00:00 2001 From: weizhihong Date: Fri, 14 Jul 2023 16:47:59 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E3=80=90=E8=8D=89=E7=A8=BF=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=8F=A6=E5=AD=98=E4=B8=BA=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/drafting.go | 58 +++++++++++++++++++++++++++++++++++++++++++++ dto/drafting.go | 12 ++++++++++ main.go | 1 + middleware/jwt.go | 5 ++++ service/drafting.go | 58 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+) create mode 100644 api/drafting.go create mode 100644 dto/drafting.go create mode 100644 service/drafting.go diff --git a/api/drafting.go b/api/drafting.go new file mode 100644 index 0000000..5cf74e7 --- /dev/null +++ b/api/drafting.go @@ -0,0 +1,58 @@ +package api + +import ( + "net/http" + "strconv" + + jwt "github.com/appleboy/gin-jwt/v2" + "github.com/gin-gonic/gin" + "go.uber.org/zap" + "joylink.club/bj-rtsts-server/dto" + "joylink.club/bj-rtsts-server/middleware" + "joylink.club/bj-rtsts-server/service" +) + +func InitDraftingRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { + authed := api.Group("/v1/drafting").Use(authMiddleware.MiddlewareFunc()) + authed.GET("/paging", pageQueryUser) + authed.POST("", createDrafting) + authed.POST("/:id/saveAs", saveAsDrafting) +} + +func pageQueryDrafting(c *gin.Context) { + user, _ := c.Get(middleware.IdentityKey) + zap.S().Debug("分页查询草稿", user) + req := dto.PageDraftingReqDto{} + if err := c.ShouldBind(&req); err != nil { + zap.S().Warn("分页查询参数绑定错误,使用默认参数", err) + req.Default() + } + zap.S().Debug("分页查草稿参数", req) + page, _ := service.PageDraftingQuery(&req) + c.JSON(http.StatusOK, page) +} + +func createDrafting(c *gin.Context) { + createId := middleware.ParseContextUserId(c) + req := dto.DraftingDto{} + if err := c.ShouldBind(&req); err != nil { + panic("传入参数为空") + } + zap.S().Debug("保存数据", req) + service.CreateDrafting(createId, &req) +} + +func saveAsDrafting(c *gin.Context) { + id, exist := c.Params.Get("id") + if !exist { + panic("必要参数id不存在") + } + zap.S().Debug("传入参数id为" + id) + createId := middleware.ParseContextUserId(c) + req := dto.DraftingDto{} + if err := c.ShouldBind(&req); err != nil { + zap.S().Warn("保存数据出错", err) + } + int64Id, _ := strconv.ParseInt(id, 10, 64) + service.SaveAsDrafting(createId, int32(int64Id), &req) +} diff --git a/dto/drafting.go b/dto/drafting.go new file mode 100644 index 0000000..7a6a1ad --- /dev/null +++ b/dto/drafting.go @@ -0,0 +1,12 @@ +package dto + +type PageDraftingReqDto struct { + PageQueryDto + Name string `json:"name" form:"name"` +} + +type DraftingDto struct { + Id int `json:"id" form:"id"` + Name string `json:"name" form:"name"` + Proto []byte `json:"proto" from:"proto"` +} diff --git a/main.go b/main.go index 651c435..2e2c23a 100644 --- a/main.go +++ b/main.go @@ -57,6 +57,7 @@ func main() { router := engine.Group("/api") api.InitUserRouter(router, authMiddleware) + api.InitDraftingRouter(router, authMiddleware) serverConfig := config.Config.Server if serverConfig.Port == 0 { diff --git a/middleware/jwt.go b/middleware/jwt.go index 16affa7..4157f7f 100644 --- a/middleware/jwt.go +++ b/middleware/jwt.go @@ -79,3 +79,8 @@ func InitGinJwtMiddleware() (authMiddleware *jwt.GinJWTMiddleware) { return } + +func ParseContextUserId(c *gin.Context) int32 { + claims := jwt.ExtractClaims(c) + return int32(claims[IdentityKey].(float64)) +} diff --git a/service/drafting.go b/service/drafting.go new file mode 100644 index 0000000..5a11667 --- /dev/null +++ b/service/drafting.go @@ -0,0 +1,58 @@ +package service + +import ( + "fmt" + "time" + + "joylink.club/bj-rtsts-server/db/dbquery" + "joylink.club/bj-rtsts-server/db/model" + "joylink.club/bj-rtsts-server/dto" +) + +// 查询草稿列表 +func PageDraftingQuery(query *dto.PageDraftingReqDto) (*dto.PageDto, error) { + d := dbquery.Drafting + dq := d.Where() + if query.Name != "" { + dq = dq.Where(d.Name.Like(fmt.Sprintf("%%%s%%", query.Name))) + } + records, total, err := dq.Debug().FindByPage(query.Offset(), query.Size) + if err != nil { + panic(err) + } + return &dto.PageDto{Total: int(total), PageQueryDto: query.PageQueryDto, Records: records}, err +} + +// 创建草稿 +func CreateDrafting(createId int32, dto *dto.DraftingDto) error { + d := model.Drafting{ + Name: dto.Name, + Proto: dto.Proto, + CreatorID: createId, + CreatedAt: time.Now(), + UpdateAt: time.Now(), + } + return dbquery.Drafting.Save(&d) +} + +// 另存为 +func SaveAsDrafting(createId int32, oldId int32, dto *dto.DraftingDto) error { + findNameQuery := dbquery.Drafting + count, err := findNameQuery.Where(findNameQuery.Name.Eq(dto.Name)).Debug().Count() + if count > 0 || err != nil { + panic("名称已存在") + } + findOldQuery := dbquery.Drafting + oldD, err := findOldQuery.Where(findOldQuery.ID.Eq(oldId)).Debug().First() + if oldD == nil || err != nil { + panic(err) + } + newD := model.Drafting{ + Name: dto.Name, + Proto: oldD.Proto, + CreatorID: createId, + CreatedAt: time.Now(), + UpdateAt: time.Now(), + } + return dbquery.Drafting.Save(&newD) +} From be84943972ea03c89cba2daf08649b7733eaf2bb Mon Sep 17 00:00:00 2001 From: weizhihong Date: Fri, 14 Jul 2023 16:48:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=BC=95=E5=85=A5=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index d657561..8ee6691 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( gorm.io/driver/mysql v1.5.1 gorm.io/gen v0.3.23 gorm.io/gorm v1.25.2 + gorm.io/plugin/dbresolver v1.3.0 ) require ( @@ -23,7 +24,6 @@ require ( gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c // indirect gorm.io/hints v1.1.0 // indirect - gorm.io/plugin/dbresolver v1.3.0 // indirect ) require (