golang-yitisheng-server/server/modules/yx/controller/yx_calculation_major_contro...

169 lines
5.3 KiB
Go

// Package controller 控制器层
package controller
import (
"strconv"
"server/common"
"server/modules/yx/entity"
"server/modules/yx/service"
"github.com/gin-gonic/gin"
)
type YxCalculationMajorController struct {
service *service.YxCalculationMajorService
}
func NewYxCalculationMajorController() *YxCalculationMajorController {
return &YxCalculationMajorController{service: service.NewYxCalculationMajorService()}
}
func (ctrl *YxCalculationMajorController) RegisterRoutes(r *gin.RouterGroup) {
r.GET("/yx-calculation-majors", ctrl.List)
r.GET("/yx-calculation-majors/:id", ctrl.GetByID)
r.POST("/yx-calculation-majors", ctrl.Create)
r.PUT("/yx-calculation-majors/:id", ctrl.Update)
r.PATCH("/yx-calculation-majors/:id", ctrl.UpdateFields)
r.DELETE("/yx-calculation-majors/:id", ctrl.Delete)
r.POST("/yx-calculation-majors/batch", ctrl.BatchCreate)
r.DELETE("/yx-calculation-majors/batch", ctrl.BatchDelete)
}
// @Summary 获取计算专业列表
// @Tags 计算专业
// @Param page query int false "页码"
// @Param size query int false "每页数量"
// @Success 200 {object} common.Response
// @Router /yx-calculation-majors [get]
func (ctrl *YxCalculationMajorController) List(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
items, total, err := ctrl.service.List(page, size)
if err != nil {
common.Error(c, 500, err.Error())
return
}
common.SuccessPage(c, items, total, page, size)
}
// @Summary 获取单个计算专业
// @Tags 计算专业
// @Param id path string true "ID"
// @Success 200 {object} common.Response
// @Router /yx-calculation-majors/{id} [get]
func (ctrl *YxCalculationMajorController) GetByID(c *gin.Context) {
item, err := ctrl.service.GetByID(c.Param("id"))
if err != nil {
common.Error(c, 404, "未找到")
return
}
common.Success(c, item)
}
// @Summary 创建计算专业
// @Tags 计算专业
// @Param item body entity.YxCalculationMajor true "计算专业信息"
// @Success 200 {object} common.Response
// @Router /yx-calculation-majors [post]
func (ctrl *YxCalculationMajorController) Create(c *gin.Context) {
var item entity.YxCalculationMajor
if err := c.ShouldBindJSON(&item); err != nil {
common.Error(c, 400, "参数错误")
return
}
if err := ctrl.service.Create(&item); err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, item)
}
// @Summary 更新计算专业
// @Tags 计算专业
// @Param id path string true "ID"
// @Param item body entity.YxCalculationMajor true "计算专业信息"
// @Success 200 {object} common.Response
// @Router /yx-calculation-majors/{id} [put]
func (ctrl *YxCalculationMajorController) Update(c *gin.Context) {
var item entity.YxCalculationMajor
if err := c.ShouldBindJSON(&item); err != nil {
common.Error(c, 400, "参数错误")
return
}
item.ID = c.Param("id")
if err := ctrl.service.Update(&item); err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, item)
}
// @Summary 动态字段更新
// @Tags 计算专业
// @Param id path string true "ID"
// @Param fields body map[string]interface{} true "要更新的字段"
// @Success 200 {object} common.Response
// @Router /yx-calculation-majors/{id} [patch]
func (ctrl *YxCalculationMajorController) UpdateFields(c *gin.Context) {
var fields map[string]interface{}
if err := c.ShouldBindJSON(&fields); err != nil {
common.Error(c, 400, "参数错误")
return
}
if err := ctrl.service.UpdateFields(c.Param("id"), fields); err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, nil)
}
// @Summary 删除计算专业
// @Tags 计算专业
// @Param id path string true "ID"
// @Success 200 {object} common.Response
// @Router /yx-calculation-majors/{id} [delete]
func (ctrl *YxCalculationMajorController) Delete(c *gin.Context) {
if err := ctrl.service.Delete(c.Param("id")); err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, nil)
}
// @Summary 批量创建计算专业
// @Tags 计算专业
// @Param items body []entity.YxCalculationMajor true "计算专业列表"
// @Success 200 {object} common.Response
// @Router /yx-calculation-majors/batch [post]
func (ctrl *YxCalculationMajorController) BatchCreate(c *gin.Context) {
var items []entity.YxCalculationMajor
if err := c.ShouldBindJSON(&items); err != nil {
common.Error(c, 400, "参数错误")
return
}
if err := ctrl.service.BatchCreate(items); err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, nil)
}
// @Summary 批量删除计算专业
// @Tags 计算专业
// @Param ids body []string true "ID列表"
// @Success 200 {object} common.Response
// @Router /yx-calculation-majors/batch [delete]
func (ctrl *YxCalculationMajorController) BatchDelete(c *gin.Context) {
var ids []string
if err := c.ShouldBindJSON(&ids); err != nil {
common.Error(c, 400, "参数错误")
return
}
if err := ctrl.service.BatchDelete(ids); err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, nil)
}