Skip to content

Commit f441720

Browse files
committed
fix review
1 parent dec7e11 commit f441720

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

model/comments.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package model
22

3-
import "fmt"
3+
import "errors"
44

55
type Comment struct {
66
GormModel
@@ -21,21 +21,21 @@ type CreateCommentPayload struct {
2121

2222
func CreateComment(p *CreateCommentPayload) (*Comment, error) {
2323
if p.ItemID == 0 {
24-
return nil, fmt.Errorf("ItemID is required")
24+
return nil, errors.New("ItemID is required")
2525
}
2626
if p.UserID == "" {
27-
return nil, fmt.Errorf("UserID is required")
27+
return nil, errors.New("UserID is required")
2828
}
2929
if p.Comment == "" {
30-
return nil, fmt.Errorf("Comment is required")
30+
return nil, errors.New("Comment is required")
3131
}
32-
c := &Comment{
32+
c := Comment{
3333
ItemID: p.ItemID,
3434
UserID: p.UserID,
3535
Comment: p.Comment,
3636
}
37-
if err := db.Create(c).Error; err != nil {
37+
if err := db.Create(&c).Error; err != nil {
3838
return nil, err
3939
}
40-
return c, nil
40+
return &c, nil
4141
}

model/comments_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestCreateComment(t *testing.T) {
1212
cases := []struct {
1313
name string
1414
payload *CreateCommentPayload
15-
fail bool
15+
ok bool
1616
}{
1717
{
1818
name: "正常系",
@@ -21,39 +21,39 @@ func TestCreateComment(t *testing.T) {
2121
UserID: "user1",
2222
Comment: "comment1",
2323
},
24-
fail: false,
24+
ok: true,
2525
},
2626
{
2727
name: "異常系: ItemIDが存在しない",
2828
payload: &CreateCommentPayload{
2929
UserID: "user1",
3030
Comment: "comment1",
3131
},
32-
fail: true,
32+
ok: false,
3333
},
3434
{
3535
name: "異常系: UserIDが存在しない",
3636
payload: &CreateCommentPayload{
3737
ItemID: 1,
3838
Comment: "comment1",
3939
},
40-
fail: true,
40+
ok: false,
4141
},
4242
{
4343
name: "異常系: Commentが存在しない",
4444
payload: &CreateCommentPayload{
4545
ItemID: 1,
4646
UserID: "user1",
4747
},
48-
fail: true,
48+
ok: false,
4949
},
5050
}
5151

5252
assert := assert.New(t)
5353
for _, tt := range cases {
5454
t.Run(tt.name, func(t *testing.T) {
5555
_, err := CreateComment(tt.payload)
56-
if tt.fail {
56+
if tt.ok {
5757
assert.Error(err)
5858
} else {
5959
assert.NoError(err)

router/comments.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package router
22

33
import (
44
"fmt"
5+
"net/http"
56
"strconv"
67

78
"github.com/labstack/echo/v4"
@@ -18,8 +19,8 @@ type PostCommentResponse struct {
1819

1920
// PostComment POST /items/:id/comments
2021
func PostComment(c echo.Context) error {
21-
itemIDRaw := c.Param("id")
22-
itemID, err := strconv.Atoi(itemIDRaw)
22+
itemIDStr := c.Param("id")
23+
itemID, err := strconv.Atoi(itemIDStr)
2324
if err != nil {
2425
return invalidRequest(c, err)
2526
}
@@ -44,5 +45,5 @@ func PostComment(c echo.Context) error {
4445
return internalServerError(c, err)
4546
}
4647

47-
return c.JSON(201, PostCommentResponse{ID: comment.ID})
48+
return c.JSON(http.StatusCreated, PostCommentResponse{ID: comment.ID})
4849
}

router/comments_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package router
22

33
import (
4+
"net/http"
45
"testing"
56

67
"github.com/labstack/echo/v4"
@@ -12,7 +13,7 @@ func TestPostComment(t *testing.T) {
1213
model.PrepareTestDatabase()
1314

1415
e := echo.New()
15-
SetupRouting(e, CreateUserProvider("s9"))
16+
SetupRouting(e, CreateUserProvider(TEST_USER))
1617

1718
cases := []struct {
1819
name string
@@ -22,24 +23,24 @@ func TestPostComment(t *testing.T) {
2223
{
2324
name: "正常系",
2425
payload: `{"text":"テストコメント"}`,
25-
expected: 201,
26+
expected: http.StatusCreated,
2627
},
2728
{
2829
name: "異常系: 空文字列",
2930
payload: `{"text":""}`,
30-
expected: 400,
31+
expected: http.StatusBadRequest,
3132
},
3233
{
3334
name: "異常系: パラメータ不足",
3435
payload: `{}`,
35-
expected: 400,
36+
expected: http.StatusBadRequest,
3637
},
3738
}
3839

3940
for _, tc := range cases {
4041
t.Run(tc.name, func(t *testing.T) {
4142
assert := assert.New(t)
42-
rec := PerformMutation(e, "POST", "/api/items/1/comments", tc.payload)
43+
rec := performMutation(e, "POST", "/api/items/1/comments", tc.payload)
4344
assert.Equal(tc.expected, rec.Code)
4445
})
4546
}

router/test_common.go renamed to router/util_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"github.com/labstack/echo/v4"
88
)
99

10-
func PerformMutation(e *echo.Echo, method, path, payload string) *httptest.ResponseRecorder {
10+
var TEST_USER = "s9"
11+
12+
func performMutation(e *echo.Echo, method, path, payload string) *httptest.ResponseRecorder {
1113
req := httptest.NewRequest(method, path, strings.NewReader(payload))
1214
req.Header.Set("Content-Type", "application/json")
1315
rec := httptest.NewRecorder()

0 commit comments

Comments
 (0)