You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
3.6 KiB
135 lines
3.6 KiB
1 month ago
|
package controllers
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"main/logic"
|
||
|
"main/models"
|
||
|
"main/pkg/vt"
|
||
|
"main/utils"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"github.com/go-playground/validator/v10"
|
||
|
"go.uber.org/zap"
|
||
|
)
|
||
|
|
||
|
func GetAllStudentInfoHandler(c *gin.Context) {
|
||
|
info, err := logic.GetAllStudentInfoLogic()
|
||
|
if err != nil {
|
||
|
zap.L().Error("Get all student info error", zap.Error(err))
|
||
|
if err == utils.ErrorStuInfoEmpty {
|
||
|
utils.ResponseError(c, utils.CodeStudentInfoEmpty)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseError(c, utils.CodeServerBusy)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseSuccess(c, info)
|
||
|
}
|
||
|
|
||
|
func GetOneRandomStudentInfoHandler(c *gin.Context) {
|
||
|
info, err := logic.GetOneRandomStudentInfoLogic()
|
||
|
if err != nil {
|
||
|
zap.L().Error("Get student info error", zap.Error(err))
|
||
|
if err == utils.ErrorStuInfoEmpty {
|
||
|
utils.ResponseError(c, utils.CodeStudentInfoEmpty)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseError(c, utils.CodeServerBusy)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseSuccess(c, info)
|
||
|
}
|
||
|
|
||
|
func GetOneStudentInfoAccordScoreHandler(c *gin.Context) {
|
||
|
info, err := logic.GetOneStudentInfoAccordScoreLogic()
|
||
|
if err != nil {
|
||
|
zap.L().Error("Get one student info error", zap.Error(err))
|
||
|
if err == utils.ErrorStuInfoEmpty {
|
||
|
utils.ResponseError(c, utils.CodeStudentInfoEmpty)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseError(c, utils.CodeServerBusy)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseSuccess(c, info)
|
||
|
}
|
||
|
|
||
|
func GetNStudentInfoAccordScoreHandler(c *gin.Context) {
|
||
|
p := new(models.ParamNum)
|
||
|
if err := c.ShouldBindJSON(p); err != nil {
|
||
|
zap.L().Error("Get N student info with invalid param", zap.Error(err))
|
||
|
errs, ok := err.(validator.ValidationErrors)
|
||
|
if !ok {
|
||
|
utils.ResponseError(c, utils.CodeInvalidParam)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseErrorWithMsg(c, utils.CodeInvalidParam, vt.RemoveTopStruct(errs.Translate(vt.Trans)))
|
||
|
return
|
||
|
}
|
||
|
info, err := logic.GetNStudentInfoAccordScoreLogic(p)
|
||
|
if err != nil {
|
||
|
zap.L().Error("Get n student info error", zap.Error(err))
|
||
|
if err == utils.ErrorStuInfoEmpty {
|
||
|
utils.ResponseError(c, utils.CodeStudentInfoEmpty)
|
||
|
return
|
||
|
}
|
||
|
if err == utils.ErrorBeyondNum {
|
||
|
utils.ResponseError(c, utils.CodeTooMuchNum)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseError(c, utils.CodeServerBusy)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseSuccess(c, info)
|
||
|
}
|
||
|
|
||
|
func GetTargetStudentInfoHandler(c *gin.Context) {
|
||
|
p := new(models.ParamUser)
|
||
|
if err := c.ShouldBindJSON(p); err != nil {
|
||
|
zap.L().Error("Get Target student info with invalid param", zap.Error(err))
|
||
|
errs, ok := err.(validator.ValidationErrors)
|
||
|
if !ok {
|
||
|
utils.ResponseError(c, utils.CodeInvalidParam)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseErrorWithMsg(c, utils.CodeInvalidParam, vt.RemoveTopStruct(errs.Translate(vt.Trans)))
|
||
|
return
|
||
|
}
|
||
|
back, err := logic.GetTargetStudentInfoLogic(p)
|
||
|
if err != nil {
|
||
|
zap.L().Error("Get Target failed", zap.Error(err))
|
||
|
if errors.Is(err, utils.ErrorInvalidStuId) {
|
||
|
utils.ResponseError(c, utils.CodeInvalidParam)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseError(c, utils.CodeServerBusy)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseSuccess(c, back)
|
||
|
}
|
||
|
|
||
|
func ChangeStuScoreHandler(c *gin.Context) {
|
||
|
p := new(models.ParamUser)
|
||
|
if err := c.ShouldBindJSON(p); err != nil {
|
||
|
zap.L().Error("Change student score with invalid param", zap.Error(err))
|
||
|
errs, ok := err.(validator.ValidationErrors)
|
||
|
if !ok {
|
||
|
utils.ResponseError(c, utils.CodeInvalidParam)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseErrorWithMsg(c, utils.CodeInvalidParam, vt.RemoveTopStruct(errs.Translate(vt.Trans)))
|
||
|
return
|
||
|
}
|
||
|
back, err := logic.ChangeStuScoreLogic(p)
|
||
|
if err != nil {
|
||
|
zap.L().Error("Change score failed", zap.Error(err))
|
||
|
if errors.Is(err, utils.ErrorInvalidStuId) {
|
||
|
utils.ResponseError(c, utils.CodeInvalidParam)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseError(c, utils.CodeServerBusy)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseSuccess(c, back)
|
||
|
}
|