Skip to content

Commit

Permalink
fix: foreign key error
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Apr 21, 2024
1 parent 5f6ef91 commit a0dc35b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 48 deletions.
10 changes: 5 additions & 5 deletions internal/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions internal/model/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
34 changes: 17 additions & 17 deletions internal/model/submission.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
24 changes: 7 additions & 17 deletions internal/service/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand All @@ -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]
Expand All @@ -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("获取失败")
Expand Down
2 changes: 1 addition & 1 deletion internal/service/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a0dc35b

Please sign in to comment.