2023-07-14 16:47:59 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
jwt "github.com/appleboy/gin-jwt/v2"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"go.uber.org/zap"
|
2023-07-27 16:59:55 +08:00
|
|
|
"joylink.club/bj-rtsts-server/db/model"
|
2023-07-14 16:47:59 +08:00
|
|
|
"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())
|
2023-07-18 17:19:03 +08:00
|
|
|
authed.GET("/paging", pageQueryDrafting)
|
2023-07-14 16:47:59 +08:00
|
|
|
authed.POST("", createDrafting)
|
|
|
|
authed.POST("/:id/saveAs", saveAsDrafting)
|
2023-07-14 18:09:07 +08:00
|
|
|
authed.GET("/:id", queryDraftingInfo)
|
|
|
|
authed.PUT("/:id", updateDraftingInfo)
|
2023-07-25 17:18:37 +08:00
|
|
|
authed.DELETE("/:id", deleteDrafting)
|
2023-07-14 16:47:59 +08:00
|
|
|
}
|
|
|
|
|
2023-07-14 18:09:07 +08:00
|
|
|
// 分页查询草稿
|
|
|
|
//
|
|
|
|
// @Summary 分页查询草稿
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 可以通过草稿名称过滤,分页查询草稿
|
|
|
|
// @Tags 草稿Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2023-07-25 17:18:37 +08:00
|
|
|
// @Param PageDraftingReqDto query dto.PageDraftingReqDto true "草稿查询条件带分页信息"
|
2023-07-14 18:09:07 +08:00
|
|
|
// @Success 200 {object} dto.PageDto
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/drafting/paging [get]
|
2023-07-14 16:47:59 +08:00
|
|
|
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)
|
2023-07-28 14:13:03 +08:00
|
|
|
page, err := service.PageDraftingQuery(&req)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-07-14 16:47:59 +08:00
|
|
|
c.JSON(http.StatusOK, page)
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:09:07 +08:00
|
|
|
// 创建草稿
|
|
|
|
//
|
|
|
|
// @Summary 创建草稿
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 创建草稿数据
|
|
|
|
// @Tags 草稿Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2023-07-25 17:18:37 +08:00
|
|
|
// @Param draftingDto query dto.DraftingDto true "创建的草稿信息"
|
2023-07-18 17:19:03 +08:00
|
|
|
// @Success 200 {object} nil
|
2023-07-14 18:09:07 +08:00
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/drafting [post]
|
2023-07-14 16:47:59 +08:00
|
|
|
func createDrafting(c *gin.Context) {
|
2023-07-27 16:59:55 +08:00
|
|
|
user, _ := c.Get(middleware.IdentityKey)
|
2023-07-28 14:13:03 +08:00
|
|
|
createId := user.(*model.User).ID
|
2023-07-14 16:47:59 +08:00
|
|
|
req := dto.DraftingDto{}
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2023-07-28 14:13:03 +08:00
|
|
|
c.JSON(http.StatusBadRequest, "传入参数错误")
|
|
|
|
return
|
2023-07-14 16:47:59 +08:00
|
|
|
}
|
|
|
|
zap.S().Debug("保存数据", req)
|
2023-07-28 14:13:03 +08:00
|
|
|
data, err := service.CreateDrafting(createId, &req)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, data)
|
2023-07-14 16:47:59 +08:00
|
|
|
}
|
|
|
|
|
2023-07-14 18:09:07 +08:00
|
|
|
// 草稿另存为
|
|
|
|
//
|
|
|
|
// @Summary 草稿另存为
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 草稿另存为
|
|
|
|
// @Tags 草稿Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2023-07-25 17:18:37 +08:00
|
|
|
// @Param id path int true "源草稿id"
|
|
|
|
// @Param draftingDto query dto.DraftingDto true "另存为草稿信息"
|
2023-07-18 17:19:03 +08:00
|
|
|
// @Success 200 {object} nil
|
2023-07-14 18:09:07 +08:00
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/drafting/:id/saveAs [post]
|
2023-07-14 16:47:59 +08:00
|
|
|
func saveAsDrafting(c *gin.Context) {
|
|
|
|
id, exist := c.Params.Get("id")
|
|
|
|
if !exist {
|
2023-07-28 14:13:03 +08:00
|
|
|
c.JSON(http.StatusBadRequest, "必要参数id不存在")
|
|
|
|
return
|
2023-07-14 16:47:59 +08:00
|
|
|
}
|
|
|
|
zap.S().Debug("传入参数id为" + id)
|
2023-07-27 16:59:55 +08:00
|
|
|
user, _ := c.Get(middleware.IdentityKey)
|
2023-07-28 14:13:03 +08:00
|
|
|
createrId := user.(*model.User).ID
|
2023-07-14 16:47:59 +08:00
|
|
|
req := dto.DraftingDto{}
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2023-07-28 14:13:03 +08:00
|
|
|
c.JSON(http.StatusBadRequest, "传入参数错误")
|
|
|
|
return
|
2023-07-14 16:47:59 +08:00
|
|
|
}
|
|
|
|
int64Id, _ := strconv.ParseInt(id, 10, 64)
|
2023-07-28 14:13:03 +08:00
|
|
|
newData, err := service.SaveAsDrafting(createrId, int32(int64Id), &req)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, "保存数据出错")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, newData)
|
2023-07-14 18:09:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 查询草稿详情
|
|
|
|
//
|
|
|
|
// @Summary 查询草稿详情
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 查询草稿详情
|
|
|
|
// @Tags 草稿Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2023-07-25 17:18:37 +08:00
|
|
|
// @Param id path int true "草稿ID"
|
2023-07-14 18:09:07 +08:00
|
|
|
// @Success 200 {object} model.Drafting
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/drafting/:id [get]
|
|
|
|
func queryDraftingInfo(c *gin.Context) {
|
|
|
|
id, exist := c.Params.Get("id")
|
|
|
|
if !exist {
|
2023-07-28 14:13:03 +08:00
|
|
|
c.JSON(http.StatusBadRequest, "必要参数id不存在")
|
|
|
|
return
|
2023-07-14 18:09:07 +08:00
|
|
|
}
|
|
|
|
zap.S().Debug("传入参数id为" + id)
|
|
|
|
int64Id, _ := strconv.ParseInt(id, 10, 64)
|
|
|
|
c.JSON(http.StatusOK, service.QueryDrafting(int32(int64Id)))
|
|
|
|
}
|
|
|
|
|
2023-07-28 14:13:03 +08:00
|
|
|
// 修改草稿信息
|
2023-07-14 18:09:07 +08:00
|
|
|
//
|
2023-07-28 14:13:03 +08:00
|
|
|
// @Summary 修改草稿信息
|
2023-07-14 18:09:07 +08:00
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
2023-07-28 14:13:03 +08:00
|
|
|
// @Description 修改草稿信息
|
2023-07-14 18:09:07 +08:00
|
|
|
// @Tags 草稿Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2023-07-25 17:18:37 +08:00
|
|
|
// @Param id path int true "草稿ID"
|
|
|
|
// @Param draftingDto query dto.DraftingDto true "修改的草稿信息"
|
2023-07-18 17:19:03 +08:00
|
|
|
// @Success 200 {object} nil
|
2023-07-14 18:09:07 +08:00
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/drafting/:id [put]
|
|
|
|
func updateDraftingInfo(c *gin.Context) {
|
|
|
|
id, exist := c.Params.Get("id")
|
|
|
|
if !exist {
|
2023-07-28 14:13:03 +08:00
|
|
|
c.JSON(http.StatusBadRequest, "必要参数id不存在")
|
|
|
|
return
|
2023-07-14 18:09:07 +08:00
|
|
|
}
|
|
|
|
zap.S().Debug("传入参数id为" + id)
|
|
|
|
req := dto.DraftingDto{}
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2023-07-28 14:13:03 +08:00
|
|
|
c.JSON(http.StatusBadRequest, "保存参数出错")
|
|
|
|
return
|
2023-07-14 18:09:07 +08:00
|
|
|
}
|
|
|
|
int64Id, _ := strconv.ParseInt(id, 10, 64)
|
2023-07-28 14:13:03 +08:00
|
|
|
result := service.UpdateDrafting(int32(int64Id), &req)
|
|
|
|
if !result {
|
|
|
|
c.JSON(http.StatusInternalServerError, "保存参数出错")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
2023-07-14 16:47:59 +08:00
|
|
|
}
|
2023-07-25 17:18:37 +08:00
|
|
|
|
|
|
|
// 删除草稿数据
|
|
|
|
//
|
|
|
|
// @Summary 删除草稿数据
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 删除草稿数据
|
|
|
|
// @Tags 草稿Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path int true "草稿ID"
|
|
|
|
// @Success 200 {object} nil
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/drafting/:id [delete]
|
|
|
|
func deleteDrafting(c *gin.Context) {
|
|
|
|
user, _ := c.Get(middleware.IdentityKey)
|
|
|
|
zap.S().Debug("id删除草稿的图形数据", user)
|
|
|
|
idStr := c.Param("id")
|
|
|
|
id, err := strconv.Atoi(idStr)
|
|
|
|
if err != nil {
|
2023-07-28 14:13:03 +08:00
|
|
|
c.JSON(http.StatusBadRequest, "id参数解析错误")
|
2023-07-25 17:18:37 +08:00
|
|
|
panic("id参数解析错误")
|
|
|
|
}
|
|
|
|
zap.S().Debug("id查询草稿的图形数据", id)
|
|
|
|
service.DeleteDraftingById(id)
|
2023-07-28 14:13:03 +08:00
|
|
|
c.JSON(http.StatusOK, true)
|
2023-07-25 17:18:37 +08:00
|
|
|
}
|