From a0dc35bc32efa0033212ab03036b70cae7d55a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9F=83=E6=8B=89?= Date: Sun, 21 Apr 2024 12:27:59 +0800 Subject: [PATCH] fix: foreign key error --- internal/controller/pod.go | 10 +++++----- internal/model/pod.go | 16 ++++++++-------- internal/model/submission.go | 34 +++++++++++++++++----------------- internal/service/pod.go | 24 +++++++----------------- internal/service/submission.go | 2 +- 5 files changed, 38 insertions(+), 48 deletions(-) diff --git a/internal/controller/pod.go b/internal/controller/pod.go index ea83a1a9..e19b7c25 100644 --- a/internal/controller/pod.go +++ b/internal/controller/pod.go @@ -38,17 +38,17 @@ func NewPodController(appService *service.Service) IPodController { // @Param input body request.PodCreateRequest true "PodCreateRequest" // @Router /pods/ [post] func (c *PodController) Create(ctx *gin.Context) { - instanceCreateRequest := request.PodCreateRequest{} - if err := ctx.ShouldBindJSON(&instanceCreateRequest); err != nil { + podCreateRequest := request.PodCreateRequest{} + if err := ctx.ShouldBindJSON(&podCreateRequest); err != nil { ctx.JSON(http.StatusBadRequest, gin.H{ "code": http.StatusBadRequest, - "msg": validator.GetValidMsg(err, &instanceCreateRequest), + "msg": validator.GetValidMsg(err, &podCreateRequest), }) return } user := ctx.MustGet("user").(*model.User) - instanceCreateRequest.UserID = user.ID - pod, err := c.podService.Create(instanceCreateRequest) + podCreateRequest.UserID = user.ID + pod, err := c.podService.Create(podCreateRequest) if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{ "code": http.StatusBadRequest, diff --git a/internal/model/pod.go b/internal/model/pod.go index adf52286..5e648640 100644 --- a/internal/model/pod.go +++ b/internal/model/pod.go @@ -2,14 +2,14 @@ package model type Pod struct { ID uint `json:"id"` - GameID uint `json:"game_id"` - Game *Game `json:"game,omitempty"` - UserID uint `json:"user_id"` - User *User `json:"user,omitempty"` - TeamID uint `json:"team_id"` - Team *Team `json:"team,omitempty"` - ChallengeID uint `json:"challenge_id"` - Challenge *Challenge `json:"challenge,omitempty"` + GameID *uint `gorm:"index;null;default:null" json:"game_id"` + Game *Game `gorm:"foreignkey:GameID;association_foreignkey:ID" json:"game,omitempty"` + UserID *uint `gorm:"index;null;default:null" json:"user_id"` + User *User `gorm:"foreignkey:UserID;association_foreignkey:ID" json:"user,omitempty"` + TeamID *uint `gorm:"index;null;default:null" json:"team_id"` + Team *Team `gorm:"foreignkey:TeamID;association_foreignkey:ID" json:"team,omitempty"` + ChallengeID *uint `gorm:"index;null;default:null" json:"challenge_id"` + Challenge *Challenge `gorm:"foreignkey:ChallengeID;association_foreignkey:ID" json:"challenge,omitempty"` RemovedAt int64 `json:"removed_at"` Nats []*Nat `json:"nats,omitempty"` } diff --git a/internal/model/submission.go b/internal/model/submission.go index 3baeb6cf..fc356666 100644 --- a/internal/model/submission.go +++ b/internal/model/submission.go @@ -1,23 +1,23 @@ package model type Submission struct { - ID uint `json:"id"` // The submission's id. As primary key. - Flag string `gorm:"type:varchar(128);not null" json:"flag,omitempty"` // The flag which was submitted for judgement. - Status int `gorm:"not null;default:0" json:"status"` // The status of the submission. (0-meaningless, 1-accepted, 2-incorrect, 3-cheat, 4-invalid(duplicate, etc.)) - UserID uint `gorm:"not null" json:"user_id"` // The user who submitted the flag. - User *User `json:"user"` // The user who submitted the flag. - ChallengeID uint `gorm:"not null;" json:"challenge_id"` // The challenge which is related to this submission. - Challenge *Challenge `json:"challenge"` // The challenge which is related to this submission. - GameChallengeID *uint `json:"game_challenge_id,omitempty"` // The game_challenge which is related to this submission. - GameChallenge *GameChallenge `json:"game_challenge,omitempty"` // The game_challenge which is related to this submission. - TeamID *uint `json:"team_id,omitempty"` // The team which submitted the flag. (Must be set when GameID is set) - Team *Team `json:"team,omitempty"` // The team which submitted the flag. - GameID *uint `json:"game_id,omitempty"` // The game which is related to this submission. (Must be set when TeamID is set) - Game *Game `json:"game,omitempty"` // The game which is related to this submission. - Rank int64 `json:"rank"` // The rank of the submission. - Pts int64 `gorm:"-" json:"pts"` // The points of the submission. - CreatedAt int64 `gorm:"autoUpdateTime:milli" json:"created_at,omitempty"` // The submission's creation time. - UpdatedAt int64 `gorm:"autoUpdateTime:milli" json:"updated_at,omitempty"` // The submission's last update time. + ID uint `json:"id"` // The submission's id. As primary key. + Flag string `gorm:"type:varchar(128);not null" json:"flag,omitempty"` // The flag which was submitted for judgement. + Status int `gorm:"not null;default:0" json:"status"` // The status of the submission. (0-meaningless, 1-accepted, 2-incorrect, 3-cheat, 4-invalid(duplicate, etc.)) + UserID uint `gorm:"not null" json:"user_id"` // The user who submitted the flag. + User *User `json:"user"` // The user who submitted the flag. + ChallengeID uint `gorm:"not null;" json:"challenge_id"` // The challenge which is related to this submission. + Challenge *Challenge `json:"challenge"` // The challenge which is related to this submission. + GameChallengeID *uint `gorm:"index;null;default:null" json:"game_challenge_id,omitempty"` // The game_challenge which is related to this submission. + GameChallenge *GameChallenge `gorm:"foreignkey:GameChallengeID;association_foreignkey:ID" json:"game_challenge,omitempty"` // The game_challenge which is related to this submission. + TeamID *uint `gorm:"index;null;default:null" json:"team_id,omitempty"` // The team which submitted the flag. (Must be set when GameID is set) + Team *Team `gorm:"foreignkey:TeamID;association_foreignkey:ID" json:"team,omitempty"` // The team which submitted the flag. + GameID *uint `gorm:"index;null;default:null" json:"game_id,omitempty"` // The game which is related to this submission. (Must be set when TeamID is set) + Game *Game `gorm:"foreignkey:GameID;association_foreignkey:ID" json:"game,omitempty"` // The game which is related to this submission. + Rank int64 `json:"rank"` // The rank of the submission. + Pts int64 `gorm:"-" json:"pts"` // The points of the submission. + CreatedAt int64 `gorm:"autoUpdateTime:milli" json:"created_at,omitempty"` // The submission's creation time. + UpdatedAt int64 `gorm:"autoUpdateTime:milli" json:"updated_at,omitempty"` // The submission's last update time. } func (s *Submission) Simplify() { diff --git a/internal/service/pod.go b/internal/service/pod.go index 6a5725af..725a232a 100644 --- a/internal/service/pod.go +++ b/internal/service/pod.go @@ -150,22 +150,12 @@ func (t *PodService) Create(req request.PodCreateRequest) (res response.PodStatu nats, err := ctnManager.Setup() - var gameID uint - if req.GameID != nil { - gameID = *(req.GameID) - } - - var teamID uint - if req.TeamID != nil { - teamID = *(req.TeamID) - } - // Create Pod model, get Pod's GameID pod, _ := t.podRepository.Create(model.Pod{ - ChallengeID: req.ChallengeID, - UserID: req.UserID, - GameID: gameID, - TeamID: teamID, + ChallengeID: &req.ChallengeID, + UserID: &req.UserID, + GameID: req.GameID, + TeamID: req.TeamID, RemovedAt: removedAt, Nats: nats, }) @@ -187,14 +177,14 @@ func (t *PodService) Create(req request.PodCreateRequest) (res response.PodStatu return response.PodStatusResponse{ ID: pod.ID, - Nats: nats, + Nats: pod.Nats, RemovedAt: removedAt, }, err } func (t *PodService) Status(podID uint) (rep response.PodStatusResponse, err error) { rep = response.PodStatusResponse{} - instance, err := t.podRepository.FindById(podID) + pod, err := t.podRepository.FindById(podID) var ctn manager.IContainerManager if PodManagers[podID] != nil { ctn = PodManagers[podID] @@ -204,7 +194,7 @@ func (t *PodService) Status(podID uint) (rep response.PodStatusResponse, err err rep.Status = status } rep.ID = podID - rep.RemovedAt = instance.RemovedAt + rep.RemovedAt = pod.RemovedAt return rep, nil } return rep, errors.New("获取失败") diff --git a/internal/service/submission.go b/internal/service/submission.go index e27f7a1c..7b19c853 100644 --- a/internal/service/submission.go +++ b/internal/service/submission.go @@ -61,7 +61,7 @@ func (t *SubmissionService) JudgeDynamicChallenge(req request.SubmissionCreateRe } for _, pod := range perhapsPods { if req.Flag == flagMap[pod.ID] { - if (req.UserID == pod.UserID && req.UserID != 0) || (req.TeamID != nil && *(req.TeamID) == pod.TeamID) { + if (pod.UserID != nil && req.UserID == *(pod.UserID) && req.UserID != 0) || (pod.TeamID != nil && req.TeamID != nil && *(req.TeamID) == *(pod.TeamID)) { status = 2 } else { status = 3