245 lines
6.5 KiB
Go
245 lines
6.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
|
|
// CreateCompositeCase godoc
|
|
// @Summary Create a composite case
|
|
// @Description Create a new composite case
|
|
// @Tags composite
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body models.CreateCompositeCaseRequest true "Create Composite Case Request"
|
|
// @Success 201 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /composite-cases [post]
|
|
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 godoc
|
|
// @Summary Get a composite case
|
|
// @Description Get a composite case by ID
|
|
// @Tags composite
|
|
// @Produce json
|
|
// @Param id path int true "Composite Case ID"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Failure 404 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /composite-cases/{id} [get]
|
|
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 godoc
|
|
// @Summary Update a composite case
|
|
// @Description Update a composite case by ID
|
|
// @Tags composite
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Composite Case ID"
|
|
// @Param body body models.UpdateCompositeCaseRequest true "Update Composite Case Request"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Failure 404 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /composite-cases/{id} [put]
|
|
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 godoc
|
|
// @Summary Delete a composite case
|
|
// @Description Delete a composite case by ID
|
|
// @Tags composite
|
|
// @Produce json
|
|
// @Param id path int true "Composite Case ID"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Failure 404 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /composite-cases/{id} [delete]
|
|
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 godoc
|
|
// @Summary List composite cases
|
|
// @Description List composite cases with pagination
|
|
// @Tags composite
|
|
// @Produce json
|
|
// @Param page query int false "Page number"
|
|
// @Param page_size query int false "Page size"
|
|
// @Param status query string false "Status"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /composite-cases [get]
|
|
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),
|
|
},
|
|
})
|
|
}
|