Skip to content

Commit

Permalink
Add user and problem name for submission resource
Browse files Browse the repository at this point in the history
  • Loading branch information
suntt2019 committed Feb 25, 2021
1 parent 3561edb commit 0764cbf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
5 changes: 3 additions & 2 deletions app/controller/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func CreateSubmission(c echo.Context) error {
func GetSubmission(c echo.Context) error {
user := c.Get("user").(models.User)
submission := models.Submission{}
if err := base.DB.Preload("Problem").First(&submission, c.Param("id")).Error; err != nil {
if err := base.DB.Preload("Problem").Preload("User").First(&submission, c.Param("id")).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
if user.Can("read_submission") {
return c.JSON(http.StatusNotFound, response.ErrorResp("NOT_FOUND", nil))
Expand Down Expand Up @@ -152,7 +152,8 @@ func GetSubmissions(c echo.Context) error {
return err
}

query := base.DB.Model(&models.Submission{}).Where("problem_set_id = 0").Order("id DESC") // Force order by id desc.
query := base.DB.Model(&models.Submission{}).Preload("User").Preload("Problem").
Where("problem_set_id = 0").Order("id DESC") // Force order by id desc.

if req.ProblemId != 0 {
query = query.Where("problem_id = ?", req.ProblemId)
Expand Down
16 changes: 8 additions & 8 deletions app/controller/submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ func createSubmissionForTest(t *testing.T, name string, id int, problem *models.
problem.LoadTestCases()
submission = models.Submission{
UserID: user.ID,
User: user,
ProblemID: problem.ID,
Problem: problem,
ProblemSetId: 0,
LanguageName: "test_language",
Language: nil,
FileName: fmt.Sprintf("test_%s_code_file_name_%d.test_language", name, id),
Priority: models.PriorityDefault,
Judged: false,
Score: 0,
Status: "PENDING",
Runs: make([]models.Run, len(problem.TestCases)),
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
}
for i, testCase := range problem.TestCases {
submission.Runs[i] = models.Run{
Expand Down Expand Up @@ -462,13 +467,10 @@ func TestGetSubmissions(t *testing.T) {
submitter: &problemCreator2,
},
5: {
problem: &problem3,
submitter: &problemCreator2,
},
6: {
problem: &problem3,
submitter: &problemCreator3,
},
// TODO: test submission in problem sets
}
submissions := make([]models.Submission, len(submissionRelations))

Expand All @@ -477,8 +479,6 @@ func TestGetSubmissions(t *testing.T) {
newFileContent("code", "code_file_name", b64Encodef("test_get_submissions_code_%d", i)), 0)
}

base.DB.Model(submissions[5]).Update("problem_set_id", 1)

successTests := []struct {
name string
req request.GetSubmissionsRequest
Expand All @@ -498,7 +498,7 @@ func TestGetSubmissions(t *testing.T) {
Offset: 0,
},
submissions: []models.Submission{
submissions[6],
submissions[5],
submissions[4],
submissions[3],
submissions[2],
Expand All @@ -520,7 +520,7 @@ func TestGetSubmissions(t *testing.T) {
Offset: 0,
},
submissions: []models.Submission{
submissions[6],
submissions[5],
submissions[3],
},
Total: 2,
Expand Down
8 changes: 8 additions & 0 deletions app/response/resource/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ type Submission struct {
ID uint `json:"id"`

UserID uint `json:"user_id"`
User *User `json:"user"`
ProblemID uint `json:"problem_id"`
ProblemName string `json:"problem_name"`
ProblemSetId uint `json:"problem_set_id"` // 0 means not in problem set
Language string `json:"language"`

Expand All @@ -23,7 +25,9 @@ type Submission struct {
func (s *Submission) convert(submission *models.Submission) {
s.ID = submission.ID
s.UserID = submission.UserID
s.User = GetUser(submission.User)
s.ProblemID = submission.ProblemID
s.ProblemName = submission.Problem.Name
s.ProblemSetId = submission.ProblemSetId
s.Language = submission.LanguageName
s.Judged = submission.Judged
Expand All @@ -50,7 +54,9 @@ type SubmissionDetail struct {
ID uint `json:"id"`

UserID uint `json:"user_id"`
User *User `json:"user"`
ProblemID uint `json:"problem_id"`
ProblemName string `json:"problem_name"`
ProblemSetId uint `json:"problem_set_id"`
Language string `json:"language"`
FileName string `json:"file_name"`
Expand All @@ -68,7 +74,9 @@ type SubmissionDetail struct {
func (s *SubmissionDetail) convert(submission *models.Submission) {
s.ID = submission.ID
s.UserID = submission.UserID
s.User = GetUser(submission.User)
s.ProblemID = submission.ProblemID
s.ProblemName = submission.Problem.Name
s.ProblemSetId = submission.ProblemSetId
s.Language = submission.LanguageName
s.FileName = submission.FileName
Expand Down
24 changes: 24 additions & 0 deletions app/response/resource/submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,19 @@ func TestGetRunAndGetRunSlice(t *testing.T) {
}

func TestGetSubmissionAndGetSubmissionDetail(t *testing.T) {
user := createUserForTest("get_submission", 1)
problem := createProblemForTest("get_submission", 1, 2)
submission := createSubmissionForTest("get_submission", 1, 2)
submission.User = &user
submission.Problem = &problem
t.Run("testGetSubmission", func(t *testing.T) {
actualSubmission := resource.GetSubmission(&submission)
expectedSubmission := resource.Submission{
ID: 1,
UserID: 1,
User: resource.GetUser(&user),
ProblemID: 1,
ProblemName: "test_get_submission_problem_1",
ProblemSetId: 1,
Language: "test_get_submission_submission_1_language",
Judged: false,
Expand All @@ -138,7 +144,9 @@ func TestGetSubmissionAndGetSubmissionDetail(t *testing.T) {
expectedSubmission := resource.SubmissionDetail{
ID: 1,
UserID: 1,
User: resource.GetUser(&user),
ProblemID: 1,
ProblemName: "test_get_submission_problem_1",
ProblemSetId: 1,
Language: "test_get_submission_submission_1_language",
FileName: "test_get_submission_submission_1_file_name",
Expand Down Expand Up @@ -185,15 +193,25 @@ func TestGetSubmissionAndGetSubmissionDetail(t *testing.T) {
}

func TestGetSubmissionSliceAndGetSubmissionDetailSlice(t *testing.T) {
user1 := createUserForTest("get_submission", 1)
problem1 := createProblemForTest("get_submission", 1, 2)
submission1 := createSubmissionForTest("get_submission", 1, 2)
submission1.User = &user1
submission1.Problem = &problem1
user2 := createUserForTest("get_submission", 2)
problem2 := createProblemForTest("get_submission", 2, 1)
submission2 := createSubmissionForTest("get_submission", 2, 1)
submission2.User = &user2
submission2.Problem = &problem2
t.Run("testGetSubmissionSlice", func(t *testing.T) {
actualSubmissionSlice := resource.GetSubmissionSlice([]models.Submission{submission1, submission2})
expectedSubmissionSlice := []resource.Submission{
{
ID: 1,
UserID: 1,
User: resource.GetUser(&user1),
ProblemID: 1,
ProblemName: "test_get_submission_problem_1",
ProblemSetId: 1,
Language: "test_get_submission_submission_1_language",
Judged: false,
Expand All @@ -203,7 +221,9 @@ func TestGetSubmissionSliceAndGetSubmissionDetailSlice(t *testing.T) {
}, {
ID: 2,
UserID: 2,
User: resource.GetUser(&user2),
ProblemID: 2,
ProblemName: "test_get_submission_problem_2",
ProblemSetId: 2,
Language: "test_get_submission_submission_2_language",
Judged: false,
Expand All @@ -220,7 +240,9 @@ func TestGetSubmissionSliceAndGetSubmissionDetailSlice(t *testing.T) {
{
ID: 1,
UserID: 1,
User: resource.GetUser(&user1),
ProblemID: 1,
ProblemName: "test_get_submission_problem_1",
ProblemSetId: 1,
Language: "test_get_submission_submission_1_language",
FileName: "test_get_submission_submission_1_file_name",
Expand Down Expand Up @@ -264,7 +286,9 @@ func TestGetSubmissionSliceAndGetSubmissionDetailSlice(t *testing.T) {
}, {
ID: 2,
UserID: 2,
User: resource.GetUser(&user2),
ProblemID: 2,
ProblemName: "test_get_submission_problem_2",
ProblemSetId: 2,
Language: "test_get_submission_submission_2_language",
FileName: "test_get_submission_submission_2_file_name",
Expand Down

0 comments on commit 0764cbf

Please sign in to comment.