120 lines
3.0 KiB
Go
120 lines
3.0 KiB
Go
package handler
|
||
|
||
import (
|
||
"exam_registration/internal/service"
|
||
"exam_registration/pkg/response"
|
||
"github.com/gin-gonic/gin"
|
||
"strconv"
|
||
)
|
||
|
||
type RegistrationHandler struct {
|
||
registrationService *service.RegistrationService
|
||
}
|
||
|
||
func NewRegistrationHandler() *RegistrationHandler {
|
||
return &RegistrationHandler{
|
||
registrationService: &service.RegistrationService{},
|
||
}
|
||
}
|
||
|
||
// CreateRegistration 创建报名
|
||
func (h *RegistrationHandler) CreateRegistration(c *gin.Context) {
|
||
userID, _ := c.Get("user_id")
|
||
|
||
var req struct {
|
||
ExamID uint64 `json:"exam_id" binding:"required"`
|
||
Remark string `json:"remark"`
|
||
}
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
response.Error(c, response.BAD_REQUEST, "参数错误")
|
||
return
|
||
}
|
||
|
||
if err := h.registrationService.CreateRegistration(userID.(uint64), req.ExamID, req.Remark); err != nil {
|
||
response.Error(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, nil)
|
||
}
|
||
|
||
// GetRegistrationList 获取报名列表
|
||
func (h *RegistrationHandler) GetRegistrationList(c *gin.Context) {
|
||
userIDStr := c.Query("user_id")
|
||
examIDStr := c.Query("exam_id")
|
||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
||
|
||
var userID, examID int
|
||
if userIDStr != "" {
|
||
userID, _ = strconv.Atoi(userIDStr)
|
||
}
|
||
if examIDStr != "" {
|
||
examID, _ = strconv.Atoi(examIDStr)
|
||
}
|
||
|
||
registrations, total, err := h.registrationService.GetRegistrationList(userID, examID, page, pageSize)
|
||
if err != nil {
|
||
response.Error(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, gin.H{
|
||
"list": registrations,
|
||
"total": total,
|
||
"page": page,
|
||
"pageSize": pageSize,
|
||
})
|
||
}
|
||
|
||
// AuditRegistration 审核报名
|
||
func (h *RegistrationHandler) AuditRegistration(c *gin.Context) {
|
||
regID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||
|
||
var req struct {
|
||
Status int `json:"status" binding:"required"` // 1:通过,2:拒绝
|
||
Comment string `json:"comment"`
|
||
}
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
response.Error(c, response.BAD_REQUEST, "参数错误")
|
||
return
|
||
}
|
||
|
||
if err := h.registrationService.AuditRegistration(regID, req.Status, req.Comment); err != nil {
|
||
response.Error(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, nil)
|
||
}
|
||
|
||
// UpdateRegistration 更新报名信息
|
||
func (h *RegistrationHandler) UpdateRegistration(c *gin.Context) {
|
||
regID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||
|
||
var updates map[string]interface{}
|
||
if err := c.ShouldBindJSON(&updates); err != nil {
|
||
response.Error(c, response.BAD_REQUEST, "参数错误")
|
||
return
|
||
}
|
||
|
||
if err := h.registrationService.UpdateRegistration(regID, updates); err != nil {
|
||
response.Error(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, nil)
|
||
}
|
||
|
||
// DeleteRegistration 取消报名
|
||
func (h *RegistrationHandler) DeleteRegistration(c *gin.Context) {
|
||
regID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||
|
||
if err := h.registrationService.DeleteRegistration(regID); err != nil {
|
||
response.Error(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, nil)
|
||
}
|