From ec72f12d10de3d82a7201c283e7118a55a77b843 Mon Sep 17 00:00:00 2001 From: cp-20 Date: Wed, 18 Dec 2024 18:55:36 +0900 Subject: [PATCH] =?UTF-8?q?router=20=E3=82=92=E3=83=AA=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=AF=E3=82=BF=E3=83=AA=E3=83=B3=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router/common.go | 17 ++++++++++++ router/items.go | 61 ++++++++++++++++++-------------------------- router/middleware.go | 8 +++++- router/router.go | 1 - 4 files changed, 49 insertions(+), 38 deletions(-) create mode 100644 router/common.go diff --git a/router/common.go b/router/common.go new file mode 100644 index 0000000..af73ac1 --- /dev/null +++ b/router/common.go @@ -0,0 +1,17 @@ +package router + +import ( + "net/http" + + "github.com/labstack/echo/v4" +) + +func invalidRequest(c echo.Context, err error) error { + c.Logger().Infof("invalid request on %s: %w", c.Path(), err.Error()) + return c.String(http.StatusBadRequest, "リクエストデータの処理に失敗しました") +} + +func internalServerError(c echo.Context, err error) error { + c.Logger().Infof("internal server error on %s: %w", c.Path(), err.Error()) + return c.String(http.StatusInternalServerError, "予期せぬエラーが発生しました") +} diff --git a/router/items.go b/router/items.go index 65e744d..f91a179 100644 --- a/router/items.go +++ b/router/items.go @@ -10,43 +10,41 @@ import ( // GetItems GET /items func GetItems(c echo.Context) error { - getItemsBody, err := getItemsParams(c) + getItemsBody, err := parseGetItemsParams(c) if err != nil { - c.Logger().Info(err.Error()) - return c.JSON(http.StatusBadRequest, "リクエストデータの処理に失敗しました") + return invalidRequest(c, err) } res, err := model.GetItems(getItemsBody) if err != nil { - c.Logger().Info(err.Error()) - return c.JSON(http.StatusInternalServerError, "DBの操作に失敗しました") + return internalServerError(c, err) } return c.JSON(http.StatusOK, res) } -func getItemsParams(c echo.Context) (model.GetItemsBody, error) { - params := c.QueryParams() +func parseGetItemsParams(c echo.Context) (model.GetItemsBody, error) { + query := c.QueryParams() getItemsBody := model.GetItemsBody{ - UserID: params.Get("userId"), - Search: params.Get("search"), - Rental: params.Get("rental"), - Tags: params["tag"], - TagsExclude: params["tag-exclude"], - SortBy: params.Get("sortby"), + UserID: query.Get("userId"), + Search: query.Get("search"), + Rental: query.Get("rental"), + Tags: query["tag"], + TagsExclude: query["tag-exclude"], + SortBy: query.Get("sortby"), } - if params.Get("limit") != "" { - limit, err := strconv.Atoi(params.Get("limit")) + if query.Get("limit") != "" { + limit, err := strconv.Atoi(query.Get("limit")) if err != nil { return model.GetItemsBody{}, err } getItemsBody.Limit = limit } - if params.Get("offset") != "" { - offset, err := strconv.Atoi(params.Get("offset")) + if query.Get("offset") != "" { + offset, err := strconv.Atoi(query.Get("offset")) if err != nil { return model.GetItemsBody{}, err } @@ -60,18 +58,16 @@ func getItemsParams(c echo.Context) (model.GetItemsBody, error) { // PostItems POST /items func PostItems(c echo.Context) error { - me := c.Get("user").(string) + me := getAuthorizedUser(c) items := []model.RequestPostItemsBody{} err := c.Bind(&items) if err != nil { - c.Logger().Info(err.Error()) - return c.JSON(http.StatusBadRequest, "リクエストデータの処理に失敗しました") + return invalidRequest(c, err) } res, err := model.CreateItems(items, me) if err != nil { - c.Logger().Info(err.Error()) - return c.JSON(http.StatusBadRequest, "DBの操作に失敗しました") + return internalServerError(c, err) } return c.JSON(http.StatusOK, res) @@ -83,14 +79,12 @@ func GetItem(c echo.Context) error { itemID, err := strconv.Atoi(itemIDRaw) if err != nil { - c.Logger().Info(err.Error()) - return c.JSON(http.StatusBadRequest, "物品のIDが不正です") + return invalidRequest(c, err) } res, err := model.GetItem(itemID) if err != nil { - c.Logger().Info(err.Error()) - return c.JSON(http.StatusInternalServerError, "DBの操作に失敗しました") + return internalServerError(c, err) } return c.JSON(http.StatusOK, res) @@ -101,21 +95,18 @@ func PatchItem(c echo.Context) error { itemBody := model.RequestPostItemsBody{} err := c.Bind(&itemBody) if err != nil { - c.Logger().Info(err.Error()) - return c.JSON(http.StatusBadRequest, "リクエストデータの処理に失敗しました") + return invalidRequest(c, err) } itemIDRaw := c.Param("id") itemID, err := strconv.Atoi(itemIDRaw) if err != nil { - c.Logger().Info(err.Error()) - return c.JSON(http.StatusBadRequest, "物品のIDが不正です") + return invalidRequest(c, err) } res, err := model.PatchItem(itemID, itemBody) if err != nil { - c.Logger().Info(err.Error()) - return c.JSON(http.StatusInternalServerError, "DBの操作に失敗しました") + return internalServerError(c, err) } return c.JSON(http.StatusOK, res) @@ -126,14 +117,12 @@ func DeleteItem(c echo.Context) error { itemIDRaw := c.Param("id") itemID, err := strconv.Atoi(itemIDRaw) if err != nil { - c.Logger().Info(err.Error()) - return c.JSON(http.StatusBadRequest, "物品のIDが不正です") + return invalidRequest(c, err) } err = model.DeleteItem(itemID) if err != nil { - c.Logger().Info(err.Error()) - return c.JSON(http.StatusInternalServerError, "DBの操作に失敗しました") + return internalServerError(c, err) } return c.NoContent(http.StatusOK) diff --git a/router/middleware.go b/router/middleware.go index 9410edf..a6bf2ea 100644 --- a/router/middleware.go +++ b/router/middleware.go @@ -8,6 +8,8 @@ import ( "github.com/labstack/echo/v4" ) +var userProviderKey = "user" + // UserProvider traQに接続する用のclient type UserProvider struct { AuthUser func(c echo.Context) (echo.Context, error) @@ -34,7 +36,11 @@ func CreateUserProvider(debugUserName string) *UserProvider { return c, errors.New("認証に失敗しました(Headerに必要な情報が存在しません)") } } - c.Set("user", res) + c.Set(userProviderKey, res) return c, nil }} } + +func getAuthorizedUser(c echo.Context) string { + return c.Get(userProviderKey).(string) +} diff --git a/router/router.go b/router/router.go index 67fb7c5..de87aa7 100644 --- a/router/router.go +++ b/router/router.go @@ -51,6 +51,5 @@ func SetupRouting(e *echo.Echo, client *UserProvider) { apiFiles.POST("", PostFile, middleware.BodyLimit("3MB")) apiFiles.GET("/:id", GetFile) } - } }