Skip to content

Commit

Permalink
:refactor: Use Context::Set
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono committed Jul 4, 2023
1 parent e72a26d commit 4eb78d0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
4 changes: 2 additions & 2 deletions router/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func GetItems(c echo.Context) error {
// PostItems POST /items
func PostItems(c echo.Context) error {
user := c.Get("user").(model.User)
item := model.Item{}
if err := BindAndValidate(c, &item); err != nil {
item := c.Get("item").(model.Item)
if err := c.Validate(&item); err != nil {
return c.JSON(http.StatusBadRequest, err)
}
res, err := model.CreateItem(item)
Expand Down
13 changes: 3 additions & 10 deletions router/middleware.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package router

import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strconv"

Expand Down Expand Up @@ -43,15 +40,11 @@ func MiddlewareAdmin(next echo.HandlerFunc) echo.HandlerFunc {
// MiddlewareBodyItemSocial リクエストボディから取得したItemがPersonalItemでない場合はAdmin以外を弾くmiddleware
func MiddlewareBodyItemSocial(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
body, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
return next(c)
}
c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(body))
item := model.Item{}
if err = json.Unmarshal(body, &item); err != nil {
return next(c)
if err := c.Bind(&item); err != nil {
return err
}
c.Set("item", item)
user := c.Get("user").(model.User)
if item.Type != model.PersonalItem && !user.Admin {
return c.NoContent(http.StatusForbidden)
Expand Down

0 comments on commit 4eb78d0

Please sign in to comment.