web
This commit is contained in:
139
server/internal/handler/platform.go
Normal file
139
server/internal/handler/platform.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"dianshang/internal/model"
|
||||
"dianshang/internal/service"
|
||||
"dianshang/pkg/response"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// PlatformHandler 平台处理器
|
||||
type PlatformHandler struct {
|
||||
platformService *service.PlatformService
|
||||
}
|
||||
|
||||
// NewPlatformHandler 创建平台处理器
|
||||
func NewPlatformHandler(platformService *service.PlatformService) *PlatformHandler {
|
||||
return &PlatformHandler{
|
||||
platformService: platformService,
|
||||
}
|
||||
}
|
||||
|
||||
// GetPlatforms 获取平台列表
|
||||
func (h *PlatformHandler) GetPlatforms(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
|
||||
name := c.Query("name")
|
||||
|
||||
var status *int
|
||||
if statusStr := c.Query("status"); statusStr != "" {
|
||||
if s, err := strconv.Atoi(statusStr); err == nil {
|
||||
status = &s
|
||||
}
|
||||
}
|
||||
|
||||
platforms, pagination, err := h.platformService.GetPlatformList(page, pageSize, status, name)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if pagination != nil {
|
||||
response.Page(c, platforms, pagination.Total, pagination.Page, pagination.PageSize)
|
||||
} else {
|
||||
response.Success(c, platforms)
|
||||
}
|
||||
}
|
||||
|
||||
// GetPlatform 获取平台详情
|
||||
func (h *PlatformHandler) GetPlatform(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "无效的平台ID")
|
||||
return
|
||||
}
|
||||
|
||||
platform, err := h.platformService.GetPlatformByID(uint(id))
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, platform)
|
||||
}
|
||||
|
||||
// CreatePlatform 创建平台
|
||||
func (h *PlatformHandler) CreatePlatform(c *gin.Context) {
|
||||
var platform model.Platform
|
||||
if err := c.ShouldBindJSON(&platform); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 验证必填字段
|
||||
if platform.Code == "" {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "平台代码不能为空")
|
||||
return
|
||||
}
|
||||
if platform.Name == "" {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "平台名称不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.platformService.CreatePlatform(&platform); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, platform)
|
||||
}
|
||||
|
||||
// UpdatePlatform 更新平台
|
||||
func (h *PlatformHandler) UpdatePlatform(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "无效的平台ID")
|
||||
return
|
||||
}
|
||||
|
||||
var updates map[string]interface{}
|
||||
if err := c.ShouldBindJSON(&updates); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.platformService.UpdatePlatform(uint(id), updates); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.SuccessWithMessage(c, "平台更新成功", nil)
|
||||
}
|
||||
|
||||
// DeletePlatform 删除平台
|
||||
func (h *PlatformHandler) DeletePlatform(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "无效的平台ID")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.platformService.DeletePlatform(uint(id)); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.SuccessWithMessage(c, "平台删除成功", nil)
|
||||
}
|
||||
|
||||
// GetAllActivePlatforms 获取所有启用的平台(用于下拉选择)
|
||||
func (h *PlatformHandler) GetAllActivePlatforms(c *gin.Context) {
|
||||
platforms, err := h.platformService.GetAllActivePlatforms()
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, platforms)
|
||||
}
|
||||
Reference in New Issue
Block a user