124 lines
3.3 KiB
Go
124 lines
3.3 KiB
Go
// Package controller 控制层
|
|
package controller
|
|
|
|
import (
|
|
"server/common"
|
|
"server/modules/yx/entity"
|
|
"server/modules/yx/service"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type YxVolunteerController struct {
|
|
service *service.YxVolunteerService
|
|
}
|
|
|
|
func NewYxVolunteerController() *YxVolunteerController {
|
|
return &YxVolunteerController{service: service.NewYxVolunteerService()}
|
|
}
|
|
|
|
// RegisterRoutes 注册路由
|
|
func (ctrl *YxVolunteerController) RegisterRoutes(rg *gin.RouterGroup) {
|
|
group := rg.Group("/yx-volunteers")
|
|
{
|
|
group.GET("", ctrl.List)
|
|
group.GET("/:id", ctrl.Get)
|
|
group.POST("", ctrl.Create)
|
|
group.PUT("/:id", ctrl.Update)
|
|
group.DELETE("/:id", ctrl.Delete)
|
|
}
|
|
}
|
|
|
|
// List 获取志愿列表
|
|
// @Summary 获取志愿列表
|
|
// @Tags 志愿
|
|
// @Param page query int false "页码" default(1)
|
|
// @Param size query int false "每页数量" default(10)
|
|
// @Success 200 {object} common.Response
|
|
// @Router /yx-volunteers [get]
|
|
func (ctrl *YxVolunteerController) 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.Success(c, gin.H{
|
|
"items": items,
|
|
"total": total,
|
|
})
|
|
}
|
|
|
|
// Get 获取单个志愿
|
|
// @Summary 获取单个志愿
|
|
// @Tags 志愿
|
|
// @Param id path string true "ID"
|
|
// @Success 200 {object} common.Response
|
|
// @Router /yx-volunteers/{id} [get]
|
|
func (ctrl *YxVolunteerController) Get(c *gin.Context) {
|
|
id := c.Param("id")
|
|
item, err := ctrl.service.GetByID(id)
|
|
if err != nil {
|
|
common.Error(c, 404, "未找到记录")
|
|
return
|
|
}
|
|
common.Success(c, item)
|
|
}
|
|
|
|
// Create 创建志愿
|
|
// @Summary 创建志愿
|
|
// @Tags 志愿
|
|
// @Param item body entity.YxVolunteer true "志愿信息"
|
|
// @Success 200 {object} common.Response
|
|
// @Router /yx-volunteers [post]
|
|
func (ctrl *YxVolunteerController) Create(c *gin.Context) {
|
|
var item entity.YxVolunteer
|
|
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)
|
|
}
|
|
|
|
// Update 更新志愿
|
|
// @Summary 更新志愿
|
|
// @Tags 志愿
|
|
// @Param id path string true "ID"
|
|
// @Param item body entity.YxVolunteer true "志愿信息"
|
|
// @Success 200 {object} common.Response
|
|
// @Router /yx-volunteers/{id} [put]
|
|
func (ctrl *YxVolunteerController) Update(c *gin.Context) {
|
|
var item entity.YxVolunteer
|
|
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)
|
|
}
|
|
|
|
// Delete 删除志愿
|
|
// @Summary 删除志愿
|
|
// @Tags 志愿
|
|
// @Param id path string true "ID"
|
|
// @Success 200 {object} common.Response
|
|
// @Router /yx-volunteers/{id} [delete]
|
|
func (ctrl *YxVolunteerController) Delete(c *gin.Context) {
|
|
id := c.Param("id")
|
|
if err := ctrl.service.Delete(id); err != nil {
|
|
common.Error(c, 500, err.Error())
|
|
return
|
|
}
|
|
common.Success(c, nil)
|
|
}
|