Skip to content

Commit

Permalink
refactor: 重构注册、提交报名和中断中间件
Browse files Browse the repository at this point in the history
Signed-off-by: Penryn <15158052130@163.com>
  • Loading branch information
Penryn committed Nov 2, 2024
1 parent 22ab58f commit 66d7867
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
9 changes: 8 additions & 1 deletion controller/register/student_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func StudentRegister(context *gin.Context) {
return
}

var user model.Person
result := global.DB.Where("identity = ? Or tel = ?", postData.ID, postData.Contact.Tel).Take(&user)
if result.RowsAffected != 0 {
utility.ResponseError(context, "您已经注册过了,请到登录页面登录")
return
}

person := model.Person{
OpenId: jwtData.OpenID,
Name: postData.Name,
Expand All @@ -55,7 +62,7 @@ func StudentRegister(context *gin.Context) {
Type: 1,
}

result := global.DB.Create(&person)
result = global.DB.Create(&person)
if result.RowsAffected == 0 {
utility.ResponseError(context, "报名失败,请重试")
} else {
Expand Down
9 changes: 8 additions & 1 deletion controller/register/teacher_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ func TeacherRegister(context *gin.Context) {
return
}

var user model.Person
result := global.DB.Where("identity = ? Or tel = ?", postData.ID, postData.Contact.Tel).Take(&user)
if result.RowsAffected != 0 {
utility.ResponseError(context, "您已经注册过了,请到登录页面登录")
return
}

person := model.Person{
OpenId: jwtData.OpenID,
StuId: postData.StuID,
Expand All @@ -51,7 +58,7 @@ func TeacherRegister(context *gin.Context) {
Type: 2,
}

result := global.DB.Create(&person)
result = global.DB.Create(&person)
if result.RowsAffected == 0 {
utility.ResponseError(context, "报名失败,请重试")
} else {
Expand Down
4 changes: 4 additions & 0 deletions controller/team/submit_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func SubmitTeam(context *gin.Context) {
}

if person.Type == 1 {
if team.Submit != true {
utility.ResponseError(context, "队伍提交失败,等待后续补报")
return
}
teamID := strconv.Itoa(int(team.ID))
dailyRoute := utility.GetCurrentDate()*10 + team.Route
dailyRouteKey := strconv.Itoa(int(dailyRoute))
Expand Down
12 changes: 12 additions & 0 deletions middleware/intercept.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package middleware

import (
"github.com/gin-gonic/gin"
"walk-server/utility"
)

func Intercept(context *gin.Context) {
utility.ResponseError(context, "该操作不允许")
context.Abort()
return
}
24 changes: 12 additions & 12 deletions router/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func MountRoutes(router *gin.Engine) {
router.POST("/api/v1/redis2mysql", middleware.TokenRateLimiter, team.RedisToMysql) // 从 Redis 中导入数据到 MySQL
router.POST("/api/v1/redis2mysql", middleware.TokenRateLimiter, middleware.Intercept, team.RedisToMysql) // 从 Redis 中导入数据到 MySQL
api := router.Group("/api/v1", middleware.TokenRateLimiter)
{
if !gin.IsDebugging() {
Expand Down Expand Up @@ -46,19 +46,19 @@ func MountRoutes(router *gin.Engine) {
if gin.IsDebugging() {
teamApi.GET("/submit", team.SubmitTeam) // 提交团队
} else {
teamApi.GET("/submit", middleware.IsExpired, middleware.CanSubmit, team.SubmitTeam) // 提交团队
teamApi.GET("/submit", middleware.IsExpired, team.SubmitTeam) // 提交团队
}

teamApi.GET("/info", team.GetTeamInfo) // 获取团队信息
teamApi.POST("/random-list", team.GetRandomList) // 随机获取开放随机组队的团队列表
teamApi.POST("/random-join", middleware.IsExpired, team.RandomJoin) // 通过随机列表加入团队
teamApi.POST("/create", middleware.IsExpired, team.CreateTeam) // 创建团队
teamApi.POST("/update", middleware.IsExpired, team.UpdateTeam) // 修改队伍信息
teamApi.POST("/join", middleware.IsExpired, team.JoinTeam) // 加入团队
teamApi.GET("/leave", middleware.IsExpired, team.LeaveTeam) // 离开团队
teamApi.GET("/remove", middleware.IsExpired, team.RemoveMember) // 移除队员
teamApi.GET("/disband", middleware.IsExpired, team.DisbandTeam) // 解散团队
teamApi.GET("/rollback", middleware.IsExpired, team.RollBackTeam) // 撤销提交
teamApi.GET("/info", team.GetTeamInfo) // 获取团队信息
teamApi.POST("/random-list", team.GetRandomList) // 随机获取开放随机组队的团队列表
teamApi.POST("/random-join", middleware.IsExpired, team.RandomJoin) // 通过随机列表加入团队
teamApi.POST("/create", middleware.IsExpired, middleware.Intercept, team.CreateTeam) // 创建团队
teamApi.POST("/update", middleware.IsExpired, middleware.Intercept, team.UpdateTeam) // 修改队伍信息
teamApi.POST("/join", middleware.IsExpired, team.JoinTeam) // 加入团队
teamApi.GET("/leave", middleware.IsExpired, team.LeaveTeam) // 离开团队
teamApi.GET("/remove", middleware.IsExpired, team.RemoveMember) // 移除队员
teamApi.GET("/disband", middleware.IsExpired, middleware.Intercept, team.DisbandTeam) // 解散团队
teamApi.GET("/rollback", middleware.IsExpired, team.RollBackTeam) // 撤销提交
}

// 事件相关的 API
Expand Down

0 comments on commit 66d7867

Please sign in to comment.