Skip to content

Commit

Permalink
Merge pull request #4 from fujiwara/bulk-insert-unread-announcements
Browse files Browse the repository at this point in the history
unread announcementsをinsert selectでやる
  • Loading branch information
mackee committed Sep 18, 2021
2 parents 6bc0f4e + 6bd974f commit c7b6805
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions webapp/go/main.go
Expand Up @@ -1457,22 +1457,14 @@ func (h *handlers) AddAnnouncement(c echo.Context) error {
return c.NoContent(http.StatusInternalServerError)
}

var targets []User
query := "SELECT `users`.* FROM `users`" +
" JOIN `registrations` ON `users`.`id` = `registrations`.`user_id`" +
query := "INSERT INTO `unread_announcements` (`announcement_id`, `user_id`)" +
" SELECT ?, `registrations`.`user_id` FROM `registrations`" +
" WHERE `registrations`.`course_id` = ?"
if err := tx.Select(&targets, query, req.CourseID); err != nil {
if _, err := tx.Exec(query, req.ID, req.CourseID); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}

for _, user := range targets {
if _, err := tx.Exec("INSERT INTO `unread_announcements` (`announcement_id`, `user_id`) VALUES (?, ?)", req.ID, user.ID); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
}

if err := tx.Commit(); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
Expand Down

0 comments on commit c7b6805

Please sign in to comment.