-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
コメント周りのAPIを実装 #12
コメント周りのAPIを実装 #12
Changes from 4 commits
5c23dfc
694d661
238e2d4
dec7e11
f441720
db22735
0357839
4ee4398
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package model | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateComment(t *testing.T) { | ||
PrepareTestDatabase() | ||
|
||
cases := []struct { | ||
name string | ||
payload *CreateCommentPayload | ||
fail bool | ||
cp-20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}{ | ||
{ | ||
name: "正常系", | ||
payload: &CreateCommentPayload{ | ||
ItemID: 1, | ||
UserID: "user1", | ||
Comment: "comment1", | ||
}, | ||
fail: false, | ||
}, | ||
{ | ||
name: "異常系: ItemIDが存在しない", | ||
payload: &CreateCommentPayload{ | ||
UserID: "user1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [imo] 0 を弾くのって話にも関連するけど、この場合 0 値として |
||
Comment: "comment1", | ||
}, | ||
fail: true, | ||
}, | ||
{ | ||
name: "異常系: UserIDが存在しない", | ||
payload: &CreateCommentPayload{ | ||
ItemID: 1, | ||
Comment: "comment1", | ||
}, | ||
fail: true, | ||
}, | ||
{ | ||
name: "異常系: Commentが存在しない", | ||
payload: &CreateCommentPayload{ | ||
ItemID: 1, | ||
UserID: "user1", | ||
}, | ||
fail: true, | ||
}, | ||
} | ||
|
||
assert := assert.New(t) | ||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := CreateComment(tt.payload) | ||
if tt.fail { | ||
assert.Error(err) | ||
} else { | ||
assert.NoError(err) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,48 @@ | ||
package router | ||
|
||
import ( | ||
"net/http" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/traPtitech/booQ-v3/model" | ||
) | ||
|
||
// PostComments POST /items/:id/comments | ||
func PostComments(c echo.Context) error { | ||
return echo.NewHTTPError(http.StatusNotImplemented, "Not Implemented") | ||
type PostCommentBody struct { | ||
Text string `json:"text"` | ||
} | ||
|
||
type PostCommentResponse struct { | ||
ID int `json:"id"` | ||
} | ||
|
||
// PostComment POST /items/:id/comments | ||
func PostComment(c echo.Context) error { | ||
itemIDRaw := c.Param("id") | ||
cp-20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
itemID, err := strconv.Atoi(itemIDRaw) | ||
if err != nil { | ||
return invalidRequest(c, err) | ||
} | ||
|
||
me := getAuthorizedUser(c) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [q] これって必ず失敗しない関数なの?(この PR とは無関係の話なので一旦無視しちゃって良いけど) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 内部で呼んでるのは There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :naruhodo: ちょっとコード読んだんだけど、 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :naruhodo: ちょっとコード読んだんだけど、AuthMiddleware を通さない状態でこの関数を呼び出すと panic するので気を付けて!外部からは一切のエンドポイントを叩けなくても ok という方針ならこの実装でも良いっちゃよいです There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. panic するんですね :hae_zubora: |
||
|
||
var body PostCommentBody | ||
if err := c.Bind(&body); err != nil { | ||
return invalidRequest(c, err) | ||
} | ||
if body.Text == "" { | ||
return invalidRequest(c, fmt.Errorf("text is empty")) | ||
} | ||
|
||
payload := model.CreateCommentPayload{ | ||
ItemID: itemID, | ||
UserID: me, | ||
Comment: body.Text, | ||
} | ||
comment, err := model.CreateComment(&payload) | ||
if err != nil { | ||
return internalServerError(c, err) | ||
} | ||
|
||
return c.JSON(201, PostCommentResponse{ID: comment.ID}) | ||
cp-20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package router | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/traPtitech/booQ-v3/model" | ||
) | ||
|
||
func TestPostComment(t *testing.T) { | ||
model.PrepareTestDatabase() | ||
|
||
e := echo.New() | ||
SetupRouting(e, CreateUserProvider("s9")) | ||
cp-20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
cases := []struct { | ||
name string | ||
payload string | ||
expected int | ||
}{ | ||
{ | ||
name: "正常系", | ||
payload: `{"text":"テストコメント"}`, | ||
expected: 201, | ||
cp-20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
{ | ||
name: "異常系: 空文字列", | ||
payload: `{"text":""}`, | ||
expected: 400, | ||
}, | ||
{ | ||
name: "異常系: パラメータ不足", | ||
payload: `{}`, | ||
expected: 400, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
rec := PerformMutation(e, "POST", "/api/items/1/comments", tc.payload) | ||
assert.Equal(tc.expected, rec.Code) | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package router | ||
|
||
import ( | ||
"net/http/httptest" | ||
"strings" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func PerformMutation(e *echo.Echo, method, path, payload string) *httptest.ResponseRecorder { | ||
cp-20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
req := httptest.NewRequest(method, path, strings.NewReader(payload)) | ||
req.Header.Set("Content-Type", "application/json") | ||
rec := httptest.NewRecorder() | ||
e.ServeHTTP(rec, req) | ||
return rec | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[q] これ、
ItemID=0
のものは弾くということであってる?[q] Invalid な
ItemID
が来たときは弾かない?弾いたほうが良さそうに見えるけど