192 lines
4.5 KiB
Go
192 lines
4.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"beacon/models"
|
|
"beacon/services"
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type CompositeCaseHandler struct {
|
|
service *services.CompositeCaseService
|
|
}
|
|
|
|
// NewCompositeCaseHandler 创建 CompositeCaseHandler 实例
|
|
func NewCompositeCaseHandler() *CompositeCaseHandler {
|
|
return &CompositeCaseHandler{
|
|
service: &services.CompositeCaseService{},
|
|
}
|
|
}
|
|
|
|
// CreateCompositeCase POST /api/composite-cases
|
|
func (h *CompositeCaseHandler) CreateCompositeCase(c *gin.Context) {
|
|
var req models.CreateCompositeCaseRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "请求参数错误",
|
|
"details": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
zap.L().Info("req", zap.Any("req", req))
|
|
compositeCase, err := h.service.CreateCompositeCase(&req)
|
|
zap.L().Info("compositeCase", zap.Any("compositeCase", compositeCase))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "创建复合案例失败",
|
|
"details": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, gin.H{
|
|
"message": "创建复合案例成功",
|
|
"data": "compositeCase",
|
|
})
|
|
}
|
|
|
|
// GetCompositeCase GET /api/composite-cases/{id}
|
|
func (h *CompositeCaseHandler) GetCompositeCase(c *gin.Context) {
|
|
idParam := c.Param("id")
|
|
id, err := strconv.ParseUint(idParam, 10, 32)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "无效的ID参数",
|
|
})
|
|
return
|
|
}
|
|
|
|
compositeCase, err := h.service.GetCompositeCaseByID(uint(id))
|
|
if err != nil {
|
|
if err.Error() == "复合案例不存在" {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "获取复合案例失败",
|
|
"details": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "获取复合案例成功",
|
|
"data": compositeCase,
|
|
})
|
|
}
|
|
|
|
// UpdateCompositeCase PUT /api/composite-cases/{id}
|
|
func (h *CompositeCaseHandler) UpdateCompositeCase(c *gin.Context) {
|
|
idParam := c.Param("id")
|
|
id, err := strconv.ParseUint(idParam, 10, 32)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "无效的ID参数",
|
|
})
|
|
return
|
|
}
|
|
|
|
var req models.UpdateCompositeCaseRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "请求参数错误",
|
|
"details": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
compositeCase, err := h.service.UpdateCompositeCase(uint(id), &req)
|
|
if err != nil {
|
|
if err.Error() == "复合案例不存在" {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "更新复合案例失败",
|
|
"details": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "更新复合案例成功",
|
|
"data": compositeCase,
|
|
})
|
|
}
|
|
|
|
// DeleteCompositeCase DELETE /api/composite-cases/{id}
|
|
func (h *CompositeCaseHandler) DeleteCompositeCase(c *gin.Context) {
|
|
idParam := c.Param("id")
|
|
id, err := strconv.ParseUint(idParam, 10, 32)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "无效的ID参数",
|
|
})
|
|
return
|
|
}
|
|
|
|
err = h.service.DeleteCompositeCase(uint(id))
|
|
if err != nil {
|
|
if err.Error() == "复合案例不存在" {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "删除复合案例失败",
|
|
"details": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "删除复合案例成功",
|
|
})
|
|
}
|
|
|
|
// ListCompositeCases GET /api/composite-cases
|
|
func (h *CompositeCaseHandler) ListCompositeCases(c *gin.Context) {
|
|
// 获取查询参数
|
|
pageStr := c.DefaultQuery("page", "1")
|
|
pageSizeStr := c.DefaultQuery("page_size", "10")
|
|
status := c.Query("status")
|
|
|
|
page, err := strconv.Atoi(pageStr)
|
|
if err != nil || page < 1 {
|
|
page = 1
|
|
}
|
|
|
|
pageSize, err := strconv.Atoi(pageSizeStr)
|
|
if err != nil || pageSize < 1 || pageSize > 100 {
|
|
pageSize = 10
|
|
}
|
|
|
|
compositeCases, total, err := h.service.ListCompositeCases(page, pageSize, status)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "获取复合案例列表失败",
|
|
"details": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "获取复合案例列表成功",
|
|
"data": gin.H{
|
|
"items": compositeCases,
|
|
"total": total,
|
|
"page": page,
|
|
"page_size": pageSize,
|
|
"total_pages": (total + int64(pageSize) - 1) / int64(pageSize),
|
|
},
|
|
})
|
|
}
|