170 lines
3.9 KiB
Go
170 lines
3.9 KiB
Go
package handler
|
||
|
||
import (
|
||
"dd_fiber_api/internal/question"
|
||
"dd_fiber_api/internal/question/service"
|
||
"strconv"
|
||
|
||
"github.com/gofiber/fiber/v2"
|
||
)
|
||
|
||
// MaterialHandler 材料处理器
|
||
type MaterialHandler struct {
|
||
materialService *service.MaterialService
|
||
}
|
||
|
||
// NewMaterialHandler 创建材料处理器
|
||
func NewMaterialHandler(materialService *service.MaterialService) *MaterialHandler {
|
||
return &MaterialHandler{
|
||
materialService: materialService,
|
||
}
|
||
}
|
||
|
||
// CreateMaterial 创建材料
|
||
func (h *MaterialHandler) CreateMaterial(c *fiber.Ctx) error {
|
||
var req service.CreateMaterialRequest
|
||
if err := c.BodyParser(&req); err != nil {
|
||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||
"success": false,
|
||
"message": "请求参数解析失败: " + err.Error(),
|
||
})
|
||
}
|
||
|
||
material, err := h.materialService.CreateMaterial(&req)
|
||
if err != nil {
|
||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||
"success": false,
|
||
"message": err.Error(),
|
||
})
|
||
}
|
||
|
||
return c.JSON(fiber.Map{
|
||
"success": true,
|
||
"message": "创建成功",
|
||
"id": material.ID,
|
||
"material": material,
|
||
})
|
||
}
|
||
|
||
// GetMaterial 获取材料
|
||
func (h *MaterialHandler) GetMaterial(c *fiber.Ctx) error {
|
||
id := c.Query("id")
|
||
if id == "" {
|
||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||
"success": false,
|
||
"message": "材料ID不能为空",
|
||
})
|
||
}
|
||
|
||
material, err := h.materialService.GetMaterial(id)
|
||
if err != nil {
|
||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
||
"success": false,
|
||
"message": err.Error(),
|
||
})
|
||
}
|
||
|
||
return c.JSON(fiber.Map{
|
||
"success": true,
|
||
"message": "获取成功",
|
||
"material": material,
|
||
})
|
||
}
|
||
|
||
// SearchMaterials 搜索材料
|
||
func (h *MaterialHandler) SearchMaterials(c *fiber.Ctx) error {
|
||
query := c.Query("query", "")
|
||
materialType := c.Query("type", "") // 材料类型:objective 或 subjective
|
||
pageStr := c.Query("page", "1")
|
||
pageSizeStr := c.Query("page_size", "10")
|
||
|
||
page, err := strconv.ParseInt(pageStr, 10, 32)
|
||
if err != nil || page < 1 {
|
||
page = 1
|
||
}
|
||
|
||
pageSize, err := strconv.ParseInt(pageSizeStr, 10, 32)
|
||
if err != nil || pageSize < 1 {
|
||
pageSize = 10
|
||
}
|
||
if pageSize > 100 {
|
||
pageSize = 100
|
||
}
|
||
|
||
var materialTypeEnum question.MaterialType
|
||
if materialType != "" {
|
||
materialTypeEnum = question.MaterialType(materialType)
|
||
}
|
||
|
||
materials, total, err := h.materialService.SearchMaterials(query, materialTypeEnum, int32(page), int32(pageSize))
|
||
if err != nil {
|
||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||
"success": false,
|
||
"message": err.Error(),
|
||
})
|
||
}
|
||
|
||
return c.JSON(fiber.Map{
|
||
"success": true,
|
||
"message": "获取成功",
|
||
"materials": materials,
|
||
"total": total,
|
||
})
|
||
}
|
||
|
||
// UpdateMaterial 更新材料
|
||
func (h *MaterialHandler) UpdateMaterial(c *fiber.Ctx) error {
|
||
var req service.UpdateMaterialRequest
|
||
if err := c.BodyParser(&req); err != nil {
|
||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||
"success": false,
|
||
"message": "请求参数解析失败: " + err.Error(),
|
||
})
|
||
}
|
||
|
||
if req.ID == "" {
|
||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||
"success": false,
|
||
"message": "材料ID不能为空",
|
||
})
|
||
}
|
||
|
||
err := h.materialService.UpdateMaterial(&req)
|
||
if err != nil {
|
||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||
"success": false,
|
||
"message": err.Error(),
|
||
})
|
||
}
|
||
|
||
return c.JSON(fiber.Map{
|
||
"success": true,
|
||
"message": "更新成功",
|
||
})
|
||
}
|
||
|
||
// DeleteMaterial 删除材料
|
||
func (h *MaterialHandler) DeleteMaterial(c *fiber.Ctx) error {
|
||
id := c.Params("id")
|
||
if id == "" {
|
||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||
"success": false,
|
||
"message": "材料ID不能为空",
|
||
})
|
||
}
|
||
|
||
err := h.materialService.DeleteMaterial(id)
|
||
if err != nil {
|
||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||
"success": false,
|
||
"message": err.Error(),
|
||
})
|
||
}
|
||
|
||
return c.JSON(fiber.Map{
|
||
"success": true,
|
||
"message": "删除成功",
|
||
})
|
||
}
|
||
|