Skip to content

Commit

Permalink
feat: Add APIs about getting grades (#133)
Browse files Browse the repository at this point in the history
Signed-off-by: Yuxuan Lu <luyuxuanleo@gmail.com>
  • Loading branch information
leoleoasd committed Jun 25, 2024
2 parents 5c5c951 + d103447 commit cb7218d
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 196 deletions.
36 changes: 32 additions & 4 deletions app/controller/problem_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,19 +485,19 @@ func RefreshGrades(c echo.Context) error {
})
}

func GetGrades(c echo.Context) error {
func GetProblemSetGrades(c echo.Context) error {
problemSet := models.ProblemSet{}
if err := base.DB.Preload("Problems").Preload("Class.Students").Preload("Grades").
First(&problemSet, "id = ? and class_id = ?", c.Param("id"), c.Param("class_id")).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return c.JSON(http.StatusNotFound, response.ErrorResp("NOT_FOUND", nil))
}
panic(errors.Wrap(err, "could not get problem set for getting grades"))
panic(errors.Wrap(err, "could not get problem set for getting problem set grades"))
}
if err := utils.CreateEmptyGrades(&problemSet); err != nil {
panic(errors.Wrap(err, "could not get grades"))
panic(errors.Wrap(err, "could not create empty grades to get problem set grades"))
}
return c.JSON(http.StatusOK, response.GetGradesResponse{
return c.JSON(http.StatusOK, response.GetProblemSetGradesResponse{
Message: "SUCCESS",
Error: nil,
Data: struct {
Expand All @@ -507,3 +507,31 @@ func GetGrades(c echo.Context) error {
},
})
}

func GetClassGrades(c echo.Context) error {
class := models.Class{}
if err := base.DB.Preload("Students").Preload("ProblemSets.Grades").Preload("ProblemSets.Problems").
First(&class, "id = ?", c.Param("id")).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return c.JSON(http.StatusNotFound, response.ErrorResp("NOT_FOUND", nil))
}
panic(errors.Wrap(err, "could not get class for getting class grades"))
}
ret := make([]*resource.ProblemSetWithGrades, 0, len(class.ProblemSets))
for _, problemSet := range class.ProblemSets {
problemSet.Class = &class
if err := utils.CreateEmptyGrades(problemSet); err != nil {
panic(errors.Wrap(err, "could not create empty grades to get class grades"))
}
ret = append(ret, resource.GetProblemSetWithGrades(problemSet))
}
return c.JSON(http.StatusOK, response.GetClassGradesResponse{
Message: "SUCCESS",
Error: nil,
Data: struct {
ProblemSets []*resource.ProblemSetWithGrades `json:"problem_sets"`
}{
ret,
},
})
}
Loading

0 comments on commit cb7218d

Please sign in to comment.