Skip to content

Commit

Permalink
Merge pull request #722 from isucon/fix/webapp-rm-grade-tx
Browse files Browse the repository at this point in the history
成績取得のトランザクションを消す
  • Loading branch information
FujishigeTemma authored Sep 14, 2021
2 parents 5ea31fe + a5e10ba commit fc88152
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions webapp/golang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,20 +559,13 @@ func (h *handlers) GetGrades(c echo.Context) error {
return c.NoContent(http.StatusInternalServerError)
}

tx, err := h.DB.Beginx()
if err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
defer tx.Rollback()

// 履修している科目一覧取得
var registeredCourses []Course
query := "SELECT `courses`.*" +
" FROM `registrations`" +
" JOIN `courses` ON `registrations`.`course_id` = `courses`.`id`" +
" WHERE `user_id` = ?"
if err := tx.Select(&registeredCourses, query, userID); err != nil {
if err := h.DB.Select(&registeredCourses, query, userID); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
Expand All @@ -588,7 +581,7 @@ func (h *handlers) GetGrades(c echo.Context) error {
" FROM `classes`" +
" WHERE `course_id` = ?" +
" ORDER BY `part` DESC"
if err := tx.Select(&classes, query, course.ID); err != nil {
if err := h.DB.Select(&classes, query, course.ID); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
Expand All @@ -598,13 +591,13 @@ func (h *handlers) GetGrades(c echo.Context) error {
var myTotalScore int
for _, class := range classes {
var submissionsCount int
if err := tx.Get(&submissionsCount, "SELECT COUNT(*) FROM `submissions` WHERE `class_id` = ?", class.ID); err != nil {
if err := h.DB.Get(&submissionsCount, "SELECT COUNT(*) FROM `submissions` WHERE `class_id` = ?", class.ID); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}

var myScore sql.NullInt64
if err := tx.Get(&myScore, "SELECT `submissions`.`score` FROM `submissions` WHERE `user_id` = ? AND `class_id` = ?", userID, class.ID); err != nil && err != sql.ErrNoRows {
if err := h.DB.Get(&myScore, "SELECT `submissions`.`score` FROM `submissions` WHERE `user_id` = ? AND `class_id` = ?", userID, class.ID); err != nil && err != sql.ErrNoRows {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
} else if err == sql.ErrNoRows || !myScore.Valid {
Expand Down Expand Up @@ -638,7 +631,7 @@ func (h *handlers) GetGrades(c echo.Context) error {
" LEFT JOIN `submissions` ON `users`.`id` = `submissions`.`user_id` AND `submissions`.`class_id` = `classes`.`id`" +
" WHERE `courses`.`id` = ?" +
" GROUP BY `users`.`id`"
if err := tx.Select(&totals, query, course.ID); err != nil {
if err := h.DB.Select(&totals, query, course.ID); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
Expand Down Expand Up @@ -682,12 +675,7 @@ func (h *handlers) GetGrades(c echo.Context) error {
" LEFT JOIN `submissions` ON `users`.`id` = `submissions`.`user_id` AND `submissions`.`class_id` = `classes`.`id`" +
" WHERE `users`.`type` = ?" +
" GROUP BY `users`.`id`"
if err := tx.Select(&gpas, query, StatusClosed, StatusClosed, Student); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}

if err := tx.Commit(); err != nil {
if err := h.DB.Select(&gpas, query, StatusClosed, StatusClosed, Student); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
Expand Down

0 comments on commit fc88152

Please sign in to comment.