rts-sim-testing-service/api/publishedGi.go

227 lines
7.2 KiB
Go
Raw Normal View History

2023-07-18 17:19:03 +08:00
package api
import (
"log/slog"
"net/http"
"strconv"
2023-07-18 17:19:03 +08:00
jwt "github.com/appleboy/gin-jwt/v2"
"github.com/gin-gonic/gin"
"joylink.club/bj-rtsts-server/db/model"
"joylink.club/bj-rtsts-server/dto"
"joylink.club/bj-rtsts-server/dto/publishedGi"
2023-07-18 17:19:03 +08:00
"joylink.club/bj-rtsts-server/middleware"
"joylink.club/bj-rtsts-server/service"
)
func InitPublishedGiRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
2023-08-30 09:28:21 +08:00
authed := api.Group("/v1/publishedGi").Use(authMiddleware.MiddlewareFunc(), middleware.PermissMiddleware)
2023-07-18 17:19:03 +08:00
authed.GET("/paging", pageQueryPublishedGi)
authed.GET("/list", listQueryPublishedGi)
authed.GET("/:id", getPublishedGiById)
authed.POST("/publish", publishFromDraft)
authed.DELETE("/:id", deletePublishedGiById)
authed.POST("/saveAsDrafting/:id", saveAsDraftingFromPublish)
authed.GET("/name", getPublishedGiByName)
2023-07-18 17:19:03 +08:00
}
// 分页查询发布的图形数据
//
// @Summary 分页查询发布的图形数据
//
// @Security JwtAuth
//
// @Description 可以通过名称过滤
// @Tags 发布的图形数据Api
// @Accept json
// @Produce json
// @Param pagePublishedGiReqDto query publishedGi.PublishedGiReqDto true "分页查询参数"
2023-07-18 17:19:03 +08:00
// @Success 200 {object} dto.PageDto
// @Failure 401 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/publishedGi/paging [get]
func pageQueryPublishedGi(c *gin.Context) {
user, _ := c.Get(middleware.IdentityKey)
slog.Debug("分页查询发布的图形数据", user)
req := publishedGi.PublishedGiReqDto{}
2023-07-18 17:19:03 +08:00
if err := c.ShouldBind(&req); err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
2023-07-18 17:19:03 +08:00
}
slog.Debug("分页查询发布的图形数据", req)
page := service.PageQueryPublishedGi(&req)
2023-07-18 17:19:03 +08:00
c.JSON(http.StatusOK, page)
}
// 列表查询发布的图形数据
//
// @Summary 列表查询发布的图形数据
//
// @Security JwtAuth
//
// @Description 可以通过名称过滤
// @Tags 发布的图形数据Api
// @Accept json
// @Produce json
2023-08-28 15:24:17 +08:00
// @Param publishedGiListReqDto query publishedGi.PublishedGiListReqDto true "查询参数"
2023-07-18 17:19:03 +08:00
// @Success 200 {object} []model.PublishedGi
// @Failure 401 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/publishedGi/list [get]
func listQueryPublishedGi(c *gin.Context) {
user, _ := c.Get(middleware.IdentityKey)
slog.Debug("列表查询发布的图形数据", user)
req := publishedGi.PublishedGiListReqDto{}
if err := c.ShouldBindQuery(&req); err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
2023-07-18 17:19:03 +08:00
}
slog.Debug("列表查询发布的图形数据", req)
list := service.ListQueryPublishedGi(&req)
2023-07-18 17:19:03 +08:00
c.JSON(http.StatusOK, list)
}
// id 查询发布的图形数据
//
// @Summary id查询发布的图形数据
//
// @Security JwtAuth
//
// @Description 可以通过名称过滤
// @Tags 发布的图形数据Api
// @Accept json
// @Produce json
// @Param id path int true "id"
// @Success 200 {object} model.PublishedGi
// @Failure 401 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/publishedGi/{id} [get]
func getPublishedGiById(c *gin.Context) {
user, _ := c.Get(middleware.IdentityKey)
slog.Debug("id查询发布的图形数据", user)
2023-07-18 17:19:03 +08:00
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
2023-07-18 17:19:03 +08:00
}
slog.Debug("id查询发布的图形数据", id)
2023-07-18 17:19:03 +08:00
entity, err := service.GetPublishedGiById(id)
if err != nil {
2023-08-30 18:05:11 +08:00
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
2023-07-18 17:19:03 +08:00
}
c.JSON(http.StatusOK, entity)
}
// 从草稿发布数据
2023-07-18 17:19:03 +08:00
//
// @Summary 从草稿发布数据
//
// @Security JwtAuth
//
// @Description
// @Tags 发布的图形数据Api
// @Accept json
// @Produce json
// @Param PublishReqDto query publishedGi.PublishReqDto true "查询参数"
// @Success 200
// @Failure 401 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/publishedGi/publish [post]
func publishFromDraft(c *gin.Context) {
user, _ := c.Get(middleware.IdentityKey)
slog.Debug("发布图形数据", user)
req := publishedGi.PublishReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
//todo
slog.Debug("发布图形数据请求参数", req)
service.PublishFormDraft(&req, user.(*model.User))
}
// id 删除发布的图形数据
//
// @Summary id删除发布的图形数据
2023-07-18 17:19:03 +08:00
//
// @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 500 {object} dto.ErrorDto
// @Router /api/v1/publishedGi/{id} [delete]
func deletePublishedGiById(c *gin.Context) {
user, _ := c.Get(middleware.IdentityKey)
slog.Debug("id删除发布的图形数据", user)
2023-07-18 17:19:03 +08:00
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
2023-07-18 17:19:03 +08:00
}
slog.Debug("id查询发布的图形数据", id)
2023-07-18 17:19:03 +08:00
service.DeletePublishedGiById(id)
}
// id 从发布数据拉取信息到草稿
//
// @Summary 从发布数据拉取信息到草稿
//
// @Security JwtAuth
//
// @Description 从发布数据拉取信息到草稿
// @Tags 发布的图形数据Api
// @Accept json
// @Produce json
// @Param id path int true "id"
// @Param PublishReqDto query publishedGi.PublishReqDto true "要保存的名称"
// @Success 200 {object} nil
// @Failure 401 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
2023-08-07 15:05:37 +08:00
// @Router /api/v1/publishedGi/saveAsDrafting/{id} [post]
func saveAsDraftingFromPublish(c *gin.Context) {
user, _ := c.Get(middleware.IdentityKey)
idStr := c.Param("id")
slog.Debug("用户拉取发布图形数据", "userId", user, "发布图数据id", idStr)
id, err := strconv.Atoi(idStr)
if err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
req := publishedGi.PublishReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
service.SaveAsDraftingFromPublish(int32(id), user.(*model.User), req.Name)
}
// 根据Code查询发布地图数据
//
// @Summary 根据Code查询发布地图数据
//
// @Security JwtAuth
//
// @Description 可以通过名称过滤
// @Tags 发布的图形数据Api
// @Accept json
// @Produce json
// @Param PublishedGiSingleQueryDto query publishedGi.PublishedGiSingleQueryDto true "查询参数"
// @Success 200 {object} []model.PublishedGi
// @Failure 401 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/publishedGi/name [get]
func getPublishedGiByName(c *gin.Context) {
user, _ := c.Get(middleware.IdentityKey)
slog.Debug("name查询发布的图形数据", user)
param := &publishedGi.PublishedGiSingleQueryDto{}
if err := c.ShouldBind(param); err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
slog.Debug("name查询发布的图形数据", param.Name)
entity, err := service.GetPublishedGiByName(param)
if err != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
}
c.JSON(http.StatusOK, entity)
}