【草稿信息另存为】
This commit is contained in:
parent
bf13aa7dab
commit
8b5bd6d9cc
58
api/drafting.go
Normal file
58
api/drafting.go
Normal file
@ -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)
|
||||
}
|
12
dto/drafting.go
Normal file
12
dto/drafting.go
Normal file
@ -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"`
|
||||
}
|
1
main.go
1
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 {
|
||||
|
@ -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))
|
||||
}
|
||||
|
58
service/drafting.go
Normal file
58
service/drafting.go
Normal file
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user