From 1bd04d23a02e528f3ee627fbf66b7f1ccf04b4cd Mon Sep 17 00:00:00 2001 From: motoki317 Date: Mon, 6 Sep 2021 15:47:13 +0900 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20check=20course=20existence?= =?UTF-8?q?=20by=20select=20first?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webapp/golang/main.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/webapp/golang/main.go b/webapp/golang/main.go index f39977474..8418a6af3 100644 --- a/webapp/golang/main.go +++ b/webapp/golang/main.go @@ -910,16 +910,18 @@ func (h *handlers) SetCourseStatus(c echo.Context) error { return echo.NewHTTPError(http.StatusBadRequest, "Invalid format.") } - result, err := h.DB.Exec("UPDATE `courses` SET `status` = ? WHERE `id` = ?", req.Status, courseID) - if err != nil { + var count int + if err := h.DB.Get(&count, "SELECT COUNT(*) FROM `courses` WHERE `id` = ?", courseID); err != nil { c.Logger().Error(err) return c.NoContent(http.StatusInternalServerError) } - if num, err := result.RowsAffected(); err != nil { + if count == 0 { + return echo.NewHTTPError(http.StatusNotFound, "No such course.") + } + + if _, err := h.DB.Exec("UPDATE `courses` SET `status` = ? WHERE `id` = ?", req.Status, courseID); err != nil { c.Logger().Error(err) return c.NoContent(http.StatusInternalServerError) - } else if num == 0 { - return echo.NewHTTPError(http.StatusNotFound, "No such course.") } return c.NoContent(http.StatusOK)