-
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
Merged
Merged
コメント周りのAPIを実装 #12
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5c23dfc
コメント周りのAPIを実装
cp-20 694d661
テストを書いた
cp-20 238e2d4
テストが落ちていたのを修正
cp-20 dec7e11
fix
cp-20 f441720
fix review
cp-20 db22735
fix review
cp-20 0357839
getAuthorizedUser のリファクタリング
cp-20 4ee4398
とりあえずテスト通した
cp-20 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
ok bool | ||
}{ | ||
{ | ||
name: "正常系", | ||
payload: &CreateCommentPayload{ | ||
ItemID: 1, | ||
UserID: "user1", | ||
Comment: "comment1", | ||
}, | ||
ok: true, | ||
}, | ||
{ | ||
name: "異常系: ItemIDが存在しない", | ||
payload: &CreateCommentPayload{ | ||
UserID: "user1", | ||
Comment: "comment1", | ||
}, | ||
ok: false, | ||
}, | ||
{ | ||
name: "異常系: UserIDが存在しない", | ||
payload: &CreateCommentPayload{ | ||
ItemID: 1, | ||
Comment: "comment1", | ||
}, | ||
ok: false, | ||
}, | ||
{ | ||
name: "異常系: Commentが存在しない", | ||
payload: &CreateCommentPayload{ | ||
ItemID: 1, | ||
UserID: "user1", | ||
}, | ||
ok: false, | ||
}, | ||
} | ||
|
||
assert := assert.New(t) | ||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := CreateComment(tt.payload) | ||
if tt.ok { | ||
assert.NoError(err) | ||
} else { | ||
// assert.Error(err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,52 @@ | ||
package router | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"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 { | ||
itemIDStr := c.Param("id") | ||
itemID, err := strconv.Atoi(itemIDStr) | ||
if err != nil { | ||
return invalidRequest(c, err) | ||
} | ||
|
||
me, err := getAuthorizedUser(c) | ||
if err != nil { | ||
return unauthorizedRequest(c, err) | ||
} | ||
|
||
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(http.StatusCreated, PostCommentResponse{ID: comment.ID}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package router | ||
|
||
import ( | ||
"net/http" | ||
"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(TEST_USER)) | ||
|
||
cases := []struct { | ||
name string | ||
payload string | ||
expected int | ||
}{ | ||
{ | ||
name: "正常系", | ||
payload: `{"text":"テストコメント"}`, | ||
expected: http.StatusCreated, | ||
}, | ||
{ | ||
name: "異常系: 空文字列", | ||
payload: `{"text":""}`, | ||
expected: http.StatusBadRequest, | ||
}, | ||
{ | ||
name: "異常系: パラメータ不足", | ||
payload: `{}`, | ||
expected: http.StatusBadRequest, | ||
}, | ||
} | ||
|
||
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package router | ||
|
||
import ( | ||
"net/http/httptest" | ||
"strings" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
var TEST_USER = "s9" | ||
|
||
func performMutation(e *echo.Echo, method, path, payload string) *httptest.ResponseRecorder { | ||
req := httptest.NewRequest(method, path, strings.NewReader(payload)) | ||
req.Header.Set("Content-Type", "application/json") | ||
rec := httptest.NewRecorder() | ||
e.ServeHTTP(rec, req) | ||
return rec | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
[imo] 0 を弾くのって話にも関連するけど、この場合 0 値として
ItemId=0
がセットされているので、これを弾くのはちょっと気になるかも