Skip to content

Commit

Permalink
Merge pull request #10 from traPtitech/router-refactor
Browse files Browse the repository at this point in the history
router をリファクタリング
  • Loading branch information
cp-20 authored Dec 18, 2024
2 parents f6feff0 + ec72f12 commit c6f241f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 38 deletions.
17 changes: 17 additions & 0 deletions router/common.go
Original file line number Diff line number Diff line change
@@ -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, "予期せぬエラーが発生しました")
}
61 changes: 25 additions & 36 deletions router/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion router/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
1 change: 0 additions & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,5 @@ func SetupRouting(e *echo.Echo, client *UserProvider) {
apiFiles.POST("", PostFile, middleware.BodyLimit("3MB"))
apiFiles.GET("/:id", GetFile)
}

}
}

0 comments on commit c6f241f

Please sign in to comment.