diff --git a/.golangci.yml b/.golangci.yml index 1e466e7b..935e6ffe 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -10,3 +10,37 @@ linters: - unused # https://golangci-lint.run/usage/linters/#disabled-by-default - gofmt + - asasalint + - asciicheck + - contextcheck + - dogsled + - durationcheck + - errchkjson + - errname + - errorlint + - exhaustive + - fatcontext + - forbidigo + - forcetypeassert + - gocheckcompilerdirectives + - importas + - inamedparam + - intrange + - ireturn + - lll + - loggercheck + - makezero + - mirror + - musttag + - nilnil + - predeclared + - reassign + - thelper + - unconvert + - usestdlibvars + - wastedassign + - whitespace +linters-settings: + lll: + line-length: 100 + tab-width: 4 diff --git a/Dockerfile b/Dockerfile index 02c9b3e6..1fdd5bfb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ ## build backend -FROM golang:1.22.4-alpine as server-build +FROM golang:1.22.5-alpine as server-build WORKDIR /github.com/traPtitech/Jomon COPY go.mod go.sum ./ @@ -11,7 +11,7 @@ RUN go build -o /Jomon -ldflags "-s -w" ## run -FROM alpine:3.20.1 +FROM alpine:3.20.2 ENV TZ Asia/Tokyo RUN apk --update --no-cache add tzdata \ diff --git a/Dockerfile.dev b/Dockerfile.dev index 63c1b4bf..619137b9 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -1,4 +1,4 @@ -FROM golang:1.22.4-alpine as build +FROM golang:1.22.5-alpine as build ENV CGO_ENABLED 0 ENV TZ Asia/Tokyo diff --git a/model/admin_impl_test.go b/model/admin_impl_test.go index 29e51b9c..57a962c0 100644 --- a/model/admin_impl_test.go +++ b/model/admin_impl_test.go @@ -21,9 +21,17 @@ func TestEntRepository_GetAdmins(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() - user1, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user1, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - user2, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user2, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) got, err := repo.GetAdmins(ctx) @@ -54,7 +62,11 @@ func TestEntRepository_AddAdmins(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), false) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + false) require.NoError(t, err) err = repo.AddAdmins(ctx, []uuid.UUID{user.ID}) @@ -74,7 +86,11 @@ func TestEntRepository_DeleteAdmins(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) err = repo.DeleteAdmins(ctx, []uuid.UUID{user.ID}) diff --git a/model/comment.go b/model/comment.go index fabda3ba..1e329a1e 100644 --- a/model/comment.go +++ b/model/comment.go @@ -18,7 +18,11 @@ type Comment struct { type CommentRepository interface { GetComments(ctx context.Context, requestID uuid.UUID) ([]*Comment, error) - CreateComment(ctx context.Context, comment string, requestID uuid.UUID, userID uuid.UUID) (*Comment, error) - UpdateComment(ctx context.Context, comment string, requestID uuid.UUID, commentID uuid.UUID) (*Comment, error) + CreateComment( + ctx context.Context, comment string, requestID uuid.UUID, userID uuid.UUID, + ) (*Comment, error) + UpdateComment( + ctx context.Context, comment string, requestID uuid.UUID, commentID uuid.UUID, + ) (*Comment, error) DeleteComment(ctx context.Context, requestID uuid.UUID, commentID uuid.UUID) error } diff --git a/model/comment_impl.go b/model/comment_impl.go index f23dfe5f..e63d545d 100644 --- a/model/comment_impl.go +++ b/model/comment_impl.go @@ -11,7 +11,9 @@ import ( "github.com/traPtitech/Jomon/ent/request" ) -func (repo *EntRepository) GetComments(ctx context.Context, requestID uuid.UUID) ([]*Comment, error) { +func (repo *EntRepository) GetComments( + ctx context.Context, requestID uuid.UUID, +) ([]*Comment, error) { _, err := repo.client.Request. Query(). Where(request.IDEQ(requestID)). @@ -38,7 +40,9 @@ func (repo *EntRepository) GetComments(ctx context.Context, requestID uuid.UUID) return modelcomments, nil } -func (repo *EntRepository) CreateComment(ctx context.Context, comment string, requestID uuid.UUID, userID uuid.UUID) (*Comment, error) { +func (repo *EntRepository) CreateComment( + ctx context.Context, comment string, requestID uuid.UUID, userID uuid.UUID, +) (*Comment, error) { created, err := repo.client.Comment. Create(). SetComment(comment). @@ -51,7 +55,9 @@ func (repo *EntRepository) CreateComment(ctx context.Context, comment string, re return ConvertEntCommentToModelComment(created, userID), nil } -func (repo *EntRepository) UpdateComment(ctx context.Context, commentContent string, requestID uuid.UUID, commentID uuid.UUID) (*Comment, error) { +func (repo *EntRepository) UpdateComment( + ctx context.Context, commentContent string, requestID uuid.UUID, commentID uuid.UUID, +) (*Comment, error) { updated, err := repo.client.Comment. UpdateOneID(commentID). SetComment(commentContent). @@ -72,7 +78,9 @@ func (repo *EntRepository) UpdateComment(ctx context.Context, commentContent str return ConvertEntCommentToModelComment(updated, updatedWithUser.Edges.User.ID), nil } -func (repo *EntRepository) DeleteComment(ctx context.Context, requestID uuid.UUID, commentID uuid.UUID) error { +func (repo *EntRepository) DeleteComment( + ctx context.Context, requestID uuid.UUID, commentID uuid.UUID, +) error { c, err := repo.client.Comment. Query(). Where( diff --git a/model/comment_impl_test.go b/model/comment_impl_test.go index 6f5d3983..dd7b8af5 100644 --- a/model/comment_impl_test.go +++ b/model/comment_impl_test.go @@ -21,9 +21,18 @@ func TestEntRepository_GetComments(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{}, []*RequestTarget{}, nil, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{}, []*RequestTarget{}, + nil, user.ID) require.NoError(t, err) comment1, err := repo.CreateComment(ctx, random.AlphaNumeric(t, 30), request.ID, user.ID) @@ -52,9 +61,18 @@ func TestEntRepository_GetComments(t *testing.T) { t.Run("Success2", func(t *testing.T) { t.Parallel() - user, err := repo2.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo2.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - request, err := repo2.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{}, []*RequestTarget{}, nil, user.ID) + request, err := repo2.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{}, []*RequestTarget{}, + nil, user.ID) require.NoError(t, err) got, err := repo2.GetComments(ctx, request.ID) @@ -77,13 +95,26 @@ func TestEntRepository_CreateComment(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{}, []*RequestTarget{}, nil, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{}, []*RequestTarget{}, + nil, user.ID) require.NoError(t, err) comment := random.AlphaNumeric(t, 30) - user2, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user2, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) created, err := repo.CreateComment(ctx, comment, request.ID, user2.ID) assert.NoError(t, err) @@ -93,7 +124,11 @@ func TestEntRepository_CreateComment(t *testing.T) { t.Run("UnknownRequest", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) _, err = repo.CreateComment(ctx, random.AlphaNumeric(t, 30), uuid.New(), user.ID) assert.Error(t, err) @@ -101,9 +136,18 @@ func TestEntRepository_CreateComment(t *testing.T) { t.Run("UnknownUser", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{}, []*RequestTarget{}, nil, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{}, []*RequestTarget{}, + nil, user.ID) require.NoError(t, err) _, err = repo.CreateComment(ctx, random.AlphaNumeric(t, 30), request.ID, uuid.New()) @@ -119,9 +163,18 @@ func TestEntREpository_UpdateComment(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{}, []*RequestTarget{}, nil, user.ID) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{}, []*RequestTarget{}, + nil, user.ID) require.NoError(t, err) created, err := repo.CreateComment(ctx, random.AlphaNumeric(t, 30), request.ID, user.ID) require.NoError(t, err) @@ -136,14 +189,28 @@ func TestEntREpository_UpdateComment(t *testing.T) { t.Run("Success2", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - request1, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{}, []*RequestTarget{}, nil, user.ID) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + request1, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{}, []*RequestTarget{}, + nil, user.ID) require.NoError(t, err) comment, err := repo.CreateComment(ctx, random.AlphaNumeric(t, 30), request1.ID, user.ID) require.NoError(t, err) - request2, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{}, []*RequestTarget{}, nil, user.ID) + request2, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{}, []*RequestTarget{}, + nil, user.ID) require.NoError(t, err) updated, err := repo.UpdateComment(ctx, comment.Comment, request2.ID, comment.ID) assert.NoError(t, err) @@ -160,20 +227,41 @@ func TestEntREpository_UpdateComment(t *testing.T) { t.Run("UnknownComment", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{}, []*RequestTarget{}, nil, user.ID) - require.NoError(t, err) - - _, err = repo.UpdateComment(ctx, random.AlphaNumeric(t, 30), request.ID, uuid.New()) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{}, []*RequestTarget{}, + nil, user.ID) + require.NoError(t, err) + + _, err = repo.UpdateComment( + ctx, + random.AlphaNumeric(t, 30), + request.ID, uuid.New()) assert.Error(t, err) }) t.Run("UnknownRequest", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{}, []*RequestTarget{}, nil, user.ID) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{}, []*RequestTarget{}, + nil, user.ID) require.NoError(t, err) comment, err := repo.CreateComment(ctx, random.AlphaNumeric(t, 30), request.ID, user.ID) require.NoError(t, err) @@ -191,9 +279,18 @@ func TestEntRepository_DeleteComment(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{}, []*RequestTarget{}, nil, user.ID) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{}, []*RequestTarget{}, + nil, user.ID) require.NoError(t, err) comment, err := repo.CreateComment(ctx, random.AlphaNumeric(t, 30), request.ID, user.ID) require.NoError(t, err) @@ -204,9 +301,18 @@ func TestEntRepository_DeleteComment(t *testing.T) { t.Run("UnknownRequest", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{}, []*RequestTarget{}, nil, user.ID) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{}, []*RequestTarget{}, + nil, user.ID) require.NoError(t, err) comment, err := repo.CreateComment(ctx, random.AlphaNumeric(t, 30), request.ID, user.ID) require.NoError(t, err) @@ -217,9 +323,18 @@ func TestEntRepository_DeleteComment(t *testing.T) { t.Run("UnknownComment", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{}, []*RequestTarget{}, nil, user.ID) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{}, []*RequestTarget{}, + nil, user.ID) require.NoError(t, err) err = repo.DeleteComment(ctx, request.ID, uuid.New()) diff --git a/model/file.go b/model/file.go index 3a09c87c..aded2e40 100644 --- a/model/file.go +++ b/model/file.go @@ -9,7 +9,9 @@ import ( ) type FileRepository interface { - CreateFile(ctx context.Context, name string, mimetype string, requestID uuid.UUID, userID uuid.UUID) (*File, error) + CreateFile( + ctx context.Context, name string, mimetype string, requestID uuid.UUID, userID uuid.UUID, + ) (*File, error) GetFile(ctx context.Context, fileID uuid.UUID) (*File, error) DeleteFile(ctx context.Context, fileID uuid.UUID) error } diff --git a/model/file_impl.go b/model/file_impl.go index fc8c0eed..40499297 100644 --- a/model/file_impl.go +++ b/model/file_impl.go @@ -8,7 +8,9 @@ import ( "github.com/traPtitech/Jomon/ent/file" ) -func (repo *EntRepository) CreateFile(ctx context.Context, name string, mimetype string, requestID uuid.UUID, userID uuid.UUID) (*File, error) { +func (repo *EntRepository) CreateFile( + ctx context.Context, name string, mimetype string, requestID uuid.UUID, userID uuid.UUID, +) (*File, error) { id := uuid.New() created, err := repo.client.File. diff --git a/model/file_impl_test.go b/model/file_impl_test.go index d6ff083a..0fd3178f 100644 --- a/model/file_impl_test.go +++ b/model/file_impl_test.go @@ -23,9 +23,18 @@ func TestEntRepository_CreateFile(t *testing.T) { var tags []*Tag var targets []*RequestTarget var group *Group - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 50), tags, targets, group, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 50), + tags, targets, + group, user.ID) require.NoError(t, err) mimetype := "image/png" @@ -42,7 +51,11 @@ func TestEntRepository_CreateFile(t *testing.T) { t.Parallel() ctx := context.Background() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) request := Request{ ID: uuid.New(), @@ -63,9 +76,18 @@ func TestEntRepository_CreateFile(t *testing.T) { var tags []*Tag var targets []*RequestTarget var group *Group - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 50), tags, targets, group, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 50), + tags, targets, + group, user.ID) assert.NoError(t, err) mimetype := "image/png" @@ -88,9 +110,18 @@ func TestEntRepository_GetFile(t *testing.T) { var tags []*Tag var targets []*RequestTarget var group *Group - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 50), tags, targets, group, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 50), + tags, targets, + group, user.ID) assert.NoError(t, err) mimetype := "image/png" @@ -128,9 +159,18 @@ func TestEntRepository_DeleteFile(t *testing.T) { var tags []*Tag var targets []*RequestTarget var group *Group - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 50), tags, targets, group, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 50), + tags, targets, + group, user.ID) assert.NoError(t, err) mimetype := "image/png" diff --git a/model/group.go b/model/group.go index 61f2bce8..1a953e10 100644 --- a/model/group.go +++ b/model/group.go @@ -12,7 +12,9 @@ type GroupRepository interface { GetGroups(ctx context.Context) ([]*Group, error) GetGroup(ctx context.Context, groupID uuid.UUID) (*Group, error) CreateGroup(ctx context.Context, name string, description string, budget *int) (*Group, error) - UpdateGroup(ctx context.Context, groupID uuid.UUID, name string, description string, budget *int) (*Group, error) + UpdateGroup( + ctx context.Context, groupID uuid.UUID, name string, description string, budget *int, + ) (*Group, error) DeleteGroup(ctx context.Context, groupID uuid.UUID) error GetOwners(ctx context.Context, groupID uuid.UUID) ([]*Owner, error) AddOwners(ctx context.Context, groupID uuid.UUID, ownerIDs []uuid.UUID) ([]*Owner, error) diff --git a/model/group_impl.go b/model/group_impl.go index a7cae0ac..cb354e07 100644 --- a/model/group_impl.go +++ b/model/group_impl.go @@ -34,7 +34,9 @@ func (repo *EntRepository) GetGroup(ctx context.Context, groupID uuid.UUID) (*Gr return ConvertEntGroupToModelGroup(g), nil } -func (repo *EntRepository) CreateGroup(ctx context.Context, name string, description string, budget *int) (*Group, error) { +func (repo *EntRepository) CreateGroup( + ctx context.Context, name string, description string, budget *int, +) (*Group, error) { created, err := repo.client.Group. Create(). SetName(name). @@ -48,7 +50,9 @@ func (repo *EntRepository) CreateGroup(ctx context.Context, name string, descrip return ConvertEntGroupToModelGroup(created), nil } -func (repo *EntRepository) UpdateGroup(ctx context.Context, groupID uuid.UUID, name string, description string, budget *int) (*Group, error) { +func (repo *EntRepository) UpdateGroup( + ctx context.Context, groupID uuid.UUID, name string, description string, budget *int, +) (*Group, error) { updated, err := repo.client.Group. UpdateOneID(groupID). SetName(name). @@ -88,7 +92,9 @@ func (repo *EntRepository) GetOwners(ctx context.Context, groupID uuid.UUID) ([] return owners, nil } -func (repo *EntRepository) AddOwners(ctx context.Context, groupID uuid.UUID, ownerIDs []uuid.UUID) ([]*Owner, error) { +func (repo *EntRepository) AddOwners( + ctx context.Context, groupID uuid.UUID, ownerIDs []uuid.UUID, +) ([]*Owner, error) { _, err := repo.client.Group. Update(). Where(group.IDEQ(groupID)). @@ -104,7 +110,9 @@ func (repo *EntRepository) AddOwners(ctx context.Context, groupID uuid.UUID, own return resowners, nil } -func (repo *EntRepository) DeleteOwners(ctx context.Context, groupID uuid.UUID, ownerIDs []uuid.UUID) error { +func (repo *EntRepository) DeleteOwners( + ctx context.Context, groupID uuid.UUID, ownerIDs []uuid.UUID, +) error { _, err := repo.client.Group. Update(). Where(group.IDEQ(groupID)). @@ -132,7 +140,9 @@ func (repo *EntRepository) GetMembers(ctx context.Context, groupID uuid.UUID) ([ return modelmembers, nil } -func (repo *EntRepository) AddMembers(ctx context.Context, groupID uuid.UUID, userIDs []uuid.UUID) ([]*Member, error) { +func (repo *EntRepository) AddMembers( + ctx context.Context, groupID uuid.UUID, userIDs []uuid.UUID, +) ([]*Member, error) { _, err := repo.client.Group. Update(). Where(group.IDEQ(groupID)). @@ -149,7 +159,9 @@ func (repo *EntRepository) AddMembers(ctx context.Context, groupID uuid.UUID, us return resMembers, nil } -func (repo *EntRepository) DeleteMembers(ctx context.Context, groupID uuid.UUID, userIDs []uuid.UUID) error { +func (repo *EntRepository) DeleteMembers( + ctx context.Context, groupID uuid.UUID, userIDs []uuid.UUID, +) error { _, err := repo.client.Group. Update(). Where(group.IDEQ(groupID)). diff --git a/model/group_impl_test.go b/model/group_impl_test.go index dd09d332..6104647d 100644 --- a/model/group_impl_test.go +++ b/model/group_impl_test.go @@ -22,7 +22,11 @@ func TestEntRepository_GetGroups(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) groups, err := repo.GetGroups(ctx) @@ -48,7 +52,11 @@ func TestEntRepository_GetGroup(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) g, err := repo.GetGroup(ctx, group.ID) @@ -112,7 +120,11 @@ func TestEntRepository_UpdateGroup(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) updatedBudget := random.Numeric(t, 10000) @@ -132,14 +144,23 @@ func TestEntRepository_UpdateGroup(t *testing.T) { t.Run("UnknownGroup", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - _, err := repo.UpdateGroup(ctx, uuid.New(), random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + _, err := repo.UpdateGroup( + ctx, + uuid.New(), + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) assert.Error(t, err) }) t.Run("SuccessWithNilBudget", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) ug := Group{ @@ -158,7 +179,11 @@ func TestEntRepository_UpdateGroup(t *testing.T) { t.Run("FailedWithEmptyName", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) _, err = repo.UpdateGroup(ctx, group.ID, "", random.AlphaNumeric(t, 15), &budget) @@ -175,7 +200,11 @@ func TestEntRepository_DeleteGroup(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) err = repo.DeleteGroup(ctx, group.ID) @@ -201,12 +230,24 @@ func TestEntRepository_GetMembers(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) - user1, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), true) + user1, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + true) require.NoError(t, err) - user2, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), true) + user2, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + true) require.NoError(t, err) _, err = repo.AddMembers(ctx, group.ID, []uuid.UUID{user1.ID, user2.ID}) @@ -227,7 +268,11 @@ func TestEntRepository_GetMembers(t *testing.T) { t.Parallel() ctx := context.Background() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) got, err := repo2.GetMembers(ctx, group.ID) @@ -245,10 +290,18 @@ func TestEntRepository_CreateMember(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + true) require.NoError(t, err) member, err := repo.AddMembers(ctx, group.ID, []uuid.UUID{user.ID}) @@ -260,7 +313,11 @@ func TestEntRepository_CreateMember(t *testing.T) { t.Parallel() ctx := context.Background() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) _, err = repo.AddMembers(ctx, group.ID, []uuid.UUID{uuid.New()}) @@ -277,10 +334,18 @@ func TestEntRepository_DeleteMember(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + true) require.NoError(t, err) member, err := repo.AddMembers(ctx, group.ID, []uuid.UUID{user.ID}) require.NoError(t, err) @@ -299,12 +364,23 @@ func TestEntRepository_GetOwners(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) - user1, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), true) + user1, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), true) require.NoError(t, err) - user2, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), true) + user2, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + true) require.NoError(t, err) _, err = repo.AddOwners(ctx, group.ID, []uuid.UUID{user1.ID, user2.ID}) @@ -325,7 +401,11 @@ func TestEntRepository_GetOwners(t *testing.T) { t.Parallel() ctx := context.Background() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) got, err := repo.GetOwners(ctx, group.ID) @@ -343,10 +423,18 @@ func TestEntRepository_CreateOwner(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + true) require.NoError(t, err) owner, err := repo.AddOwners(ctx, group.ID, []uuid.UUID{user.ID}) @@ -358,7 +446,11 @@ func TestEntRepository_CreateOwner(t *testing.T) { t.Parallel() ctx := context.Background() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) _, err = repo.AddOwners(ctx, group.ID, []uuid.UUID{uuid.New()}) @@ -375,10 +467,18 @@ func TestEntRepository_DeleteOwner(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() budget := random.Numeric(t, 100000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + &budget) require.NoError(t, err) - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 15), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 15), + true) require.NoError(t, err) _, err = repo.AddOwners(ctx, group.ID, []uuid.UUID{user.ID}) require.NoError(t, err) diff --git a/model/model.go b/model/model.go index 59ce8f49..ce106ba9 100644 --- a/model/model.go +++ b/model/model.go @@ -24,7 +24,9 @@ func SetupEntClient() (*ent.Client, error) { dbName := testutil.GetEnvOrDefault("MARIADB_DATABASE", "jomon") dbPort := testutil.GetEnvOrDefault("MARIADB_PORT", "3306") - dbDsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbPort, dbName) + dbDsn := fmt.Sprintf( + "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", + dbUser, dbPass, dbHost, dbPort, dbName) client, err := ent.Open("mysql", dbDsn, entOptions...) if err != nil { diff --git a/model/model_test.go b/model/model_test.go index 9f37220b..7634af84 100644 --- a/model/model_test.go +++ b/model/model_test.go @@ -12,7 +12,8 @@ import ( "github.com/traPtitech/Jomon/testutil" ) -func SetupTestEntClient(t *testing.T, dbName string) (*ent.Client, error) { +func SetupTestEntClient(t *testing.T, ctx context.Context, dbName string) (*ent.Client, error) { + t.Helper() entOptions := []enttest.Option{ enttest.WithOptions(ent.Log(t.Log)), } @@ -21,7 +22,9 @@ func SetupTestEntClient(t *testing.T, dbName string) (*ent.Client, error) { dbHost := testutil.GetEnvOrDefault("MARIADB_HOSTNAME", "db") dbPort := testutil.GetEnvOrDefault("MARIADB_PORT", "3306") - dbDsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbPort) + dbDsn := fmt.Sprintf( + "%s:%s@tcp(%s:%s)/?charset=utf8mb4&parseTime=True&loc=Local", + dbUser, dbPass, dbHost, dbPort) conn, err := sql.Open("mysql", dbDsn) if err != nil { return nil, err @@ -32,12 +35,13 @@ func SetupTestEntClient(t *testing.T, dbName string) (*ent.Client, error) { return nil, err } - dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbPort, dbName) + dsn := fmt.Sprintf( + "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", + dbUser, dbPass, dbHost, dbPort, dbName) + // nolint:contextcheck client := enttest.Open(t, "mysql", dsn, entOptions...).Debug() - ctx := context.Background() - if err := client.Schema.Create(ctx); err != nil { return nil, err } diff --git a/model/repository.go b/model/repository.go index 5d2525af..02cbd4a1 100644 --- a/model/repository.go +++ b/model/repository.go @@ -28,7 +28,7 @@ type EntRepository struct { storage storagePkg.Storage } -func NewEntRepository(client *ent.Client, storage storagePkg.Storage) Repository { +func NewEntRepository(client *ent.Client, storage storagePkg.Storage) *EntRepository { repo := &EntRepository{ client: client, storage: storage, diff --git a/model/repository_test.go b/model/repository_test.go index 746beb4e..94fbbdff 100644 --- a/model/repository_test.go +++ b/model/repository_test.go @@ -13,9 +13,10 @@ const ( dbPrefix = "jomon_test_repo_" ) +//nolint:ireturn func setup(t *testing.T, ctx context.Context, dbName string) (*ent.Client, storage.Storage, error) { t.Helper() - client, err := SetupTestEntClient(t, dbPrefix+dbName) + client, err := SetupTestEntClient(t, ctx, dbPrefix+dbName) if err != nil { return nil, nil, err } diff --git a/model/request.go b/model/request.go index 9986abe1..13d424d1 100644 --- a/model/request.go +++ b/model/request.go @@ -10,9 +10,15 @@ import ( type RequestRepository interface { GetRequests(ctx context.Context, query RequestQuery) ([]*RequestResponse, error) - CreateRequest(ctx context.Context, title string, content string, tags []*Tag, targets []*RequestTarget, group *Group, userID uuid.UUID) (*RequestDetail, error) + CreateRequest( + ctx context.Context, title string, content string, + tags []*Tag, targets []*RequestTarget, group *Group, userID uuid.UUID, + ) (*RequestDetail, error) GetRequest(ctx context.Context, requestID uuid.UUID) (*RequestDetail, error) - UpdateRequest(ctx context.Context, requestID uuid.UUID, title string, content string, tags []*Tag, targets []*RequestTarget, group *Group) (*RequestDetail, error) + UpdateRequest( + ctx context.Context, requestID uuid.UUID, title string, content string, + tags []*Tag, targets []*RequestTarget, group *Group, + ) (*RequestDetail, error) } type Request struct { diff --git a/model/request_impl.go b/model/request_impl.go index 51e7a5ab..7caabf23 100644 --- a/model/request_impl.go +++ b/model/request_impl.go @@ -15,7 +15,9 @@ import ( "github.com/traPtitech/Jomon/ent/user" ) -func (repo *EntRepository) GetRequests(ctx context.Context, query RequestQuery) ([]*RequestResponse, error) { +func (repo *EntRepository) GetRequests( + ctx context.Context, query RequestQuery, +) ([]*RequestResponse, error) { // Querying var requestsq *ent.RequestQuery var err error @@ -115,13 +117,17 @@ func (repo *EntRepository) GetRequests(ctx context.Context, query RequestQuery) } reqres := lo.Map(requests, func(r *ent.Request, index int) *RequestResponse { - return convertEntRequestResponseToModelRequestResponse(r, r.Edges.Tag, r.Edges.Group, r.Edges.Status[0], r.Edges.User) + return convertEntRequestResponseToModelRequestResponse( + r, r.Edges.Tag, r.Edges.Group, r.Edges.Status[0], r.Edges.User) }) return reqres, nil } -func (repo *EntRepository) CreateRequest(ctx context.Context, title string, content string, tags []*Tag, targets []*RequestTarget, group *Group, userID uuid.UUID) (*RequestDetail, error) { +func (repo *EntRepository) CreateRequest( + ctx context.Context, title string, content string, + tags []*Tag, targets []*RequestTarget, group *Group, userID uuid.UUID, +) (*RequestDetail, error) { tx, err := repo.client.Tx(ctx) if err != nil { return nil, err @@ -209,7 +215,9 @@ func (repo *EntRepository) CreateRequest(ctx context.Context, title string, cont return reqdetail, nil } -func (repo *EntRepository) GetRequest(ctx context.Context, requestID uuid.UUID) (*RequestDetail, error) { +func (repo *EntRepository) GetRequest( + ctx context.Context, requestID uuid.UUID, +) (*RequestDetail, error) { r, err := repo.client.Request. Query(). Where(request.IDEQ(requestID)). @@ -253,7 +261,10 @@ func (repo *EntRepository) GetRequest(ctx context.Context, requestID uuid.UUID) return reqdetail, nil } -func (repo *EntRepository) UpdateRequest(ctx context.Context, requestID uuid.UUID, title string, content string, tags []*Tag, targets []*RequestTarget, group *Group) (*RequestDetail, error) { +func (repo *EntRepository) UpdateRequest( + ctx context.Context, requestID uuid.UUID, title string, content string, + tags []*Tag, targets []*RequestTarget, group *Group, +) (*RequestDetail, error) { tx, err := repo.client.Tx(ctx) if err != nil { return nil, err @@ -363,7 +374,10 @@ func (repo *EntRepository) UpdateRequest(ctx context.Context, requestID uuid.UUI return reqdetail, nil } -func convertEntRequestResponseToModelRequestResponse(request *ent.Request, tags []*ent.Tag, group *ent.Group, status *ent.RequestStatus, user *ent.User) *RequestResponse { +func convertEntRequestResponseToModelRequestResponse( + request *ent.Request, tags []*ent.Tag, + group *ent.Group, status *ent.RequestStatus, user *ent.User, +) *RequestResponse { if request == nil { return nil } diff --git a/model/request_impl_test.go b/model/request_impl_test.go index 8fc3d72c..91fa8bcf 100644 --- a/model/request_impl_test.go +++ b/model/request_impl_test.go @@ -41,9 +41,17 @@ func TestEntRepository_GetRequests(t *testing.T) { t.Run("SuccessWithSortCreatedAt", func(t *testing.T) { t.Parallel() ctx := context.Background() - user1, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - user2, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user1, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + user2, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -53,13 +61,31 @@ func TestEntRepository_GetRequests(t *testing.T) { } budget := random.Numeric(t, 10000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) - require.NoError(t, err) - - request1, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user1.ID) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) + require.NoError(t, err) + + request1, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user1.ID) require.NoError(t, err) time.Sleep(1 * time.Second) - request2, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user2.ID) + request2, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user2.ID) require.NoError(t, err) sort := "created_at" @@ -87,9 +113,17 @@ func TestEntRepository_GetRequests(t *testing.T) { t.Run("SuccessWithReverseSortCreatedAt", func(t *testing.T) { t.Parallel() ctx := context.Background() - user1, err := repo2.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - user2, err := repo2.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user1, err := repo2.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + user2, err := repo2.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo2.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -99,13 +133,31 @@ func TestEntRepository_GetRequests(t *testing.T) { } budget := random.Numeric(t, 10000) - group, err := repo2.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) - require.NoError(t, err) - - request1, err := repo2.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user1.ID) + group, err := repo2.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) + require.NoError(t, err) + + request1, err := repo2.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user1.ID) require.NoError(t, err) time.Sleep(1 * time.Second) - request2, err := repo2.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user2.ID) + request2, err := repo2.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user2.ID) require.NoError(t, err) sort := "-created_at" @@ -133,9 +185,17 @@ func TestEntRepository_GetRequests(t *testing.T) { t.Run("SuccessWithSortTitle", func(t *testing.T) { t.Parallel() ctx := context.Background() - user1, err := repo3.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - user2, err := repo3.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user1, err := repo3.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + user2, err := repo3.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo3.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -145,12 +205,30 @@ func TestEntRepository_GetRequests(t *testing.T) { } budget := random.Numeric(t, 10000) - group, err := repo3.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) - require.NoError(t, err) - - request1, err := repo3.CreateRequest(ctx, "b", random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user1.ID) - require.NoError(t, err) - request2, err := repo3.CreateRequest(ctx, "a", random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user2.ID) + group, err := repo3.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) + require.NoError(t, err) + + request1, err := repo3.CreateRequest( + ctx, + "b", + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user1.ID) + require.NoError(t, err) + request2, err := repo3.CreateRequest( + ctx, + "a", + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user2.ID) require.NoError(t, err) sort := "title" @@ -178,9 +256,17 @@ func TestEntRepository_GetRequests(t *testing.T) { t.Run("SuccessWithReverseSortTitle", func(t *testing.T) { t.Parallel() ctx := context.Background() - user1, err := repo4.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - user2, err := repo4.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user1, err := repo4.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + user2, err := repo4.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo4.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -190,12 +276,30 @@ func TestEntRepository_GetRequests(t *testing.T) { } budget := random.Numeric(t, 10000) - group, err := repo4.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) - require.NoError(t, err) - - request1, err := repo4.CreateRequest(ctx, "b", random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user1.ID) - require.NoError(t, err) - request2, err := repo4.CreateRequest(ctx, "a", random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user2.ID) + group, err := repo4.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) + require.NoError(t, err) + + request1, err := repo4.CreateRequest( + ctx, + "b", + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user1.ID) + require.NoError(t, err) + request2, err := repo4.CreateRequest( + ctx, + "a", + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user2.ID) require.NoError(t, err) sort := "-title" @@ -223,9 +327,17 @@ func TestEntRepository_GetRequests(t *testing.T) { t.Run("SuccessWithQueryTarget", func(t *testing.T) { t.Parallel() ctx := context.Background() - user1, err := repo5.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - user2, err := repo5.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user1, err := repo5.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + user2, err := repo5.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo5.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -239,12 +351,30 @@ func TestEntRepository_GetRequests(t *testing.T) { } budget := random.Numeric(t, 10000) - group, err := repo5.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) - require.NoError(t, err) - - request1, err := repo5.CreateRequest(ctx, "b", random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target1}, group, user1.ID) - require.NoError(t, err) - _, err = repo5.CreateRequest(ctx, "a", random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target2}, group, user2.ID) + group, err := repo5.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) + require.NoError(t, err) + + request1, err := repo5.CreateRequest( + ctx, + "b", + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target1}, + group, + user1.ID) + require.NoError(t, err) + _, err = repo5.CreateRequest( + ctx, + "a", + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target2}, + group, + user2.ID) require.NoError(t, err) target := target1.Target @@ -265,9 +395,17 @@ func TestEntRepository_GetRequests(t *testing.T) { t.Run("SuccessWithQuerySince", func(t *testing.T) { t.Parallel() ctx := context.Background() - user1, err := repo6.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - user2, err := repo6.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user1, err := repo6.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + user2, err := repo6.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo6.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -277,13 +415,31 @@ func TestEntRepository_GetRequests(t *testing.T) { } budget := random.Numeric(t, 10000) - group, err := repo6.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) - require.NoError(t, err) - - request1, err := repo6.CreateRequest(ctx, "b", random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user1.ID) + group, err := repo6.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) + require.NoError(t, err) + + request1, err := repo6.CreateRequest( + ctx, + "b", + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user1.ID) require.NoError(t, err) time.Sleep(1 * time.Second) - request2, err := repo6.CreateRequest(ctx, "a", random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user2.ID) + request2, err := repo6.CreateRequest( + ctx, + "a", + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user2.ID) require.NoError(t, err) since := request1.CreatedAt.Add(10 * time.Millisecond) @@ -304,9 +460,17 @@ func TestEntRepository_GetRequests(t *testing.T) { t.Run("SuccessWithQueryUntil", func(t *testing.T) { t.Parallel() ctx := context.Background() - user1, err := repo7.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - user2, err := repo7.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user1, err := repo7.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + user2, err := repo7.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo7.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -316,13 +480,31 @@ func TestEntRepository_GetRequests(t *testing.T) { } budget := random.Numeric(t, 10000) - group, err := repo7.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) - require.NoError(t, err) - - request1, err := repo7.CreateRequest(ctx, "b", random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user1.ID) + group, err := repo7.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) + require.NoError(t, err) + + request1, err := repo7.CreateRequest( + ctx, + "b", + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user1.ID) require.NoError(t, err) time.Sleep(2 * time.Second) - request2, err := repo7.CreateRequest(ctx, "a", random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user2.ID) + request2, err := repo7.CreateRequest( + ctx, + "a", + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user2.ID) require.NoError(t, err) until := request2.CreatedAt.Add(-1 * time.Second) @@ -343,9 +525,17 @@ func TestEntRepository_GetRequests(t *testing.T) { t.Run("SuccessWithQueryStatus", func(t *testing.T) { t.Parallel() ctx := context.Background() - user1, err := repo8.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) - require.NoError(t, err) - user2, err := repo8.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user1, err := repo8.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) + require.NoError(t, err) + user2, err := repo8.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo8.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -355,13 +545,31 @@ func TestEntRepository_GetRequests(t *testing.T) { } budget := random.Numeric(t, 10000) - group, err := repo8.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) - require.NoError(t, err) - - request1, err := repo8.CreateRequest(ctx, "b", random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user1.ID) + group, err := repo8.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) + require.NoError(t, err) + + request1, err := repo8.CreateRequest( + ctx, + "b", + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user1.ID) require.NoError(t, err) time.Sleep(2 * time.Second) - _, err = repo8.CreateRequest(ctx, "a", random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user2.ID) + _, err = repo8.CreateRequest( + ctx, + "a", + random.AlphaNumeric(t, 100), + []*Tag{tag}, + []*RequestTarget{target}, + group, + user2.ID) require.NoError(t, err) time.Sleep(1 * time.Second) @@ -405,7 +613,11 @@ func TestEntRepository_CreateRequest(t *testing.T) { ctx := context.Background() title := random.AlphaNumeric(t, 40) content := random.AlphaNumeric(t, 100) - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -415,10 +627,18 @@ func TestEntRepository_CreateRequest(t *testing.T) { } budget := random.Numeric(t, 10000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) - require.NoError(t, err) - - request, err := repo.CreateRequest(ctx, title, content, []*Tag{tag}, []*RequestTarget{target}, group, user.ID) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) + require.NoError(t, err) + + request, err := repo.CreateRequest( + ctx, + title, content, + []*Tag{tag}, []*RequestTarget{target}, + group, user.ID) assert.NoError(t, err) assert.Equal(t, request.CreatedBy, user.ID) assert.Equal(t, request.Status, Status(1)) @@ -437,10 +657,18 @@ func TestEntRepository_CreateRequest(t *testing.T) { require.NoError(t, err) budget := random.Numeric(t, 10000) - group, err := repo2.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) - require.NoError(t, err) - - _, err = repo2.CreateRequest(ctx, title, content, []*Tag{tag}, []*RequestTarget{}, group, uuid.New()) + group, err := repo2.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) + require.NoError(t, err) + + _, err = repo2.CreateRequest( + ctx, + title, content, + []*Tag{tag}, []*RequestTarget{}, + group, uuid.New()) assert.Error(t, err) }) @@ -449,7 +677,11 @@ func TestEntRepository_CreateRequest(t *testing.T) { ctx := context.Background() title := random.AlphaNumeric(t, 40) content := random.AlphaNumeric(t, 100) - user, err := repo3.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo3.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) date := time.Now() @@ -461,10 +693,18 @@ func TestEntRepository_CreateRequest(t *testing.T) { } budget := random.Numeric(t, 10000) - group, err := repo3.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) - require.NoError(t, err) - - _, err = repo3.CreateRequest(ctx, title, content, []*Tag{tag}, []*RequestTarget{}, group, user.ID) + group, err := repo3.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) + require.NoError(t, err) + + _, err = repo3.CreateRequest( + ctx, + title, content, + []*Tag{tag}, []*RequestTarget{}, + group, user.ID) assert.Error(t, err) }) @@ -473,7 +713,11 @@ func TestEntRepository_CreateRequest(t *testing.T) { ctx := context.Background() title := random.AlphaNumeric(t, 40) content := random.AlphaNumeric(t, 100) - user, err := repo4.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo4.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo4.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -489,7 +733,11 @@ func TestEntRepository_CreateRequest(t *testing.T) { UpdatedAt: date, } - _, err = repo4.CreateRequest(ctx, title, content, []*Tag{tag}, []*RequestTarget{}, group, user.ID) + _, err = repo4.CreateRequest( + ctx, + title, content, + []*Tag{tag}, []*RequestTarget{}, + group, user.ID) assert.Error(t, err) }) } @@ -506,7 +754,11 @@ func TestEntRepository_GetRequest(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() ctx := context.Background() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -515,9 +767,18 @@ func TestEntRepository_GetRequest(t *testing.T) { Amount: random.Numeric(t, 10000), } budget := random.Numeric(t, 10000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{tag}, []*RequestTarget{target}, + group, user.ID) require.NoError(t, err) got, err := repo.GetRequest(ctx, request.ID) @@ -565,7 +826,11 @@ func TestEntRepository_UpdateRequest(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() ctx := context.Background() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -574,12 +839,25 @@ func TestEntRepository_UpdateRequest(t *testing.T) { Amount: random.Numeric(t, 10000), } budget := random.Numeric(t, 10000) - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) - require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user.ID) - require.NoError(t, err) - - updatedRequest, err := repo.UpdateRequest(ctx, request.ID, request.Title, request.Content, []*Tag{tag}, []*RequestTarget{target}, group) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) + require.NoError(t, err) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{tag}, []*RequestTarget{target}, + group, user.ID) + require.NoError(t, err) + + updatedRequest, err := repo.UpdateRequest( + ctx, + request.ID, request.Title, request.Content, + []*Tag{tag}, []*RequestTarget{target}, + group) assert.NoError(t, err) assert.Equal(t, updatedRequest.ID, request.ID) assert.Equal(t, updatedRequest.Status, request.Status) @@ -596,7 +874,11 @@ func TestEntRepository_UpdateRequest(t *testing.T) { t.Run("Success2", func(t *testing.T) { t.Parallel() ctx := context.Background() - user, err := repo2.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo2.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo2.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -605,13 +887,26 @@ func TestEntRepository_UpdateRequest(t *testing.T) { Amount: random.Numeric(t, 10000), } budget := random.Numeric(t, 10000) - group, err := repo2.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) + group, err := repo2.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) require.NoError(t, err) - request, err := repo2.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user.ID) + request, err := repo2.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{tag}, []*RequestTarget{target}, + group, user.ID) require.NoError(t, err) title := random.AlphaNumeric(t, 40) - updatedRequest, err := repo2.UpdateRequest(ctx, request.ID, title, request.Content, []*Tag{tag}, []*RequestTarget{target}, group) + updatedRequest, err := repo2.UpdateRequest( + ctx, + request.ID, title, request.Content, + []*Tag{tag}, []*RequestTarget{target}, + group) assert.NoError(t, err) assert.Equal(t, updatedRequest.ID, request.ID) assert.Equal(t, updatedRequest.Status, request.Status) @@ -628,7 +923,11 @@ func TestEntRepository_UpdateRequest(t *testing.T) { t.Run("Success3", func(t *testing.T) { t.Parallel() ctx := context.Background() - user, err := repo3.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo3.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo3.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -637,12 +936,25 @@ func TestEntRepository_UpdateRequest(t *testing.T) { Amount: random.Numeric(t, 10000), } budget := random.Numeric(t, 10000) - group, err := repo3.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) - require.NoError(t, err) - request, err := repo3.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user.ID) + group, err := repo3.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) + require.NoError(t, err) + request, err := repo3.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{tag}, []*RequestTarget{target}, + group, user.ID) require.NoError(t, err) content := random.AlphaNumeric(t, 100) - updatedRequest, err := repo3.UpdateRequest(ctx, request.ID, request.Title, content, []*Tag{tag}, []*RequestTarget{target}, group) + updatedRequest, err := repo3.UpdateRequest( + ctx, + request.ID, request.Title, content, + []*Tag{tag}, []*RequestTarget{target}, + group) assert.NoError(t, err) assert.Equal(t, updatedRequest.ID, request.ID) assert.Equal(t, updatedRequest.Status, request.Status) @@ -659,7 +971,11 @@ func TestEntRepository_UpdateRequest(t *testing.T) { t.Run("UnknownTag", func(t *testing.T) { t.Parallel() ctx := context.Background() - user, err := repo4.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo4.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo4.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -668,9 +984,18 @@ func TestEntRepository_UpdateRequest(t *testing.T) { Amount: random.Numeric(t, 10000), } budget := random.Numeric(t, 10000) - group, err := repo4.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) + group, err := repo4.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) require.NoError(t, err) - request, err := repo4.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user.ID) + request, err := repo4.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{tag}, []*RequestTarget{target}, + group, user.ID) require.NoError(t, err) date := time.Now() @@ -680,14 +1005,22 @@ func TestEntRepository_UpdateRequest(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - _, err = repo4.UpdateRequest(ctx, request.ID, request.Title, request.Content, []*Tag{unknownTag}, []*RequestTarget{target}, group) + _, err = repo4.UpdateRequest( + ctx, + request.ID, request.Title, request.Content, + []*Tag{unknownTag}, []*RequestTarget{target}, + group) assert.Error(t, err) }) t.Run("UnknownGroup", func(t *testing.T) { t.Parallel() ctx := context.Background() - user, err := repo5.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo5.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) tag, err := repo5.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) @@ -696,9 +1029,18 @@ func TestEntRepository_UpdateRequest(t *testing.T) { Amount: random.Numeric(t, 10000), } budget := random.Numeric(t, 10000) - group, err := repo5.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) + group, err := repo5.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) require.NoError(t, err) - request, err := repo5.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 100), []*Tag{tag}, []*RequestTarget{target}, group, user.ID) + request, err := repo5.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 100), + []*Tag{tag}, []*RequestTarget{target}, + group, user.ID) require.NoError(t, err) date := time.Now() @@ -711,7 +1053,11 @@ func TestEntRepository_UpdateRequest(t *testing.T) { CreatedAt: date, UpdatedAt: date, } - _, err = repo5.UpdateRequest(ctx, request.ID, request.Title, request.Content, []*Tag{tag}, []*RequestTarget{target}, unknownGroup) + _, err = repo5.UpdateRequest( + ctx, + request.ID, request.Title, request.Content, + []*Tag{tag}, []*RequestTarget{target}, + unknownGroup) assert.Error(t, err) }) } diff --git a/model/request_status.go b/model/request_status.go index 7dd98d96..ea6f9b20 100644 --- a/model/request_status.go +++ b/model/request_status.go @@ -69,7 +69,9 @@ func (s *Status) UnmarshalJSON(data []byte) error { } type RequestStatusRepository interface { - CreateStatus(ctx context.Context, requestID uuid.UUID, userID uuid.UUID, status Status) (*RequestStatus, error) + CreateStatus( + ctx context.Context, requestID uuid.UUID, userID uuid.UUID, status Status, + ) (*RequestStatus, error) } type RequestStatus struct { diff --git a/model/request_status_impl.go b/model/request_status_impl.go index 9012e5ab..1e5a1c25 100644 --- a/model/request_status_impl.go +++ b/model/request_status_impl.go @@ -9,7 +9,9 @@ import ( "github.com/traPtitech/Jomon/ent/requeststatus" ) -func (repo *EntRepository) CreateStatus(ctx context.Context, requestID uuid.UUID, userID uuid.UUID, status Status) (*RequestStatus, error) { +func (repo *EntRepository) CreateStatus( + ctx context.Context, requestID uuid.UUID, userID uuid.UUID, status Status, +) (*RequestStatus, error) { c, err := repo.client.RequestStatus. Create(). SetStatus(requeststatus.Status(status.String())). diff --git a/model/request_status_impl_test.go b/model/request_status_impl_test.go index 9029f806..b3ecd830 100644 --- a/model/request_status_impl_test.go +++ b/model/request_status_impl_test.go @@ -18,9 +18,18 @@ func TestEntRepository_CreateStatus(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 40), nil, nil, nil, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 40), + nil, nil, + nil, user.ID) require.NoError(t, err) status := Status(random.Numeric(t, 5) + 1) @@ -31,9 +40,18 @@ func TestEntRepository_CreateStatus(t *testing.T) { t.Run("InvalidStatus", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 40), nil, nil, nil, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 40), + nil, nil, + nil, user.ID) require.NoError(t, err) invalidStatus := Status(6) @@ -43,7 +61,11 @@ func TestEntRepository_CreateStatus(t *testing.T) { t.Run("UnknownRequestID", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) status := Status(random.Numeric(t, 5) + 1) @@ -53,9 +75,18 @@ func TestEntRepository_CreateStatus(t *testing.T) { t.Run("UnknownUserID", func(t *testing.T) { t.Parallel() - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 40), nil, nil, nil, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 40), + nil, nil, + nil, user.ID) require.NoError(t, err) status := Status(random.Numeric(t, 5) + 1) diff --git a/model/request_target_impl.go b/model/request_target_impl.go index c108a836..6c0c6f39 100644 --- a/model/request_target_impl.go +++ b/model/request_target_impl.go @@ -10,7 +10,9 @@ import ( "github.com/traPtitech/Jomon/ent/requesttarget" ) -func (repo *EntRepository) GetRequestTargets(ctx context.Context, requestID uuid.UUID) ([]*RequestTargetDetail, error) { +func (repo *EntRepository) GetRequestTargets( + ctx context.Context, requestID uuid.UUID, +) ([]*RequestTargetDetail, error) { // Querying ts, err := repo.client.RequestTarget. Query(). @@ -30,7 +32,9 @@ func (repo *EntRepository) GetRequestTargets(ctx context.Context, requestID uuid return targets, err } -func (repo *EntRepository) createRequestTargets(ctx context.Context, tx *ent.Tx, requestID uuid.UUID, targets []*RequestTarget) ([]*RequestTargetDetail, error) { +func (repo *EntRepository) createRequestTargets( + ctx context.Context, tx *ent.Tx, requestID uuid.UUID, targets []*RequestTarget, +) ([]*RequestTargetDetail, error) { bulk := lo.Map(targets, func(t *RequestTarget, index int) *ent.RequestTargetCreate { return tx.Client().RequestTarget. Create(). @@ -63,7 +67,9 @@ func (repo *EntRepository) createRequestTargets(ctx context.Context, tx *ent.Tx, return ts, nil } -func (repo *EntRepository) deleteRequestTargets(ctx context.Context, tx *ent.Tx, requestID uuid.UUID) error { +func (repo *EntRepository) deleteRequestTargets( + ctx context.Context, tx *ent.Tx, requestID uuid.UUID, +) error { _, err := tx.Client().RequestTarget. Delete(). Where( diff --git a/model/request_target_impl_test.go b/model/request_target_impl_test.go index 1fdffffe..402caa45 100644 --- a/model/request_target_impl_test.go +++ b/model/request_target_impl_test.go @@ -20,9 +20,17 @@ func TestEntRepository_GetRequestTargets(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() - user1, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user1, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - user2, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user2, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) target1 := &RequestTarget{ Target: user1.ID, @@ -33,7 +41,12 @@ func TestEntRepository_GetRequestTargets(t *testing.T) { Amount: random.Numeric(t, 100000), } - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 40), nil, []*RequestTarget{target1, target2}, nil, user1.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 40), + nil, []*RequestTarget{target1, target2}, + nil, user1.ID) require.NoError(t, err) got, err := repo.GetRequestTargets(ctx, request.ID) assert.NoError(t, err) @@ -53,9 +66,18 @@ func TestEntRepository_GetRequestTargets(t *testing.T) { t.Run("Success2", func(t *testing.T) { t.Parallel() - user, err := repo2.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user, err := repo2.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - request, err := repo2.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 40), nil, nil, nil, user.ID) + request, err := repo2.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 40), + nil, nil, + nil, user.ID) require.NoError(t, err) got, err := repo2.GetRequestTargets(ctx, request.ID) assert.NoError(t, err) @@ -71,9 +93,17 @@ func TestEntRepository_createRequestTargets(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() - user1, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user1, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - user2, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user2, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) target1 := &RequestTarget{ Target: user1.ID, @@ -84,7 +114,12 @@ func TestEntRepository_createRequestTargets(t *testing.T) { Amount: random.Numeric(t, 100000), } - got, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 40), nil, []*RequestTarget{target1, target2}, nil, user1.ID) + got, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 40), + nil, []*RequestTarget{target1, target2}, + nil, user1.ID) assert.NoError(t, err) if got.Targets[0].Target == target1.Target { assert.Equal(t, got.Targets[0].Target, target1.Target) @@ -111,9 +146,17 @@ func TestEntRepository_deleteRequestTargets(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() - user1, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user1, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - user2, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user2, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) target1 := &RequestTarget{ Target: user1.ID, @@ -124,9 +167,20 @@ func TestEntRepository_deleteRequestTargets(t *testing.T) { Amount: random.Numeric(t, 100000), } - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 40), nil, []*RequestTarget{target1, target2}, nil, user1.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 40), + nil, []*RequestTarget{target1, target2}, + nil, user1.ID) require.NoError(t, err) - _, err = repo.UpdateRequest(ctx, request.ID, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 40), nil, []*RequestTarget{}, nil) + _, err = repo.UpdateRequest( + ctx, + request.ID, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 40), + nil, []*RequestTarget{}, + nil) assert.NoError(t, err) got, err := repo.GetRequestTargets(ctx, request.ID) assert.NoError(t, err) @@ -135,9 +189,17 @@ func TestEntRepository_deleteRequestTargets(t *testing.T) { t.Run("Success2", func(t *testing.T) { t.Parallel() - user1, err := repo2.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user1, err := repo2.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) - user2, err := repo2.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), true) + user2, err := repo2.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + true) require.NoError(t, err) target1 := &RequestTarget{ Target: user1.ID, @@ -148,9 +210,20 @@ func TestEntRepository_deleteRequestTargets(t *testing.T) { Amount: random.Numeric(t, 100000), } - request, err := repo2.CreateRequest(ctx, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 40), nil, nil, nil, user1.ID) + request, err := repo2.CreateRequest( + ctx, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 40), + nil, nil, + nil, user1.ID) require.NoError(t, err) - _, err = repo2.UpdateRequest(ctx, request.ID, random.AlphaNumeric(t, 40), random.AlphaNumeric(t, 40), nil, []*RequestTarget{target1, target2}, nil) + _, err = repo2.UpdateRequest( + ctx, + request.ID, + random.AlphaNumeric(t, 40), + random.AlphaNumeric(t, 40), + nil, []*RequestTarget{target1, target2}, + nil) assert.NoError(t, err) got, err := repo2.GetRequestTargets(ctx, request.ID) assert.NoError(t, err) diff --git a/model/tag_impl.go b/model/tag_impl.go index 8dab3fa7..da7c2c67 100644 --- a/model/tag_impl.go +++ b/model/tag_impl.go @@ -46,7 +46,9 @@ func (repo *EntRepository) CreateTag(ctx context.Context, name string) (*Tag, er return ConvertEntTagToModelTag(created), nil } -func (repo *EntRepository) UpdateTag(ctx context.Context, tagID uuid.UUID, name string) (*Tag, error) { +func (repo *EntRepository) UpdateTag( + ctx context.Context, tagID uuid.UUID, name string, +) (*Tag, error) { t, err := repo.client.Tag. UpdateOneID(tagID). SetName(name). diff --git a/model/transaction.go b/model/transaction.go index f4d046b3..43c9c3ba 100644 --- a/model/transaction.go +++ b/model/transaction.go @@ -10,9 +10,15 @@ import ( type TransactionRepository interface { GetTransactions(ctx context.Context, query TransactionQuery) ([]*TransactionResponse, error) - CreateTransaction(ctx context.Context, Amount int, Target string, tags []*uuid.UUID, group *uuid.UUID, requestID *uuid.UUID) (*TransactionResponse, error) + CreateTransaction( + ctx context.Context, Amount int, Target string, + tags []*uuid.UUID, group *uuid.UUID, requestID *uuid.UUID, + ) (*TransactionResponse, error) GetTransaction(ctx context.Context, transactionID uuid.UUID) (*TransactionResponse, error) - UpdateTransaction(ctx context.Context, transactionID uuid.UUID, Amount int, Target string, tags []*uuid.UUID, group *uuid.UUID, requestID *uuid.UUID) (*TransactionResponse, error) + UpdateTransaction( + ctx context.Context, transactionID uuid.UUID, Amount int, Target string, + tags []*uuid.UUID, group *uuid.UUID, requestID *uuid.UUID, + ) (*TransactionResponse, error) } type Transaction struct { diff --git a/model/transaction_detail_impl.go b/model/transaction_detail_impl.go index d57653b0..2b611089 100644 --- a/model/transaction_detail_impl.go +++ b/model/transaction_detail_impl.go @@ -9,7 +9,9 @@ import ( "github.com/traPtitech/Jomon/ent/transactiondetail" ) -func (repo *EntRepository) createTransactionDetail(ctx context.Context, tx *ent.Tx, amount int, target string) (*TransactionDetail, error) { +func (repo *EntRepository) createTransactionDetail( + ctx context.Context, tx *ent.Tx, amount int, target string, +) (*TransactionDetail, error) { enttd, err := tx.Client().TransactionDetail.Create(). SetAmount(amount). SetTarget(target). @@ -20,7 +22,9 @@ func (repo *EntRepository) createTransactionDetail(ctx context.Context, tx *ent. return convertEntTransactionDetailToModelTransactionDetail(enttd), nil } -func (repo *EntRepository) updateTransactionDetail(ctx context.Context, tx *ent.Tx, transactionID uuid.UUID, amount int, target string) (*TransactionDetail, error) { +func (repo *EntRepository) updateTransactionDetail( + ctx context.Context, tx *ent.Tx, transactionID uuid.UUID, amount int, target string, +) (*TransactionDetail, error) { _, err := tx.Client().TransactionDetail.Update(). Where(transactiondetail.HasTransactionWith( transaction.IDEQ(transactionID), @@ -41,7 +45,9 @@ func (repo *EntRepository) updateTransactionDetail(ctx context.Context, tx *ent. return convertEntTransactionDetailToModelTransactionDetail(enttd), nil } -func convertEntTransactionDetailToModelTransactionDetail(enttd *ent.TransactionDetail) *TransactionDetail { +func convertEntTransactionDetailToModelTransactionDetail( + enttd *ent.TransactionDetail, +) *TransactionDetail { if enttd == nil { return nil } diff --git a/model/transaction_detail_impl_test.go b/model/transaction_detail_impl_test.go index 5f411ec0..427aa50c 100644 --- a/model/transaction_detail_impl_test.go +++ b/model/transaction_detail_impl_test.go @@ -15,8 +15,6 @@ func TestEntRepository_createTransactionDetail(t *testing.T) { require.NoError(t, err) repo := NewEntRepository(client, storage) - r := repo.(*EntRepository) - t.Run("Success", func(t *testing.T) { t.Parallel() ctx := context.Background() @@ -32,7 +30,7 @@ func TestEntRepository_createTransactionDetail(t *testing.T) { amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 10) // Create TransactionDetail - td, err := r.createTransactionDetail(ctx, tx, amount, target) + td, err := repo.createTransactionDetail(ctx, tx, amount, target) assert.NoError(t, err) err = tx.Commit() assert.NoError(t, err) @@ -48,8 +46,6 @@ func TestEntRepository_updateTransactionDetail(t *testing.T) { require.NoError(t, err) repo := NewEntRepository(client, storage) - r := repo.(*EntRepository) - t.Run("Success", func(t *testing.T) { t.Parallel() ctx := context.Background() @@ -72,7 +68,7 @@ func TestEntRepository_updateTransactionDetail(t *testing.T) { // Update TransactionDetail updatedAmount := 1000 updatedTarget := "fuga" - td, err := r.updateTransactionDetail(ctx, tx, trns.ID, updatedAmount, updatedTarget) + td, err := repo.updateTransactionDetail(ctx, tx, trns.ID, updatedAmount, updatedTarget) assert.NoError(t, err) err = tx.Commit() assert.NoError(t, err) diff --git a/model/transaction_impl.go b/model/transaction_impl.go index 7539aba0..d0d6fd1c 100644 --- a/model/transaction_impl.go +++ b/model/transaction_impl.go @@ -15,7 +15,9 @@ import ( "github.com/traPtitech/Jomon/ent/transactiondetail" ) -func (repo *EntRepository) GetTransactions(ctx context.Context, query TransactionQuery) ([]*TransactionResponse, error) { +func (repo *EntRepository) GetTransactions( + ctx context.Context, query TransactionQuery, +) ([]*TransactionResponse, error) { // Querying var transactionsq *ent.TransactionQuery if query.Sort == nil || *query.Sort == "" || *query.Sort == "created_at" { @@ -122,7 +124,9 @@ func (repo *EntRepository) GetTransactions(ctx context.Context, query Transactio return res, nil } -func (repo *EntRepository) GetTransaction(ctx context.Context, transactionID uuid.UUID) (*TransactionResponse, error) { +func (repo *EntRepository) GetTransaction( + ctx context.Context, transactionID uuid.UUID, +) (*TransactionResponse, error) { // Querying tx, err := repo.client.Transaction. Query(). @@ -142,7 +146,10 @@ func (repo *EntRepository) GetTransaction(ctx context.Context, transactionID uui return ConvertEntTransactionToModelTransactionResponse(tx), nil } -func (repo *EntRepository) CreateTransaction(ctx context.Context, amount int, target string, tags []*uuid.UUID, groupID *uuid.UUID, requestID *uuid.UUID) (*TransactionResponse, error) { +func (repo *EntRepository) CreateTransaction( + ctx context.Context, amount int, target string, + tags []*uuid.UUID, groupID *uuid.UUID, requestID *uuid.UUID, +) (*TransactionResponse, error) { tx, err := repo.client.Tx(ctx) if err != nil { return nil, err @@ -240,7 +247,10 @@ func (repo *EntRepository) CreateTransaction(ctx context.Context, amount int, ta return ConvertEntTransactionToModelTransactionResponse(trns), nil } -func (repo *EntRepository) UpdateTransaction(ctx context.Context, transactionID uuid.UUID, amount int, target string, tags []*uuid.UUID, groupID *uuid.UUID, requestID *uuid.UUID) (*TransactionResponse, error) { +func (repo *EntRepository) UpdateTransaction( + ctx context.Context, transactionID uuid.UUID, amount int, target string, + tags []*uuid.UUID, groupID *uuid.UUID, requestID *uuid.UUID, +) (*TransactionResponse, error) { tx, err := repo.client.Tx(ctx) if err != nil { return nil, err @@ -366,7 +376,9 @@ func ConvertEntTransactionToModelTransaction(transaction *ent.Transaction) *Tran } } -func ConvertEntTransactionToModelTransactionResponse(transaction *ent.Transaction) *TransactionResponse { +func ConvertEntTransactionToModelTransactionResponse( + transaction *ent.Transaction, +) *TransactionResponse { tags := lo.Map(transaction.Edges.Tag, func(t *ent.Tag, index int) *Tag { return ConvertEntTagToModelTag(t) }) diff --git a/model/transaction_impl_test.go b/model/transaction_impl_test.go index 6242f0a1..b97d9c2b 100644 --- a/model/transaction_impl_test.go +++ b/model/transaction_impl_test.go @@ -51,13 +51,22 @@ func TestEntRepository_GetTransactions(t *testing.T) { t.Parallel() // Create user - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create Transactions amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), nil, nil, nil, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + nil, nil, + nil, user.ID) require.NoError(t, err) tx1, err := repo.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) @@ -91,13 +100,22 @@ func TestEntRepository_GetTransactions(t *testing.T) { t.Parallel() // Create user - user, err := repo2.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo2.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create Transactions amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) - request, err := repo2.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), nil, nil, nil, user.ID) + request, err := repo2.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + nil, nil, + nil, user.ID) require.NoError(t, err) tx1, err := repo2.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) @@ -131,12 +149,21 @@ func TestEntRepository_GetTransactions(t *testing.T) { t.Parallel() // Create user - user, err := repo3.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo3.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create Transactions target := random.AlphaNumeric(t, 20) - request, err := repo3.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), nil, nil, nil, user.ID) + request, err := repo3.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + nil, nil, + nil, user.ID) require.NoError(t, err) tx1, err := repo3.CreateTransaction(ctx, 100, target, nil, nil, &request.ID) @@ -172,17 +199,30 @@ func TestEntRepository_GetTransactions(t *testing.T) { ctx := context.Background() // Create user - user, err := repo4.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + // nolint:contextcheck + user, err := repo4.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create Transactions target := random.AlphaNumeric(t, 20) - request, err := repo4.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), nil, nil, nil, user.ID) + // nolint:contextcheck + request, err := repo4.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + nil, nil, + nil, user.ID) require.NoError(t, err) + // nolint:contextcheck tx1, err := repo4.CreateTransaction(ctx, 100, target, nil, nil, &request.ID) require.NoError(t, err) time.Sleep(1 * time.Second) + // nolint:contextcheck tx2, err := repo4.CreateTransaction(ctx, 10000, target, nil, nil, &request.ID) require.NoError(t, err) @@ -191,6 +231,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { query := TransactionQuery{ Sort: &sort, } + // nolint:contextcheck got, err := repo4.GetTransactions(ctx, query) assert.NoError(t, err) if assert.Len(t, got, 2) { @@ -211,13 +252,22 @@ func TestEntRepository_GetTransactions(t *testing.T) { t.Parallel() // Create user - user, err := repo5.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo5.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create Transactions amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) - request, err := repo5.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), nil, nil, nil, user.ID) + request, err := repo5.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + nil, nil, + nil, user.ID) require.NoError(t, err) tx1, err := repo5.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) @@ -251,14 +301,23 @@ func TestEntRepository_GetTransactions(t *testing.T) { t.Parallel() // Create user - user, err := repo6.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo6.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create Transactions amount := random.Numeric(t, 100000) target1 := random.AlphaNumeric(t, 20) target2 := random.AlphaNumeric(t, 20) - request, err := repo6.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), nil, nil, nil, user.ID) + request, err := repo6.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + nil, nil, + nil, user.ID) require.NoError(t, err) tx, err := repo6.CreateTransaction(ctx, amount, target1, nil, nil, &request.ID) @@ -285,13 +344,22 @@ func TestEntRepository_GetTransactions(t *testing.T) { t.Parallel() // Create user - user, err := repo7.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo7.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create Transactions amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) - request, err := repo7.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), nil, nil, nil, user.ID) + request, err := repo7.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + nil, nil, + nil, user.ID) require.NoError(t, err) _, err = repo7.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) @@ -324,7 +392,11 @@ func TestEntRepository_GetTransactions(t *testing.T) { t.Parallel() // Create user - user, err := repo8.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo8.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create Transactions @@ -332,7 +404,12 @@ func TestEntRepository_GetTransactions(t *testing.T) { target := random.AlphaNumeric(t, 20) tag, err := repo8.CreateTag(ctx, random.AlphaNumeric(t, 20)) require.NoError(t, err) - request, err := repo8.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), nil, nil, nil, user.ID) + request, err := repo8.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + nil, nil, + nil, user.ID) require.NoError(t, err) _, err = repo8.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) @@ -343,7 +420,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { Tag: &tag.Name, } - tx, err := repo8.CreateTransaction(ctx, amount, target, []*uuid.UUID{&tag.ID}, nil, &request.ID) + tx, err := repo8.CreateTransaction( + ctx, + amount, target, + []*uuid.UUID{&tag.ID}, nil, &request.ID) require.NoError(t, err) got, err := repo8.GetTransactions(ctx, query) @@ -361,16 +441,29 @@ func TestEntRepository_GetTransactions(t *testing.T) { t.Parallel() // Create user - user, err := repo9.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo9.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create Transactions amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) budget := random.Numeric(t, 100000) - group, err := repo9.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &budget) + group, err := repo9.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &budget) require.NoError(t, err) - request, err := repo9.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), nil, nil, nil, user.ID) + request, err := repo9.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + nil, nil, + nil, user.ID) require.NoError(t, err) _, err = repo9.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) @@ -399,13 +492,22 @@ func TestEntRepository_GetTransactions(t *testing.T) { t.Parallel() // Create user - user, err := repo10.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo10.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create Transactions amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) - request, err := repo10.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), nil, nil, nil, user.ID) + request, err := repo10.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + nil, nil, + nil, user.ID) require.NoError(t, err) _, err = repo10.CreateTransaction(ctx, amount, target, nil, nil, nil) @@ -451,13 +553,22 @@ func TestEntRepository_GetTransaction(t *testing.T) { ctx := context.Background() // Create user - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create Transactions amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) - _, err = repo.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), nil, nil, nil, user.ID) + _, err = repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + nil, nil, + nil, user.ID) require.NoError(t, err) tx, err := repo.CreateTransaction(ctx, amount, target, nil, nil, nil) @@ -489,7 +600,11 @@ func TestEntRepository_CreateTransaction(t *testing.T) { amount := random.Numeric(t, 100000) // Create user - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create tag @@ -497,15 +612,27 @@ func TestEntRepository_CreateTransaction(t *testing.T) { require.NoError(t, err) // Create group - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &amount) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &amount) require.NoError(t, err) // Create Transactions target := random.AlphaNumeric(t, 20) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), nil, nil, nil, user.ID) - require.NoError(t, err) - - tx, err := repo.CreateTransaction(ctx, amount, target, []*uuid.UUID{&tag.ID}, &group.ID, &request.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + nil, nil, + nil, user.ID) + require.NoError(t, err) + + tx, err := repo.CreateTransaction( + ctx, + amount, target, + []*uuid.UUID{&tag.ID}, &group.ID, &request.ID) assert.NoError(t, err) if assert.NotNil(t, tx) { assert.Equal(t, amount, tx.Amount) @@ -530,16 +657,29 @@ func TestEntRepository_CreateTransaction(t *testing.T) { amount := random.Numeric(t, 100000) // Create user - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create group - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &amount) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &amount) require.NoError(t, err) // Create Transactions target := random.AlphaNumeric(t, 20) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), nil, nil, nil, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + nil, nil, + nil, user.ID) require.NoError(t, err) tx, err := repo.CreateTransaction(ctx, amount, target, nil, &group.ID, &request.ID) @@ -564,7 +704,11 @@ func TestEntRepository_CreateTransaction(t *testing.T) { amount := random.Numeric(t, 100000) // Create user - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create tag @@ -573,10 +717,18 @@ func TestEntRepository_CreateTransaction(t *testing.T) { // Create Transactions target := random.AlphaNumeric(t, 20) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), nil, nil, nil, user.ID) - require.NoError(t, err) - - tx, err := repo.CreateTransaction(ctx, amount, target, []*uuid.UUID{&tag.ID}, nil, &request.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + nil, nil, + nil, user.ID) + require.NoError(t, err) + + tx, err := repo.CreateTransaction( + ctx, + amount, target, + []*uuid.UUID{&tag.ID}, nil, &request.ID) assert.NoError(t, err) if assert.NotNil(t, tx) { assert.Equal(t, amount, tx.Amount) @@ -639,7 +791,11 @@ func TestEntRepository_UpdateTransaction(t *testing.T) { amount := random.Numeric(t, 100000) // Create user - user, err := repo.CreateUser(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 20), random.Numeric(t, 1) == 0) + user, err := repo.CreateUser( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 20), + random.Numeric(t, 1) == 0) require.NoError(t, err) // Create tag @@ -647,15 +803,27 @@ func TestEntRepository_UpdateTransaction(t *testing.T) { require.NoError(t, err) // Create group - group, err := repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &amount) + group, err := repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &amount) require.NoError(t, err) // Create Transactions target := random.AlphaNumeric(t, 20) - request, err := repo.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), nil, nil, nil, user.ID) + request, err := repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + nil, nil, + nil, user.ID) require.NoError(t, err) - tx, err := repo.CreateTransaction(ctx, amount, target, []*uuid.UUID{&tag.ID}, &group.ID, &request.ID) + tx, err := repo.CreateTransaction( + ctx, + amount, target, + []*uuid.UUID{&tag.ID}, &group.ID, &request.ID) require.NoError(t, err) // Update Transactions @@ -666,15 +834,27 @@ func TestEntRepository_UpdateTransaction(t *testing.T) { require.NoError(t, err) // Create group - group, err = repo.CreateGroup(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), &amount) + group, err = repo.CreateGroup( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + &amount) require.NoError(t, err) // Create Transactions target = random.AlphaNumeric(t, 20) - request, err = repo.CreateRequest(ctx, random.AlphaNumeric(t, 20), random.AlphaNumeric(t, 30), nil, nil, nil, user.ID) - require.NoError(t, err) - - tx, err = repo.UpdateTransaction(ctx, tx.ID, amount, target, []*uuid.UUID{&tag.ID}, &group.ID, &request.ID) + request, err = repo.CreateRequest( + ctx, + random.AlphaNumeric(t, 20), + random.AlphaNumeric(t, 30), + nil, nil, + nil, user.ID) + require.NoError(t, err) + + tx, err = repo.UpdateTransaction( + ctx, + tx.ID, amount, target, + []*uuid.UUID{&tag.ID}, &group.ID, &request.ID) assert.NoError(t, err) if assert.NotNil(t, tx) { assert.Equal(t, amount, tx.Amount) diff --git a/model/user.go b/model/user.go index 03e6ccb2..f8252ca2 100644 --- a/model/user.go +++ b/model/user.go @@ -23,5 +23,7 @@ type UserRepository interface { GetUserByID(ctx context.Context, userID uuid.UUID) (*User, error) GetUserByName(ctx context.Context, name string) (*User, error) GetUsers(ctx context.Context) ([]*User, error) - UpdateUser(ctx context.Context, userID uuid.UUID, name string, dn string, admin bool) (*User, error) + UpdateUser( + ctx context.Context, userID uuid.UUID, name string, dn string, admin bool, + ) (*User, error) } diff --git a/model/user_impl.go b/model/user_impl.go index d14fb451..99739d15 100644 --- a/model/user_impl.go +++ b/model/user_impl.go @@ -10,7 +10,9 @@ import ( "github.com/traPtitech/Jomon/ent/user" ) -func (repo *EntRepository) CreateUser(ctx context.Context, name string, dn string, admin bool) (*User, error) { +func (repo *EntRepository) CreateUser( + ctx context.Context, name string, dn string, admin bool, +) (*User, error) { u, err := repo.client.User. Create(). SetName(name). @@ -58,7 +60,9 @@ func (repo *EntRepository) GetUsers(ctx context.Context) ([]*User, error) { return modelusers, nil } -func (repo *EntRepository) UpdateUser(ctx context.Context, userID uuid.UUID, name string, dn string, admin bool) (*User, error) { +func (repo *EntRepository) UpdateUser( + ctx context.Context, userID uuid.UUID, name string, dn string, admin bool, +) (*User, error) { u, err := repo.client.User. UpdateOneID(userID). SetName(name). diff --git a/router/admin_test.go b/router/admin_test.go index 712d46ed..cca30cc1 100644 --- a/router/admin_test.go +++ b/router/admin_test.go @@ -124,7 +124,10 @@ func TestHandler_PostAdmin(t *testing.T) { admin := []uuid.UUID{uuid.New()} e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/admins", strings.NewReader(`["`+admin[0].String()+`"]`)) + req, err := http.NewRequest( + http.MethodPost, + "/api/admins", + strings.NewReader(`["`+admin[0].String()+`"]`)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -149,7 +152,10 @@ func TestHandler_PostAdmin(t *testing.T) { adminID := uuid.New() e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/admins", strings.NewReader(`["`+adminID.String()+`"]`)) + req, err := http.NewRequest( + http.MethodPost, + "/api/admins", + strings.NewReader(`["`+adminID.String()+`"]`)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -177,7 +183,10 @@ func TestHandler_PostAdmin(t *testing.T) { adminID := uuid.New() e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/admins", strings.NewReader(`["`+adminID.String()+`"]`)) + req, err := http.NewRequest( + http.MethodPost, + "/api/admins", + strings.NewReader(`["`+adminID.String()+`"]`)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -210,7 +219,10 @@ func TestHandler_DeleteAdmin(t *testing.T) { admin := []uuid.UUID{uuid.New()} e := echo.New() - req, err := http.NewRequest(http.MethodDelete, "/api/admins", strings.NewReader(`["`+admin[0].String()+`"]`)) + req, err := http.NewRequest( + http.MethodDelete, + "/api/admins", + strings.NewReader(`["`+admin[0].String()+`"]`)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -235,7 +247,10 @@ func TestHandler_DeleteAdmin(t *testing.T) { adminID := uuid.New() e := echo.New() - req, err := http.NewRequest(http.MethodDelete, "/api/admins", strings.NewReader(`["`+adminID.String()+`"]`)) + req, err := http.NewRequest( + http.MethodDelete, + "/api/admins", + strings.NewReader(`["`+adminID.String()+`"]`)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -261,7 +276,10 @@ func TestHandler_DeleteAdmin(t *testing.T) { ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, "/api/admins", strings.NewReader(`["invalid"]`)) + req, err := http.NewRequest( + http.MethodDelete, + "/api/admins", + strings.NewReader(`["invalid"]`)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -281,7 +299,10 @@ func TestHandler_DeleteAdmin(t *testing.T) { adminID := uuid.New() e := echo.New() - req, err := http.NewRequest(http.MethodDelete, "/api/admins", strings.NewReader(`["`+adminID.String()+`"]`)) + req, err := http.NewRequest( + http.MethodDelete, + "/api/admins", + strings.NewReader(`["`+adminID.String()+`"]`)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() diff --git a/router/auth.go b/router/auth.go index 6ebb1a2c..e67626bf 100644 --- a/router/auth.go +++ b/router/auth.go @@ -49,7 +49,9 @@ func (h Handlers) AuthCallback(c echo.Context) error { codeVerifier, ok := sess.Values[sessionCodeVerifierKey].(string) if !ok { - return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("code_verifier is not found in session")) + return echo.NewHTTPError( + http.StatusInternalServerError, + fmt.Errorf("code_verifier is not found in session")) } res, err := service.RequestAccessToken(code, codeVerifier) @@ -68,7 +70,8 @@ func (h Handlers) AuthCallback(c echo.Context) error { modelUser, err = h.Repository.GetUserByName(c.Request().Context(), u.Name) if err != nil { if ent.IsNotFound(err) { - modelUser, err = h.Repository.CreateUser(c.Request().Context(), u.Name, u.DisplayName, false) + modelUser, err = h.Repository.CreateUser( + c.Request().Context(), u.Name, u.DisplayName, false) if err != nil { h.Logger.Error("failed to create user", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) @@ -113,7 +116,9 @@ func (h Handlers) GeneratePKCE(c echo.Context) error { sess.Values[sessionCodeVerifierKey] = codeVerifier codeVerifierHash := sha256.Sum256([]byte(codeVerifier)) - encoder := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_").WithPadding(base64.NoPadding) + encoder := base64. + NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"). + WithPadding(base64.NoPadding) codeChallengeMethod := "S256" @@ -122,8 +127,13 @@ func (h Handlers) GeneratePKCE(c echo.Context) error { h.Logger.Error("failed to save session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } + // nolint:lll + to := fmt.Sprintf( + "%s/oauth2/authorize?response_type=code&client_id=%s&code_challenge=%s&code_challenge_method=%s", + service.TraQBaseURL, service.JomonClientID, + encoder.EncodeToString(codeVerifierHash[:]), codeChallengeMethod) - return c.Redirect(http.StatusFound, fmt.Sprintf("%s/oauth2/authorize?response_type=code&client_id=%s&code_challenge=%s&code_challenge_method=%s", service.TraQBaseURL, service.JomonClientID, encoder.EncodeToString(codeVerifierHash[:]), codeChallengeMethod)) + return c.Redirect(http.StatusFound, to) } var randSrcPool = sync.Pool{ @@ -145,7 +155,10 @@ const ( func randAlphabetAndNumberString(n int) string { b := make([]byte, n) - randSrc := randSrcPool.Get().(*rand.Rand) + randSrc, ok := randSrcPool.Get().(*rand.Rand) + if !ok { + panic("failed to get rand source") + } cache, remain := randSrc.Int64(), rs6LetterIdxMax for i := n - 1; i >= 0; { if remain == 0 { diff --git a/router/auth_test.go b/router/auth_test.go index a4c8baa3..44a85d82 100644 --- a/router/auth_test.go +++ b/router/auth_test.go @@ -14,7 +14,9 @@ func TestHandler_GetCodeChallenge(t *testing.T) { codeVerifier := "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk" codeVerifierHash := sha256.Sum256([]byte(codeVerifier)) - encoder := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_").WithPadding(base64.NoPadding) + encoder := base64. + NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"). + WithPadding(base64.NoPadding) challenge := encoder.EncodeToString(codeVerifierHash[:]) assert.Equal(t, "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM", challenge) diff --git a/router/file.go b/router/file.go index 01dc6174..a3ec7cd1 100644 --- a/router/file.go +++ b/router/file.go @@ -66,7 +66,9 @@ func (h Handlers) PostFile(c echo.Context) error { mimetype := reqfile.Header.Get(echo.HeaderContentType) if !acceptedMimeTypes[mimetype] { h.Logger.Info("requested unsupported mime type", zap.String("mime-type", mimetype)) - return echo.NewHTTPError(http.StatusUnsupportedMediaType, fmt.Errorf("unsupported media type")) + return echo.NewHTTPError( + http.StatusUnsupportedMediaType, + fmt.Errorf("unsupported media type")) } src, err := reqfile.Open() diff --git a/router/file_test.go b/router/file_test.go index c9f35f37..f03904a2 100644 --- a/router/file_test.go +++ b/router/file_test.go @@ -24,6 +24,7 @@ import ( "github.com/traPtitech/Jomon/testutil/random" ) +// nolint:lll var testJpeg = `/9j/4AAQSkZJRgABAQIAOAA4AAD/2wBDAP//////////////////////////////////////////////////////////////////////////////////////2wBDAf//////////////////////////////////////////////////////////////////////////////////////wAARCAABAAEDAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwBKBH//2Q` func TestHandlers_PostFile(t *testing.T) { diff --git a/router/group.go b/router/group.go index 07f83b4e..4a719d3d 100644 --- a/router/group.go +++ b/router/group.go @@ -177,7 +177,9 @@ func (h Handlers) PutGroup(c echo.Context) error { } ctx := c.Request().Context() - updated, err := h.Repository.UpdateGroup(ctx, groupID, group.Name, group.Description, group.Budget) + updated, err := h.Repository.UpdateGroup( + ctx, + groupID, group.Name, group.Description, group.Budget) if err != nil { h.Logger.Error("failed to update group in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) diff --git a/router/group_test.go b/router/group_test.go index 087fb10f..ab786c3d 100644 --- a/router/group_test.go +++ b/router/group_test.go @@ -162,7 +162,13 @@ func TestHandlers_PostGroup(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/groups", strings.NewReader(fmt.Sprintf(`{"name":"%s","description":"%s","budget":%d}`, group.Name, group.Description, *group.Budget))) + reqBody := fmt.Sprintf( + `{"name":"%s","description":"%s","budget":%d}`, + group.Name, group.Description, *group.Budget) + req, err := http.NewRequest( + http.MethodPost, + "/api/groups", + strings.NewReader(reqBody)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -200,7 +206,11 @@ func TestHandlers_PostGroup(t *testing.T) { budget := random.Numeric(t, 1000000) e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/groups", strings.NewReader(fmt.Sprintf(`{"name":"test","description":"test","budget":%d}`, budget))) + reqBody := fmt.Sprintf(`{"name":"test","description":"test","budget":%d}`, budget) + req, err := http.NewRequest( + http.MethodPost, + "/api/groups", + strings.NewReader(reqBody)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -416,7 +426,10 @@ func TestHandlers_GetGroupDetail(t *testing.T) { ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/groups/%s", uuid.Nil.String()), nil) + req, err := http.NewRequest( + http.MethodGet, + fmt.Sprintf("/api/groups/%s", uuid.Nil.String()), + nil) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -444,7 +457,10 @@ func TestHandlers_GetGroupDetail(t *testing.T) { errors.As(errors.New("unknown group id"), &resErr) e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/groups/%s", unknownGroupID), nil) + req, err := http.NewRequest( + http.MethodGet, + fmt.Sprintf("/api/groups/%s", unknownGroupID), + nil) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -644,7 +660,13 @@ func TestHandlers_PutGroup(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/groups/%s", group.ID.String()), strings.NewReader(fmt.Sprintf(`{"name":"%s","description":"%s","budget":%d}`, updated.Name, updated.Description, *updated.Budget))) + reqBody := fmt.Sprintf( + `{"name":"%s","description":"%s","budget":%d}`, + updated.Name, updated.Description, *updated.Budget) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/groups/%s", group.ID.String()), + strings.NewReader(reqBody)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -657,7 +679,10 @@ func TestHandlers_PutGroup(t *testing.T) { require.NoError(t, err) h.Repository.MockGroupRepository. EXPECT(). - UpdateGroup(c.Request().Context(), group.ID, updated.Name, updated.Description, updated.Budget). + UpdateGroup( + c.Request().Context(), + group.ID, updated.Name, + updated.Description, updated.Budget). Return(updated, nil) res := &GroupOverview{ @@ -706,7 +731,13 @@ func TestHandlers_PutGroup(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/groups/%s", group.ID.String()), strings.NewReader(fmt.Sprintf(`{"name":"%s","description":"%s","budget":%d}`, updated.Name, updated.Description, *updated.Budget))) + reqBody := fmt.Sprintf( + `{"name":"%s","description":"%s","budget":%d}`, + updated.Name, updated.Description, *updated.Budget) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/groups/%s", group.ID.String()), + strings.NewReader(reqBody)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -720,7 +751,9 @@ func TestHandlers_PutGroup(t *testing.T) { require.NoError(t, err) h.Repository.MockGroupRepository. EXPECT(). - UpdateGroup(c.Request().Context(), group.ID, updated.Name, updated.Description, updated.Budget). + UpdateGroup( + c.Request().Context(), + group.ID, updated.Name, updated.Description, updated.Budget). Return(nil, resErr) err = h.Handlers.PutGroup(c) @@ -737,7 +770,10 @@ func TestHandlers_PutGroup(t *testing.T) { ctrl := gomock.NewController(t) e := echo.New() - req, err := http.NewRequest(http.MethodPut, "/api/groups/invalid-uuid", strings.NewReader(`{"name":"test","description":"test","budget":1000000}`)) + req, err := http.NewRequest( + http.MethodPut, + "/api/groups/invalid-uuid", + strings.NewReader(`{"name":"test","description":"test","budget":1000000}`)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -775,7 +811,10 @@ func TestHandlers_DeleteGroup(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/groups/%s", group.ID.String()), nil) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/groups/%s", group.ID.String()), + nil) require.NoError(t, err) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -812,7 +851,10 @@ func TestHandlers_DeleteGroup(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/groups/%s", group.ID.String()), nil) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/groups/%s", group.ID.String()), + nil) require.NoError(t, err) rec := httptest.NewRecorder() c := e.NewContext(req, rec) @@ -863,7 +905,6 @@ func TestHandlers_PostMember(t *testing.T) { t.Parallel() t.Run("Success", func(t *testing.T) { - t.Parallel() ctrl := gomock.NewController(t) date := time.Now() @@ -892,7 +933,10 @@ func TestHandlers_PostMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("/api/groups/%s/members", group.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPost, + fmt.Sprintf("/api/groups/%s/members", group.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -924,7 +968,6 @@ func TestHandlers_PostMember(t *testing.T) { }) t.Run("InvalidUUID", func(t *testing.T) { - t.Parallel() ctrl := gomock.NewController(t) @@ -933,7 +976,10 @@ func TestHandlers_PostMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/groups/hoge/members", bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPost, + "/api/groups/hoge/members", + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -962,7 +1008,10 @@ func TestHandlers_PostMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("/api/groups/%s/members", uuid.Nil.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPost, + fmt.Sprintf("/api/groups/%s/members", uuid.Nil.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1005,7 +1054,10 @@ func TestHandlers_PostMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("/api/groups/%s/members", unknownGroupID), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPost, + fmt.Sprintf("/api/groups/%s/members", unknownGroupID), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1051,7 +1103,10 @@ func TestHandlers_PostMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("/api/groups/%s/members", group.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPost, + fmt.Sprintf("/api/groups/%s/members", group.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1106,7 +1161,10 @@ func TestHandlers_DeleteMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/groups/%s/members", group.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/groups/%s/members", group.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1146,7 +1204,10 @@ func TestHandlers_DeleteMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/groups/%s/members", uuid.Nil.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/groups/%s/members", uuid.Nil.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1189,7 +1250,10 @@ func TestHandlers_DeleteMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/groups/%s/members", unknownGroupID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/groups/%s/members", unknownGroupID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1235,7 +1299,10 @@ func TestHandlers_DeleteMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/groups/%s/members", group.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/groups/%s/members", group.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1270,7 +1337,10 @@ func TestHandlers_DeleteMember(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/groups/%s/members", invID), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/groups/%s/members", invID), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1293,7 +1363,6 @@ func TestHandlers_PostOwner(t *testing.T) { t.Parallel() t.Run("Success", func(t *testing.T) { - t.Parallel() ctrl := gomock.NewController(t) date := time.Now() @@ -1322,7 +1391,10 @@ func TestHandlers_PostOwner(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("/api/groups/%s/owners", group.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPost, + fmt.Sprintf("/api/groups/%s/owners", group.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1354,7 +1426,6 @@ func TestHandlers_PostOwner(t *testing.T) { }) t.Run("InvalidUUID", func(t *testing.T) { - t.Parallel() ctrl := gomock.NewController(t) @@ -1363,7 +1434,10 @@ func TestHandlers_PostOwner(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/groups/hoge/owners", bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPost, + "/api/groups/hoge/owners", + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1392,7 +1466,10 @@ func TestHandlers_PostOwner(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("/api/groups/%s/owners", uuid.Nil.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPost, + fmt.Sprintf("/api/groups/%s/owners", uuid.Nil.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1435,7 +1512,10 @@ func TestHandlers_PostOwner(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("/api/groups/%s/owners", unknownGroupID), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPost, + fmt.Sprintf("/api/groups/%s/owners", unknownGroupID), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1481,7 +1561,10 @@ func TestHandlers_PostOwner(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("/api/groups/%s/owners", group.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPost, + fmt.Sprintf("/api/groups/%s/owners", group.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1536,7 +1619,10 @@ func TestHandlers_DeleteOwner(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/groups/%s/owners", group.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/groups/%s/owners", group.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1566,7 +1652,10 @@ func TestHandlers_DeleteOwner(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/groups/%s/owners", uuid.Nil.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/groups/%s/owners", uuid.Nil.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1609,7 +1698,10 @@ func TestHandlers_DeleteOwner(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/groups/%s/owners", unknownGroupID), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/groups/%s/owners", unknownGroupID), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1653,7 +1745,10 @@ func TestHandlers_DeleteOwner(t *testing.T) { errors.As(errors.New("unknown owner id"), &resErr) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/groups/%s/owners", group.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/groups/%s/owners", group.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1687,7 +1782,10 @@ func TestHandlers_DeleteOwner(t *testing.T) { _, resErr := uuid.Parse(invID) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/groups/%s/owners", invID), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/groups/%s/owners", invID), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() diff --git a/router/middleware.go b/router/middleware.go index ccd9223d..44fe6e01 100644 --- a/router/middleware.go +++ b/router/middleware.go @@ -35,6 +35,7 @@ func (h Handlers) AccessLoggingMiddleware(logger *zap.Logger) echo.MiddlewareFun req := c.Request() res := c.Response() + latency := strconv.FormatFloat(stop.Sub(start).Seconds(), 'f', 9, 64) + "s" fields := []zapcore.Field{ zap.String("requestMethod", req.Method), zap.Int("status", res.Status), @@ -45,7 +46,7 @@ func (h Handlers) AccessLoggingMiddleware(logger *zap.Logger) echo.MiddlewareFun zap.String("requestUrl", req.URL.String()), zap.String("requestSize", req.Header.Get(echo.HeaderContentLength)), zap.String("responseSize", strconv.FormatInt(res.Size, 10)), - zap.String("latency", strconv.FormatFloat(stop.Sub(start).Seconds(), 'f', 9, 64)+"s"), + zap.String("latency", latency), } httpCode := res.Status switch { @@ -118,7 +119,12 @@ func (h Handlers) CheckRequestCreatorMiddleware(next echo.HandlerFunc) echo.Hand return echo.NewHTTPError(http.StatusInternalServerError, err) } - creator := sess.Values[sessionRequestCreatorKey].(uuid.UUID) + creator, ok := sess.Values[sessionRequestCreatorKey].(uuid.UUID) + if !ok { + return echo.NewHTTPError( + http.StatusInternalServerError, + "session request creator key is not set") + } if creator != user.ID { return echo.NewHTTPError(http.StatusForbidden, "you are not creator") } @@ -141,7 +147,12 @@ func (h Handlers) CheckAdminOrRequestCreatorMiddleware(next echo.HandlerFunc) ec return echo.NewHTTPError(http.StatusInternalServerError, err) } - creator := sess.Values[sessionRequestCreatorKey].(uuid.UUID) + creator, ok := sess.Values[sessionRequestCreatorKey].(uuid.UUID) + if !ok { + return echo.NewHTTPError( + http.StatusInternalServerError, + "session request creator key is not set") + } if creator != user.ID && !user.Admin { return echo.NewHTTPError(http.StatusForbidden, "you are not admin or creator") } @@ -198,7 +209,12 @@ func (h Handlers) CheckFileCreatorMiddleware(next echo.HandlerFunc) echo.Handler return echo.NewHTTPError(http.StatusInternalServerError, err) } - creator := sess.Values[sessionFileCreatorKey].(uuid.UUID) + creator, ok := sess.Values[sessionFileCreatorKey].(uuid.UUID) + if !ok { + return echo.NewHTTPError( + http.StatusInternalServerError, + "session file creator key is not set") + } if creator != user.ID { return echo.NewHTTPError(http.StatusForbidden, "you are not creator") } @@ -219,7 +235,12 @@ func (h Handlers) CheckAdminOrFileCreatorMiddleware(next echo.HandlerFunc) echo. return echo.NewHTTPError(http.StatusInternalServerError, err) } - creator := sess.Values[sessionFileCreatorKey].(uuid.UUID) + creator, ok := sess.Values[sessionFileCreatorKey].(uuid.UUID) + if !ok { + return echo.NewHTTPError( + http.StatusInternalServerError, + "session file creator key is not set") + } if creator != user.ID && !user.Admin { return echo.NewHTTPError(http.StatusForbidden, "you are not admin or creator") } diff --git a/router/request.go b/router/request.go index 2438626b..c8ba9e72 100644 --- a/router/request.go +++ b/router/request.go @@ -264,17 +264,23 @@ func (h Handlers) PostRequest(c echo.Context) error { group, err = h.Repository.GetGroup(ctx, *req.Group) if err != nil { if ent.IsNotFound(err) { - h.Logger.Info("could not find group in repository", zap.String("ID", req.Group.String())) + h.Logger.Info( + "could not find group in repository", + zap.String("ID", req.Group.String())) return echo.NewHTTPError(http.StatusNotFound, err) } h.Logger.Error("failed to get group from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } } - request, err := h.Repository.CreateRequest(ctx, req.Title, req.Content, tags, targets, group, req.CreatedBy) + request, err := h.Repository.CreateRequest( + ctx, + req.Title, req.Content, tags, targets, group, req.CreatedBy) if err != nil { if ent.IsNotFound(err) { - h.Logger.Info("could not find request in repository", zap.String("ID", req.CreatedBy.String())) + h.Logger.Info( + "could not find request in repository", + zap.String("ID", req.CreatedBy.String())) return echo.NewHTTPError(http.StatusNotFound, err) } h.Logger.Error("failed to create request in repository", zap.Error(err)) @@ -466,17 +472,23 @@ func (h Handlers) PutRequest(c echo.Context) error { group, err = h.Repository.GetGroup(ctx, *req.Group) if err != nil { if ent.IsNotFound(err) { - h.Logger.Info("could not find group in repository", zap.String("ID", req.Group.String())) + h.Logger.Info( + "could not find group in repository", + zap.String("ID", req.Group.String())) return echo.NewHTTPError(http.StatusNotFound, err) } h.Logger.Error("failed to get group from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } } - request, err := h.Repository.UpdateRequest(ctx, requestID, req.Title, req.Content, tags, targets, group) + request, err := h.Repository.UpdateRequest( + ctx, + requestID, req.Title, req.Content, tags, targets, group) if err != nil { if ent.IsNotFound(err) { - h.Logger.Info("could not find request in repository", zap.String("ID", requestID.String())) + h.Logger.Info( + "could not find request in repository", + zap.String("ID", requestID.String())) return echo.NewHTTPError(http.StatusNotFound, err) } h.Logger.Error("failed to update request in repository", zap.Error(err)) @@ -584,7 +596,9 @@ func (h Handlers) PostComment(c echo.Context) error { comment, err := h.Repository.CreateComment(ctx, req.Comment, requestID, user.ID) if err != nil { if ent.IsNotFound(err) { - h.Logger.Info("could not find request in repository", zap.String("ID", requestID.String())) + h.Logger.Info( + "could not find request in repository", + zap.String("ID", requestID.String())) return echo.NewHTTPError(http.StatusNotFound, err) } h.Logger.Error("failed to create comment in repository", zap.Error(err)) @@ -632,7 +646,9 @@ func (h Handlers) PutStatus(c echo.Context) error { request, err := h.Repository.GetRequest(ctx, requestID) if err != nil { if ent.IsNotFound(err) { - h.Logger.Info("could not find request in repository", zap.String("ID", requestID.String())) + h.Logger.Info( + "could not find request in repository", + zap.String("ID", requestID.String())) return echo.NewHTTPError(http.StatusNotFound, err) } h.Logger.Error("failed to get request from repository", zap.Error(err)) @@ -645,7 +661,10 @@ func (h Handlers) PutStatus(c echo.Context) error { } if req.Comment == "" { if !IsAbleNoCommentChangeStatus(req.Status, request.Status) { - return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("unable to change %v to %v without comment", request.Status.String(), req.Status.String())) + err := fmt.Errorf( + "unable to change %v to %v without comment", + request.Status.String(), req.Status.String()) + return echo.NewHTTPError(http.StatusBadRequest, err) } } @@ -661,7 +680,10 @@ func (h Handlers) PutStatus(c echo.Context) error { if u.Admin { if !IsAbleAdminChangeState(req.Status, request.Status) { h.Logger.Info("admin unable to change status") - return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("admin unable to change %v to %v", request.Status.String(), req.Status.String())) + err := fmt.Errorf( + "admin unable to change %v to %v", + request.Status.String(), req.Status.String()) + return echo.NewHTTPError(http.StatusBadRequest, err) } if req.Status == model.Submitted && request.Status == model.Accepted { targets, err := h.Repository.GetRequestTargets(ctx, requestID) @@ -686,7 +708,10 @@ func (h Handlers) PutStatus(c echo.Context) error { if !u.Admin && user.ID == request.CreatedBy { if !IsAbleCreatorChangeStatus(req.Status, request.Status) { h.Logger.Info("creator unable to change status") - return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("creator unable to change %v to %v", request.Status.String(), req.Status.String())) + err := fmt.Errorf( + "creator unable to change %v to %v", + request.Status.String(), req.Status.String()) + return echo.NewHTTPError(http.StatusBadRequest, err) } } @@ -728,28 +753,19 @@ func (h Handlers) PutStatus(c echo.Context) error { } func IsAbleNoCommentChangeStatus(status, latestStatus model.Status) bool { - if status == model.FixRequired && latestStatus == model.Submitted || - status == model.Rejected && latestStatus == model.Submitted || - status == model.Submitted && latestStatus == model.Accepted { - return false - } - return true + return !(status == model.FixRequired && latestStatus == model.Submitted) && + !(status == model.Rejected && latestStatus == model.Submitted) && + !(status == model.Submitted && latestStatus == model.Accepted) } func IsAbleCreatorChangeStatus(status, latestStatus model.Status) bool { - if status == model.Submitted && latestStatus == model.FixRequired { - return true - } - return false + return status == model.Submitted && latestStatus == model.FixRequired } func IsAbleAdminChangeState(status, latestStatus model.Status) bool { - if status == model.Rejected && latestStatus == model.Submitted || + return status == model.Rejected && latestStatus == model.Submitted || status == model.Submitted && latestStatus == model.FixRequired || status == model.Accepted && latestStatus == model.Submitted || status == model.Submitted && latestStatus == model.Accepted || - status == model.FixRequired && latestStatus == model.Submitted { - return true - } - return false + status == model.FixRequired && latestStatus == model.Submitted } diff --git a/router/request_test.go b/router/request_test.go index 076041cb..b0f2a797 100644 --- a/router/request_test.go +++ b/router/request_test.go @@ -214,7 +214,10 @@ func TestHandlers_GetRequests(t *testing.T) { requests := []*model.RequestResponse{request1} e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/requests?until=%s", date2str), nil) + req, err := http.NewRequest( + http.MethodGet, + fmt.Sprintf("/api/requests?until=%s", date2str), + nil) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -273,7 +276,10 @@ func TestHandlers_GetRequests(t *testing.T) { requests := []*model.RequestResponse{request1} e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/requests?since=%s", date2str), nil) + req, err := http.NewRequest( + http.MethodGet, + fmt.Sprintf("/api/requests?since=%s", date2str), + nil) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -344,7 +350,10 @@ func TestHandlers_GetRequests(t *testing.T) { requests := []*model.RequestResponse{request1} e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/requests?tag=%s", tag1.Name), nil) + req, err := http.NewRequest( + http.MethodGet, + fmt.Sprintf("/api/requests?tag=%s", tag1.Name), + nil) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -476,7 +485,11 @@ func TestHandlers_PostRequest(t *testing.T) { require.NoError(t, err) h.Repository.MockRequestRepository. EXPECT(). - CreateRequest(c.Request().Context(), reqRequest.Title, reqRequest.Content, tags, targets, group, reqRequest.CreatedBy). + CreateRequest( + c.Request().Context(), + reqRequest.Title, reqRequest.Content, + tags, targets, + group, reqRequest.CreatedBy). Return(request, nil) res := &RequestResponse{ @@ -562,7 +575,11 @@ func TestHandlers_PostRequest(t *testing.T) { Return(tag, nil) h.Repository.MockRequestRepository. EXPECT(). - CreateRequest(c.Request().Context(), reqRequest.Title, reqRequest.Content, tags, targets, group, reqRequest.CreatedBy). + CreateRequest( + c.Request().Context(), + reqRequest.Title, reqRequest.Content, + tags, targets, + group, reqRequest.CreatedBy). Return(request, nil) res := &RequestResponse{ @@ -654,7 +671,11 @@ func TestHandlers_PostRequest(t *testing.T) { Return(group, nil) h.Repository.MockRequestRepository. EXPECT(). - CreateRequest(c.Request().Context(), reqRequest.Title, reqRequest.Content, tags, targets, group, reqRequest.CreatedBy). + CreateRequest( + c.Request().Context(), + reqRequest.Title, reqRequest.Content, + tags, targets, + group, reqRequest.CreatedBy). Return(request, nil) res := &RequestResponse{ @@ -753,7 +774,11 @@ func TestHandlers_PostRequest(t *testing.T) { require.NoError(t, err) h.Repository.MockRequestRepository. EXPECT(). - CreateRequest(c.Request().Context(), reqRequest.Title, reqRequest.Content, tags, []*model.RequestTarget{target}, group, reqRequest.CreatedBy). + CreateRequest( + c.Request().Context(), + reqRequest.Title, reqRequest.Content, + tags, []*model.RequestTarget{target}, + group, reqRequest.CreatedBy). Return(request, nil) tgov := &TargetOverview{ @@ -927,7 +952,11 @@ func TestHandlers_PostRequest(t *testing.T) { require.NoError(t, err) h.Repository.MockRequestRepository. EXPECT(). - CreateRequest(c.Request().Context(), reqRequest.Title, reqRequest.Content, tags, targets, group, reqRequest.CreatedBy). + CreateRequest( + c.Request().Context(), + reqRequest.Title, reqRequest.Content, + tags, targets, + group, reqRequest.CreatedBy). Return(nil, resErr) err = h.Handlers.PostRequest(c) @@ -967,7 +996,10 @@ func TestHandlers_GetRequest(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/requests/%s", request.ID.String()), nil) + req, err := http.NewRequest( + http.MethodGet, + fmt.Sprintf("/api/requests/%s", request.ID.String()), + nil) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1058,7 +1090,10 @@ func TestHandlers_GetRequest(t *testing.T) { comments := []*model.Comment{comment1, comment2} e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/requests/%s", request.ID.String()), nil) + req, err := http.NewRequest( + http.MethodGet, + fmt.Sprintf("/api/requests/%s", request.ID.String()), + nil) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1166,7 +1201,10 @@ func TestHandlers_GetRequest(t *testing.T) { CreatedBy: uuid.New(), } e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/requests/%s", request.ID.String()), nil) + req, err := http.NewRequest( + http.MethodGet, + fmt.Sprintf("/api/requests/%s", request.ID.String()), + nil) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1273,7 +1311,10 @@ func TestHandlers_GetRequest(t *testing.T) { unknownID := uuid.New() e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/requests/%s", unknownID.String()), nil) + req, err := http.NewRequest( + http.MethodGet, + fmt.Sprintf("/api/requests/%s", unknownID.String()), + nil) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1348,7 +1389,10 @@ func TestHandlers_PutRequest(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s", request.ID), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s", request.ID), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1361,7 +1405,12 @@ func TestHandlers_PutRequest(t *testing.T) { assert.NoError(t, err) h.Repository.MockRequestRepository. EXPECT(). - UpdateRequest(c.Request().Context(), request.ID, reqRequest.Title, reqRequest.Content, tags, targets, group). + UpdateRequest( + c.Request().Context(), + request.ID, + reqRequest.Title, reqRequest.Content, + tags, targets, + group). Return(updateRequest, nil) h.Repository.MockCommentRepository. EXPECT(). @@ -1453,7 +1502,10 @@ func TestHandlers_PutRequest(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1475,7 +1527,12 @@ func TestHandlers_PutRequest(t *testing.T) { Return(tag2, nil) h.Repository.MockRequestRepository. EXPECT(). - UpdateRequest(c.Request().Context(), request.ID, reqRequest.Title, reqRequest.Content, tags, targets, group). + UpdateRequest( + c.Request().Context(), + request.ID, + reqRequest.Title, reqRequest.Content, + tags, targets, + group). Return(updateRequest, nil) h.Repository.MockCommentRepository. EXPECT(). @@ -1601,7 +1658,10 @@ func TestHandlers_PutRequest(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1615,7 +1675,12 @@ func TestHandlers_PutRequest(t *testing.T) { h.Repository.MockRequestRepository. EXPECT(). - UpdateRequest(c.Request().Context(), request.ID, reqRequest.Title, reqRequest.Content, tags, targets, group). + UpdateRequest( + c.Request().Context(), + request.ID, + reqRequest.Title, reqRequest.Content, + tags, targets, + group). Return(updateRequest, nil) h.Repository.MockCommentRepository. EXPECT(). @@ -1718,7 +1783,10 @@ func TestHandlers_PutRequest(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1736,7 +1804,12 @@ func TestHandlers_PutRequest(t *testing.T) { Return(group, nil) h.Repository.MockRequestRepository. EXPECT(). - UpdateRequest(c.Request().Context(), request.ID, reqRequest.Title, reqRequest.Content, tags, targets, group). + UpdateRequest( + c.Request().Context(), + request.ID, + reqRequest.Title, reqRequest.Content, + tags, targets, + group). Return(updateRequest, nil) h.Repository.MockCommentRepository. EXPECT(). @@ -1837,7 +1910,10 @@ func TestHandlers_PutRequest(t *testing.T) { comments := []*model.Comment{comment1, comment2} e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s", request.ID), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s", request.ID), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1850,7 +1926,12 @@ func TestHandlers_PutRequest(t *testing.T) { assert.NoError(t, err) h.Repository.MockRequestRepository. EXPECT(). - UpdateRequest(c.Request().Context(), request.ID, reqRequest.Title, reqRequest.Content, tags, targets, group). + UpdateRequest( + c.Request().Context(), + request.ID, + reqRequest.Title, reqRequest.Content, + tags, targets, + group). Return(updateRequest, nil) h.Repository.MockCommentRepository. EXPECT(). @@ -1966,7 +2047,10 @@ func TestHandlers_PutRequest(t *testing.T) { var group *model.Group e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s", unknownID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s", unknownID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -1982,7 +2066,12 @@ func TestHandlers_PutRequest(t *testing.T) { require.NoError(t, err) h.Repository.MockRequestRepository. EXPECT(). - UpdateRequest(c.Request().Context(), unknownID, reqRequest.Title, reqRequest.Content, tags, targets, group). + UpdateRequest( + c.Request().Context(), + unknownID, + reqRequest.Title, reqRequest.Content, + tags, targets, + group). Return(nil, resErr) err = h.Handlers.PutRequest(c) @@ -2022,7 +2111,10 @@ func TestHandlers_PutRequest(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -2081,7 +2173,10 @@ func TestHandlers_PutRequest(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -2153,7 +2248,10 @@ func TestHandlers_PutStatus(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -2261,7 +2359,10 @@ func TestHandlers_PutStatus(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -2369,7 +2470,10 @@ func TestHandlers_PutStatus(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -2477,7 +2581,10 @@ func TestHandlers_PutStatus(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -2585,7 +2692,10 @@ func TestHandlers_PutStatus(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -2700,7 +2810,10 @@ func TestHandlers_PutStatus(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -2799,7 +2912,10 @@ func TestHandlers_PutStatus(t *testing.T) { }`, invalidStatus, random.AlphaNumeric(t, 20)) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), strings.NewReader(reqStatus)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + strings.NewReader(reqStatus)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -2826,7 +2942,9 @@ func TestHandlers_PutStatus(t *testing.T) { } resErr := echo.NewHTTPError(http.StatusBadRequest) - resErrMessage := echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("invalid Status %s", invalidStatus)) + resErrMessage := echo.NewHTTPError( + http.StatusBadRequest, + fmt.Sprintf("invalid Status %s", invalidStatus)) resErrMessage.Internal = fmt.Errorf("invalid Status %s", invalidStatus) resErr.Message = resErrMessage @@ -2858,7 +2976,10 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", "hoge"), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", "hoge"), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -2914,7 +3035,10 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", uuid.Nil), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", uuid.Nil), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -2978,7 +3102,10 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -3034,7 +3161,10 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -3103,7 +3233,10 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -3135,7 +3268,10 @@ func TestHandlers_PutStatus(t *testing.T) { GetRequest(ctx, request.ID). Return(request, nil) - resErr := fmt.Errorf("unable to change %v to %v without comment", request.Status.String(), reqStatus.Status.String()) + resErr := fmt.Errorf( + "unable to change %v to %v without comment", + request.Status.String(), + reqStatus.Status.String()) err = h.Handlers.PutStatus(c) if assert.Error(t, err) { @@ -3172,7 +3308,10 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -3204,7 +3343,10 @@ func TestHandlers_PutStatus(t *testing.T) { GetRequest(ctx, request.ID). Return(request, nil) - resErr := fmt.Errorf("unable to change %v to %v without comment", request.Status.String(), reqStatus.Status.String()) + resErr := fmt.Errorf( + "unable to change %v to %v without comment", + request.Status.String(), + reqStatus.Status.String()) err = h.Handlers.PutStatus(c) if assert.Error(t, err) { @@ -3241,7 +3383,10 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -3273,7 +3418,10 @@ func TestHandlers_PutStatus(t *testing.T) { GetRequest(ctx, request.ID). Return(request, nil) - resErr := fmt.Errorf("unable to change %v to %v without comment", request.Status.String(), reqStatus.Status.String()) + resErr := fmt.Errorf( + "unable to change %v to %v without comment", + request.Status.String(), + reqStatus.Status.String()) err = h.Handlers.PutStatus(c) if assert.Error(t, err) { @@ -3311,7 +3459,10 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -3386,7 +3537,10 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -3422,7 +3576,10 @@ func TestHandlers_PutStatus(t *testing.T) { GetUserByID(ctx, user.ID). Return(user, nil) - resErr := fmt.Errorf("admin unable to change %v to %v", request.Status.String(), reqStatus.Status.String()) + resErr := fmt.Errorf( + "admin unable to change %v to %v", + request.Status.String(), + reqStatus.Status.String()) err = h.Handlers.PutStatus(c) if assert.Error(t, err) { @@ -3467,7 +3624,10 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -3545,7 +3705,10 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -3581,7 +3744,9 @@ func TestHandlers_PutStatus(t *testing.T) { GetUserByID(ctx, user.ID). Return(user, nil) - resErr := fmt.Errorf("creator unable to change %v to %v", request.Status.String(), reqStatus.Status.String()) + resErr := fmt.Errorf( + "creator unable to change %v to %v", + request.Status.String(), reqStatus.Status.String()) err = h.Handlers.PutStatus(c) if assert.Error(t, err) { @@ -3619,7 +3784,10 @@ func TestHandlers_PutStatus(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/requests/%s/status", request.ID.String()), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/requests/%s/status", request.ID.String()), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -3687,7 +3855,10 @@ func TestHandlers_PutStatus(t *testing.T) { // require.NoError(t, err) // e := echo.New() -// req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("/api/requests/%s/comments", requestID), bytes.NewReader(reqBody)) +// req, err := http.NewRequest( +// http.MethodPost, +// fmt.Sprintf("/api/requests/%s/comments", requestID), +// bytes.NewReader(reqBody)) // assert.NoError(t, err) // req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) // rec := httptest.NewRecorder() diff --git a/router/router.go b/router/router.go index d9d49401..21984a4b 100644 --- a/router/router.go +++ b/router/router.go @@ -53,8 +53,15 @@ func NewServer(h Handlers) *echo.Echo { apiRequestIDs := apiRequests.Group("/:requestID", retrieveRequestCreator) { apiRequestIDs.GET("", h.GetRequest) - apiRequestIDs.PUT("", h.PutRequest, middleware.BodyDump(service.WebhookEventHandler), h.CheckRequestCreatorMiddleware) - apiRequestIDs.POST("/comments", h.PostComment, middleware.BodyDump(service.WebhookEventHandler)) + apiRequestIDs.PUT( + "", + h.PutRequest, + middleware.BodyDump(service.WebhookEventHandler), + h.CheckRequestCreatorMiddleware) + apiRequestIDs.POST( + "/comments", + h.PostComment, + middleware.BodyDump(service.WebhookEventHandler)) apiRequestIDs.PUT("/status", h.PutStatus, h.CheckAdminOrRequestCreatorMiddleware) } } @@ -62,9 +69,17 @@ func NewServer(h Handlers) *echo.Echo { apiTransactions := api.Group("/transactions", h.CheckLoginMiddleware) { apiTransactions.GET("", h.GetTransactions) - apiTransactions.POST("", h.PostTransaction, middleware.BodyDump(service.WebhookEventHandler), h.CheckAdminMiddleware) + apiTransactions.POST( + "", + h.PostTransaction, + middleware.BodyDump(service.WebhookEventHandler), + h.CheckAdminMiddleware) apiTransactions.GET("/:transactionID", h.GetTransaction) - apiTransactions.PUT("/:transactionID", h.PutTransaction, middleware.BodyDump(service.WebhookEventHandler), h.CheckAdminMiddleware) + apiTransactions.PUT( + "/:transactionID", + h.PutTransaction, + middleware.BodyDump(service.WebhookEventHandler), + h.CheckAdminMiddleware) } apiFiles := api.Group("/files", h.CheckLoginMiddleware) diff --git a/router/tag_test.go b/router/tag_test.go index eebc751d..38140a7d 100644 --- a/router/tag_test.go +++ b/router/tag_test.go @@ -256,7 +256,10 @@ func TestHandlers_PutTag(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/tags/%s", tag.ID), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/tags/%s", tag.ID), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -307,7 +310,10 @@ func TestHandlers_PutTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/tags/%s", tag.ID), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/tags/%s", tag.ID), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -391,7 +397,10 @@ func TestHandlers_PutTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/tags/%s", tag.ID), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/tags/%s", tag.ID), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -405,7 +414,10 @@ func TestHandlers_PutTag(t *testing.T) { err = h.Handlers.PutTag(c) if assert.Error(t, err) { - assert.Equal(t, echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid tag ID")), err) + assert.Equal( + t, + echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid tag ID")), + err) } }) } @@ -433,7 +445,10 @@ func TestHandlers_DeleteTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/tags/%s", tag.ID), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/tags/%s", tag.ID), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -475,7 +490,10 @@ func TestHandlers_DeleteTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/tags/%s", tag.ID), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/tags/%s", tag.ID), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -559,7 +577,10 @@ func TestHandlers_DeleteTag(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/tags/%s", tag.ID), bytes.NewReader(reqBody)) + req, err := http.NewRequest( + http.MethodDelete, + fmt.Sprintf("/api/tags/%s", tag.ID), + bytes.NewReader(reqBody)) assert.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -573,7 +594,10 @@ func TestHandlers_DeleteTag(t *testing.T) { err = h.Handlers.DeleteTag(c) if assert.Error(t, err) { - assert.Equal(t, echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid tag ID")), err) + assert.Equal( + t, + echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid tag ID")), + err) } }) } diff --git a/router/transaction.go b/router/transaction.go index 69ff4af9..819209c3 100644 --- a/router/transaction.go +++ b/router/transaction.go @@ -82,7 +82,8 @@ func (h Handlers) GetTransactions(c echo.Context) error { } if limitI < 0 { h.Logger.Info("received negative limit", zap.Int("limit", limitI)) - return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("negative limit(=%d) is invalid", limitI)) + err := fmt.Errorf("negative limit(=%d) is invalid", limitI) + return echo.NewHTTPError(http.StatusBadRequest, err) } limit = limitI } @@ -95,7 +96,8 @@ func (h Handlers) GetTransactions(c echo.Context) error { } if offsetI < 0 { h.Logger.Info("received negative offset", zap.Int("offset", offsetI)) - return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("negative offset(=%d) is invalid", offsetI)) + err := fmt.Errorf("negative offset(=%d) is invalid", offsetI) + return echo.NewHTTPError(http.StatusBadRequest, err) } offset = offsetI } @@ -185,7 +187,9 @@ func (h Handlers) PostTransaction(c echo.Context) error { h.Logger.Info("target is nil") return echo.NewHTTPError(http.StatusBadRequest, "target is nil") } - created, err := h.Repository.CreateTransaction(ctx, tx.Amount, *target, tx.Tags, tx.Group, tx.Request) + created, err := h.Repository.CreateTransaction( + ctx, + tx.Amount, *target, tx.Tags, tx.Group, tx.Request) if err != nil { h.Logger.Error("failed to create transaction in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) @@ -284,12 +288,16 @@ func (h Handlers) PutTransaction(c echo.Context) error { var tx *TransactionOverviewWithOneTarget if err := c.Bind(&tx); err != nil { - h.Logger.Info("could not get transaction overview with one target from request", zap.Error(err)) + h.Logger.Info( + "could not get transaction overview with one target from request", + zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } ctx := c.Request().Context() - updated, err := h.Repository.UpdateTransaction(ctx, txID, tx.Amount, tx.Target, tx.Tags, tx.Group, tx.Request) + updated, err := h.Repository.UpdateTransaction( + ctx, + txID, tx.Amount, tx.Target, tx.Tags, tx.Group, tx.Request) if err != nil { h.Logger.Error("failed to update transaction in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) diff --git a/router/transaction_test.go b/router/transaction_test.go index 4bf2e5d3..0275a2e8 100644 --- a/router/transaction_test.go +++ b/router/transaction_test.go @@ -1,7 +1,6 @@ package router import ( - "bytes" "encoding/json" "fmt" "net/http" @@ -425,7 +424,10 @@ func TestHandlers_GetTransactions(t *testing.T) { txs := []*model.TransactionResponse{tx1, tx2} e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/transactions?target=%s", target1), nil) + req, err := http.NewRequest( + http.MethodGet, + fmt.Sprintf("/api/transactions?target=%s", target1), + nil) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -525,7 +527,10 @@ func TestHandlers_GetTransactions(t *testing.T) { require.NoError(t, err) e := echo.New() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/transactions?since=%s&until=%s", "2020-01-01", "2020-01-02"), nil) + req, err := http.NewRequest( + http.MethodGet, + fmt.Sprintf("/api/transactions?since=%s&until=%s", "2020-01-01", "2020-01-02"), + nil) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -630,7 +635,13 @@ func TestHandlers_PostTransaction(t *testing.T) { group := tx1.Group.ID e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/transactions", bytes.NewBuffer([]byte(fmt.Sprintf(`{"amount": %d, "targets": ["%s"], "tags": ["%s"], "group": "%s"}`, tx1.Amount, tx1.Target, tag.ID, group)))) + reqBody := fmt.Sprintf( + `{"amount": %d, "targets": ["%s"], "tags": ["%s"], "group": "%s"}`, + tx1.Amount, tx1.Target, tag.ID, group) + req, err := http.NewRequest( + http.MethodPost, + "/api/transactions", + strings.NewReader(reqBody)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -724,7 +735,7 @@ func TestHandlers_PostTransaction(t *testing.T) { request := &model.RequestDetail{ ID: uuid.New(), - Status: model.Status(model.Accepted), + Status: model.Accepted, Title: random.AlphaNumeric(t, 20), Content: random.AlphaNumeric(t, 50), Comments: []*model.Comment{}, @@ -738,7 +749,13 @@ func TestHandlers_PostTransaction(t *testing.T) { } e := echo.New() - req, err := http.NewRequest(http.MethodPost, "/api/transactions", bytes.NewBuffer([]byte(fmt.Sprintf(`{"amount": %d, "targets": ["%s"], "tags": ["%s"], "group": "%s", "request": "%s"}`, tx.Amount, tx.Target, tag.ID, group, request.ID)))) + reqBody := fmt.Sprintf( + `{"amount": %d, "targets": ["%s"], "tags": ["%s"], "group": "%s", "request": "%s"}`, + tx.Amount, tx.Target, tag.ID, group, request.ID) + req, err := http.NewRequest( + http.MethodPost, + "/api/transactions", + strings.NewReader(reqBody)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -749,7 +766,10 @@ func TestHandlers_PostTransaction(t *testing.T) { h.Repository.MockTransactionRepository. EXPECT(). - CreateTransaction(c.Request().Context(), tx.Amount, tx.Target, tags, &group, &request.ID). + CreateTransaction( + c.Request().Context(), + tx.Amount, tx.Target, + tags, &group, &request.ID). Return(tx, nil) var resOverview Transaction @@ -947,7 +967,13 @@ func TestHandlers_PutTransaction(t *testing.T) { }) e := echo.New() - req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("/api/transactions/%s", tx.ID), strings.NewReader(fmt.Sprintf(`{"amount": %d, "target": "%s", "tags": ["%s"]}`, updated.Amount, updated.Target, updatedTag.ID))) + reqBody := fmt.Sprintf( + `{"amount": %d, "target": "%s", "tags": ["%s"]}`, + updated.Amount, updated.Target, updatedTag.ID) + req, err := http.NewRequest( + http.MethodPut, + fmt.Sprintf("/api/transactions/%s", tx.ID), + strings.NewReader(reqBody)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -960,7 +986,10 @@ func TestHandlers_PutTransaction(t *testing.T) { h.Repository.MockTransactionRepository. EXPECT(). - UpdateTransaction(c.Request().Context(), tx.ID, updated.Amount, updated.Target, updatedTags, nil, nil). + UpdateTransaction( + c.Request().Context(), + tx.ID, updated.Amount, updated.Target, + updatedTags, nil, nil). Return(updated, nil) var resOverview Transaction diff --git a/router/user.go b/router/user.go index 5a6cf5c6..dcdbb32c 100644 --- a/router/user.go +++ b/router/user.go @@ -63,7 +63,9 @@ func (h Handlers) UpdateUserInfo(c echo.Context) error { return echo.NewHTTPError(http.StatusBadRequest, err) } - updated, err := h.Repository.UpdateUser(c.Request().Context(), user.ID, newUser.Name, newUser.DisplayName, newUser.Admin) + updated, err := h.Repository.UpdateUser( + c.Request().Context(), + user.ID, newUser.Name, newUser.DisplayName, newUser.Admin) if err != nil { h.Logger.Error("failed to update user in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) diff --git a/router/user_test.go b/router/user_test.go index e95c9764..685298e5 100644 --- a/router/user_test.go +++ b/router/user_test.go @@ -179,7 +179,9 @@ func TestHandlers_UpdateUserInfo(t *testing.T) { Return(user, nil) h.Repository.MockUserRepository. EXPECT(). - UpdateUser(c.Request().Context(), user.ID, updateUser.Name, updateUser.DisplayName, updateUser.Admin). + UpdateUser( + c.Request().Context(), + user.ID, updateUser.Name, updateUser.DisplayName, updateUser.Admin). Return(updateUser, nil) if assert.NoError(t, h.Handlers.UpdateUserInfo(c)) { @@ -226,7 +228,9 @@ func TestHandlers_UpdateUserInfo(t *testing.T) { mocErr := errors.New("failed to get users.") h.Repository.MockUserRepository. EXPECT(). - UpdateUser(c.Request().Context(), user.ID, updateUser.Name, updateUser.DisplayName, updateUser.Admin). + UpdateUser( + c.Request().Context(), + user.ID, updateUser.Name, updateUser.DisplayName, updateUser.Admin). Return(nil, mocErr) err = h.Handlers.UpdateUserInfo(c) @@ -346,8 +350,10 @@ func TestHandlers_GetMe(t *testing.T) { err = h.Handlers.GetMe(c) if assert.Error(t, err) { - assert.Equal(t, echo.NewHTTPError(http.StatusInternalServerError, "failed to get user info"), err) + assert.Equal( + t, + echo.NewHTTPError(http.StatusInternalServerError, "failed to get user info"), + err) } }) - } diff --git a/service/traq.go b/service/traq.go index fba242c9..4c5976cf 100644 --- a/service/traq.go +++ b/service/traq.go @@ -33,7 +33,7 @@ func RequestAccessToken(code, codeVerifier string) (Authority, error) { form.Set("code_verifier", codeVerifier) reqBody := strings.NewReader(form.Encode()) - req, err := http.NewRequest("POST", TraQBaseURL+"/oauth2/token", reqBody) + req, err := http.NewRequest(http.MethodPost, TraQBaseURL+"/oauth2/token", reqBody) if err != nil { return Authority{}, err } @@ -61,7 +61,7 @@ type TraQUser struct { } func FetchTraQUserInfo(token string) (TraQUser, error) { //GetMe - req, err := http.NewRequest("GET", TraQBaseURL+"/users/me", nil) + req, err := http.NewRequest(http.MethodGet, TraQBaseURL+"/users/me", nil) if err != nil { return TraQUser{}, err } diff --git a/service/webhook.go b/service/webhook.go index d91e7ef8..6bd431cb 100644 --- a/service/webhook.go +++ b/service/webhook.go @@ -87,9 +87,16 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) { } splitedPath := strings.Split(c.Request().URL.Path, "/") - message += fmt.Sprintf("## :comment:[申請](%s/requests/%s)", "https://jomon.trap.jp", splitedPath[3]) + message += fmt.Sprintf( + "## :comment:[申請](%s/requests/%s)", + "https://jomon.trap.jp", + splitedPath[3]) message += "に対する" - message += fmt.Sprintf("[コメント](%s/requests/%s/comments/%s)", "https://jomon.trap.jp", splitedPath[3], resApp.ID) + message += fmt.Sprintf( + "[コメント](%s/requests/%s/comments/%s)", + "https://jomon.trap.jp", + splitedPath[3], + resApp.ID) message += "が作成されました\n\n" message += resApp.Comment + "\n" } else { @@ -104,7 +111,11 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) { message += "## :receipt:申請が更新されました\n" } - message += fmt.Sprintf("### [%s](%s/applications/%s)", resApp.Title, "https://jomon.trap.jp", resApp.ID) + "\n" + message += fmt.Sprintf( + "### [%s](%s/applications/%s)\n", + resApp.Title, + "https://jomon.trap.jp", + resApp.ID) amount := 0 for _, target := range resApp.Targets { @@ -117,7 +128,6 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) { } if resApp.Tags != nil && len(resApp.Tags) != 0 { - tags := lo.Map(resApp.Tags, func(tag *Tag, index int) string { return tag.Name }) @@ -134,15 +144,27 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) { return } if c.Request().Method == http.MethodPost { - message += fmt.Sprintf("## :scroll:[入出金記録](%s/transactions/%s)が新規作成されました\n", "https://jomon.trap.jp", resApp.ID) + message += fmt.Sprintf( + "## :scroll:[入出金記録](%s/transactions/%s)が新規作成されました\n", + "https://jomon.trap.jp", + resApp.ID) } else if c.Request().Method == http.MethodPut { - message += fmt.Sprintf("## :scroll:[入出金記録](%s/transactions/%s)が修正されました\n", "https://jomon.trap.jp", resApp.ID) + message += fmt.Sprintf( + "## :scroll:[入出金記録](%s/transactions/%s)が修正されました\n", + "https://jomon.trap.jp", + resApp.ID) } if len(resApps) == 1 { if resApp.Amount < 0 { - message += fmt.Sprintf("- `%s`への支払い\n - 支払い金額: %d円\n", resApp.Target, -resApp.Amount) + message += fmt.Sprintf( + "- `%s`への支払い\n - 支払い金額: %d円\n", + resApp.Target, + -resApp.Amount) } else { - message += fmt.Sprintf("- `%s`からの振込\n - 受け取り金額: %d円\n", resApp.Target, resApp.Amount) + message += fmt.Sprintf( + "- `%s`からの振込\n - 受け取り金額: %d円\n", + resApp.Target, + resApp.Amount) } } else { targets := make([]string, len(resApps)) @@ -150,11 +172,18 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) { targets[i] = resApp.Target } if resApp.Amount < 0 { - message += fmt.Sprintf("- %sへの支払い\n - 支払い金額: 計%d円(一人当たりへの支払い金額: %d円)\n", strings.Join(targets, " "), -len(resApps)*resApp.Amount, -resApp.Amount) + message += fmt.Sprintf( + "- %sへの支払い\n - 支払い金額: 計%d円(一人当たりへの支払い金額: %d円)\n", + strings.Join(targets, " "), + -len(resApps)*resApp.Amount, + -resApp.Amount) } else { - message += fmt.Sprintf("- %sからの振込\n - 受け取り金額: 計%d円(一人当たりからの受け取り金額: %d円)\n", strings.Join(targets, " "), len(resApps)*resApp.Amount, resApp.Amount) + message += fmt.Sprintf( + "- %sからの振込\n - 受け取り金額: 計%d円(一人当たりからの受け取り金額: %d円)\n", + strings.Join(targets, " "), + len(resApps)*resApp.Amount, + resApp.Amount) } - } if resApp.Group != nil { message += fmt.Sprintf("- 関連するグループ: %s\n", resApp.Group.Name) diff --git a/storage/swift.go b/storage/swift.go index f4bfeb29..ff16d4a8 100644 --- a/storage/swift.go +++ b/storage/swift.go @@ -1,6 +1,7 @@ package storage import ( + "errors" "io" "github.com/ncw/swift" @@ -11,7 +12,10 @@ type Swift struct { conn *swift.Connection } -func NewSwiftStorage(container string, userName string, apiKey string, tenant string, tenantID string, authURL string) (*Swift, error) { +func NewSwiftStorage( + container string, userName string, apiKey string, + tenant string, tenantID string, authURL string, +) (*Swift, error) { conn := &swift.Connection{ AuthUrl: authURL, UserName: userName, @@ -43,7 +47,7 @@ func (s *Swift) Save(filename string, src io.Reader) error { func (s *Swift) Open(filename string) (io.ReadCloser, error) { file, _, err := s.conn.ObjectOpen(s.container, filename, true, nil) if err != nil { - if err == swift.ObjectNotFound { + if errors.Is(err, swift.ObjectNotFound) { return nil, ErrFileNotFound } return nil, err @@ -54,7 +58,7 @@ func (s *Swift) Open(filename string) (io.ReadCloser, error) { func (s *Swift) Delete(filename string) error { err := s.conn.ObjectDelete(s.container, filename) if err != nil { - if err == swift.ObjectNotFound { + if errors.Is(err, swift.ObjectNotFound) { return ErrFileNotFound } return err diff --git a/testutil/random/ramdom.go b/testutil/random/ramdom.go index 94b2043e..4c1bca38 100644 --- a/testutil/random/ramdom.go +++ b/testutil/random/ramdom.go @@ -33,18 +33,21 @@ func Numeric64(t *testing.T, max int64) int64 { } func AlphaNumericSlice(t *testing.T, length int, max int64) []string { + t.Helper() return lo.Times(length, func(index int) string { return AlphaNumeric(t, int(max)) }) } func NumericSlice(t *testing.T, length int, max int) []int { + t.Helper() return lo.Times(length, func(index int) int { return Numeric(t, max) }) } func Numeric64Slice(t *testing.T, length int, max int64) []int64 { + t.Helper() return lo.Times(length, func(index int) int64 { return Numeric64(t, max) })