-
Notifications
You must be signed in to change notification settings - Fork 0
やりたいことボードのusecase層以下を実装 #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/3/base
Are you sure you want to change the base?
Changes from all commits
6c21bd0
f5e3264
c1c1a37
380d7f6
63a937a
bfea40e
e77a907
7ded141
e8ab473
f1f6d6e
a53b1e0
97048bf
af285f7
4bcf2c3
651fca9
7c0a566
3f420b1
a7f6368
f040add
ea185d9
72509ac
1a419a6
27b7929
cc13e1a
f80c6d0
f0abdec
88962cd
471ac90
f9dd192
d9759d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| package wishboard | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "wantum/pkg/domain/entity/wishboard" | ||
| "wantum/pkg/domain/repository" | ||
| fileservice "wantum/pkg/domain/service/file" | ||
| userservice "wantum/pkg/domain/service/user" | ||
| wishboardservice "wantum/pkg/domain/service/wishboard" | ||
| "wantum/pkg/tlog" | ||
| "wantum/pkg/werrors" | ||
| ) | ||
|
|
||
| type Interactor interface { | ||
| CreateNewWishBoard(ctx context.Context, authID, title string, backgroundImage []byte) (*wishboard.Entity, error) | ||
| GetMyWishBoards(ctx context.Context, authID string) (wishboard.EntitySlice, error) | ||
| UpdateTitle(ctx context.Context, wishBoardID int, title, authID string) error | ||
| UpdateBackgroundImage(ctx context.Context, wishBoardID int, backgroundImage []byte, authID string) error | ||
| DeleteWishBoard(ctx context.Context, wishBoardID int, authID string) error | ||
| } | ||
|
|
||
| type interactor struct { | ||
| masterTxManager repository.MasterTxManager | ||
| userService userservice.Service | ||
| wishBoardService wishboardservice.Service | ||
| fileService fileservice.Service | ||
| } | ||
|
|
||
| func New(masterTxManager repository.MasterTxManager, userService userservice.Service, wishBoardService wishboardservice.Service, fileService fileservice.Service) Interactor { | ||
| return &interactor{ | ||
| masterTxManager: masterTxManager, | ||
| userService: userService, | ||
| wishBoardService: wishBoardService, | ||
| fileService: fileService, | ||
| } | ||
| } | ||
|
|
||
| func (i *interactor) CreateNewWishBoard(ctx context.Context, authID, title string, backgroundImage []byte) (*wishboard.Entity, error) { | ||
| if title == "" { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 感想ですー! |
||
| err := errors.New("Error occurred when board title validation.") | ||
| tlog.PrintErrorLogWithCtx(ctx, err) | ||
| return nil, werrors.FromConstant(err, werrors.BadRequest) | ||
| } | ||
|
|
||
| var wishBoardEntity *wishboard.Entity | ||
| err := i.masterTxManager.Transaction(ctx, func(ctx context.Context, masterTx repository.MasterTx) error { | ||
| userEntity, err := i.userService.GetByAuthID(ctx, masterTx, authID) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| // 背景画像を保存し、URLを取得 | ||
| backgroundImageURL, err := i.fileService.UploadImageToLocalFolder(backgroundImage) | ||
sana37 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| // TODO: 招待URLの自動生成 | ||
| inviteURL := "hoge" | ||
|
Comment on lines
+59
to
+60
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. uuidで自動生成するだけやと思うから、ここで実装しちゃっても良いかもね?w
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あと、実装する場合は、
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. service層で実装する了解!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
招待を受け付けるエンドポイントをRESTで用意してあげる必要があるね! |
||
|
|
||
| wishBoardEntity, err = i.wishBoardService.Create(ctx, masterTx, title, backgroundImageURL, inviteURL, userEntity.ID) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return nil, werrors.Stack(err) | ||
| } | ||
|
|
||
| return wishBoardEntity, nil | ||
| } | ||
|
|
||
| func (i *interactor) GetMyWishBoards(ctx context.Context, authID string) (wishboard.EntitySlice, error) { | ||
| var wishBoardSlice wishboard.EntitySlice | ||
| err := i.masterTxManager.Transaction(ctx, func(ctx context.Context, masterTx repository.MasterTx) error { | ||
| userEntity, err := i.userService.GetByAuthID(ctx, masterTx, authID) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| // 自分が所属しているWishBoardのリストを取得 | ||
| wishBoardSlice, err = i.wishBoardService.GetMyBoards(ctx, masterTx, userEntity.ID) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return nil, werrors.Stack(err) | ||
| } | ||
|
|
||
| return wishBoardSlice, nil | ||
| } | ||
|
|
||
| func (i *interactor) UpdateTitle(ctx context.Context, wishBoardID int, title, authID string) error { | ||
| if title == "" { | ||
| err := errors.New("Error occurred when board title validation.") | ||
| tlog.PrintErrorLogWithCtx(ctx, err) | ||
| return werrors.FromConstant(err, werrors.BadRequest) | ||
| } | ||
|
|
||
| err := i.masterTxManager.Transaction(ctx, func(ctx context.Context, masterTx repository.MasterTx) error { | ||
| userEntity, err := i.userService.GetByAuthID(ctx, masterTx, authID) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| // WishBoardが存在するか確認 | ||
| wishBoardEntity, err := i.wishBoardService.GetByPK(ctx, masterTx, wishBoardID) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
Comment on lines
+112
to
+116
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WishBoardが存在するかのチェックが抜けてる気がする!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. いや、これは |
||
|
|
||
| // ユーザがWishBoardのメンバーでなければPermissionDenied | ||
| isMember, err := i.wishBoardService.IsMember(ctx, masterTx, userEntity.ID, wishBoardEntity.ID) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
| if !isMember { | ||
| err := errors.New("Error occurred when update board title. cause: permission denied") | ||
| tlog.PrintErrorLogWithCtx(ctx, err) | ||
| return werrors.FromConstant(err, werrors.WishBoardPermissionDenied) | ||
| } | ||
|
|
||
| if err := i.wishBoardService.UpdateTitle(ctx, masterTx, wishBoardEntity.ID, title); err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (i *interactor) UpdateBackgroundImage(ctx context.Context, wishBoardID int, backgroundImage []byte, authID string) error { | ||
| err := i.masterTxManager.Transaction(ctx, func(ctx context.Context, masterTx repository.MasterTx) error { | ||
| userEntity, err := i.userService.GetByAuthID(ctx, masterTx, authID) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| // WishBoardが存在するか確認 | ||
| wishBoardEntity, err := i.wishBoardService.GetByPK(ctx, masterTx, wishBoardID) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| // ユーザがWishBoardのメンバーでなければPermissionDenied | ||
| isMember, err := i.wishBoardService.IsMember(ctx, masterTx, userEntity.ID, wishBoardEntity.ID) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
| if !isMember { | ||
| err := errors.New("Error occurred when update board background image. cause: permission denied") | ||
| tlog.PrintErrorLogWithCtx(ctx, err) | ||
| return werrors.FromConstant(err, werrors.WishBoardPermissionDenied) | ||
| } | ||
|
|
||
| // 背景画像を保存し、URLを取得 | ||
| backgroundImageURL, err := i.fileService.UploadImageToLocalFolder(backgroundImage) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| if err := i.wishBoardService.UpdateBackgroundImageURL(ctx, masterTx, wishBoardEntity.ID, backgroundImageURL); err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (i *interactor) DeleteWishBoard(ctx context.Context, wishBoardID int, authID string) error { | ||
| err := i.masterTxManager.Transaction(ctx, func(ctx context.Context, masterTx repository.MasterTx) error { | ||
| userEntity, err := i.userService.GetByAuthID(ctx, masterTx, authID) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| // WishBoardが存在するか確認 | ||
| wishBoardEntity, err := i.wishBoardService.GetByPK(ctx, masterTx, wishBoardID) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| // ユーザがWishBoardのメンバーでなければPermissionDenied | ||
| isMember, err := i.wishBoardService.IsMember(ctx, masterTx, userEntity.ID, wishBoardEntity.ID) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
| if !isMember { | ||
| err := errors.New("Error occurred when delete board. cause: permission denied") | ||
| tlog.PrintErrorLogWithCtx(ctx, err) | ||
| return werrors.FromConstant(err, werrors.WishBoardPermissionDenied) | ||
| } | ||
|
|
||
| if err := i.wishBoardService.Delete(ctx, masterTx, wishBoardEntity.ID); err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package wishboard | ||
|
|
||
| import "time" | ||
|
|
||
| type Entity struct { | ||
| ID int | ||
| Title string | ||
| BackgroundImageURL string | ||
| InviteURL string | ||
| UserID int | ||
| CreatedAt *time.Time | ||
| UpdatedAt *time.Time | ||
| DeletedAt *time.Time | ||
| // WishCategories | ||
| } | ||
|
|
||
| type EntitySlice []*Entity |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package userwishboard | ||
|
|
||
| import ( | ||
| "context" | ||
| "wantum/pkg/domain/repository" | ||
| ) | ||
|
|
||
| type Repository interface { | ||
| Insert(ctx context.Context, masterTx repository.MasterTx, userID, wishBoardID int) error | ||
| Exists(ctx context.Context, masterTx repository.MasterTx, userID, wishBoardID int) (bool, error) | ||
| SelectWishBoardIDsByUserID(ctx context.Context, masterTx repository.MasterTx, userID int) ([]int, error) | ||
| Delete(ctx context.Context, masterTx repository.MasterTx, userID, wishBoardID int) error | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package wishboard | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
| "wantum/pkg/domain/entity/wishboard" | ||
| "wantum/pkg/domain/repository" | ||
| ) | ||
|
|
||
| type Repository interface { | ||
| Insert(ctx context.Context, masterTx repository.MasterTx, wishBoardEntity *wishboard.Entity) (*wishboard.Entity, error) | ||
| SelectByPK(ctx context.Context, masterTx repository.MasterTx, wishBoardID int) (*wishboard.Entity, error) | ||
| SelectByPKs(ctx context.Context, masterTx repository.MasterTx, wishBoardIDs []int) (wishboard.EntitySlice, error) | ||
| UpdateTitle(ctx context.Context, masterTx repository.MasterTx, wishBoardID int, title string, updatedAt *time.Time) error | ||
| UpdateBackgroundImageURL(ctx context.Context, masterTx repository.MasterTx, wishBoardID int, backgroundImageURL string, updatedAt *time.Time) error | ||
| Delete(ctx context.Context, masterTx repository.MasterTx, wishBoardEntity *wishboard.Entity) error | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| package wishboard | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
| "wantum/pkg/domain/entity/wishboard" | ||
| "wantum/pkg/domain/repository" | ||
| userwishboardrepository "wantum/pkg/domain/repository/userwishboard" | ||
| wishboardrepository "wantum/pkg/domain/repository/wishboard" | ||
| "wantum/pkg/werrors" | ||
| ) | ||
|
|
||
| type Service interface { | ||
| Create(ctx context.Context, masterTx repository.MasterTx, title, backgroundImageURL, inviteURL string, userID int) (*wishboard.Entity, error) | ||
| GetByPK(ctx context.Context, masterTx repository.MasterTx, wishBoardID int) (*wishboard.Entity, error) | ||
| GetMyBoards(ctx context.Context, masterTx repository.MasterTx, userID int) (wishboard.EntitySlice, error) | ||
| IsMember(ctx context.Context, masterTx repository.MasterTx, userID, wishBoardID int) (bool, error) | ||
| UpdateTitle(ctx context.Context, masterTx repository.MasterTx, wishBoardID int, title string) error | ||
| UpdateBackgroundImageURL(ctx context.Context, masterTx repository.MasterTx, wishBoardID int, backgroundImageURL string) error | ||
| Delete(ctx context.Context, masterTx repository.MasterTx, wishBoardID int) error | ||
| } | ||
|
|
||
| type service struct { | ||
| wishBoardRepository wishboardrepository.Repository | ||
| userWishBoardRepository userwishboardrepository.Repository | ||
| } | ||
|
|
||
| func New(wishBoardRepository wishboardrepository.Repository, userWishBoardRepository userwishboardrepository.Repository) Service { | ||
| return &service{ | ||
| wishBoardRepository: wishBoardRepository, | ||
| userWishBoardRepository: userWishBoardRepository, | ||
| } | ||
| } | ||
|
|
||
| func (s *service) Create(ctx context.Context, masterTx repository.MasterTx, title, backgroundImageURL, inviteURL string, userID int) (*wishboard.Entity, error) { | ||
| now := time.Now() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. たしかに!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sana37
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらで立てました。 |
||
|
|
||
| newWishBoard := &wishboard.Entity{ | ||
| Title: title, | ||
| BackgroundImageURL: backgroundImageURL, | ||
| InviteURL: inviteURL, | ||
| UserID: userID, | ||
| CreatedAt: &now, | ||
| UpdatedAt: &now, | ||
| } | ||
|
|
||
| createdWishBoard, err := s.wishBoardRepository.Insert(ctx, masterTx, newWishBoard) | ||
| if err != nil { | ||
| return nil, werrors.Stack(err) | ||
| } | ||
|
|
||
| if err := s.userWishBoardRepository.Insert(ctx, masterTx, userID, createdWishBoard.ID); err != nil { | ||
| return nil, werrors.Stack(err) | ||
| } | ||
|
|
||
| return createdWishBoard, nil | ||
| } | ||
|
|
||
| func (s *service) GetByPK(ctx context.Context, masterTx repository.MasterTx, wishBoardID int) (*wishboard.Entity, error) { | ||
| wishBoardEntity, err := s.wishBoardRepository.SelectByPK(ctx, masterTx, wishBoardID) | ||
| if err != nil { | ||
| return nil, werrors.Stack(err) | ||
| } | ||
| return wishBoardEntity, nil | ||
| } | ||
|
|
||
| func (s *service) GetMyBoards(ctx context.Context, masterTx repository.MasterTx, userID int) (wishboard.EntitySlice, error) { | ||
| wishBoardIDs, err := s.userWishBoardRepository.SelectWishBoardIDsByUserID(ctx, masterTx, userID) | ||
| if err != nil { | ||
| return nil, werrors.Stack(err) | ||
| } | ||
|
|
||
| if len(wishBoardIDs) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| wishBoardSlice, err := s.wishBoardRepository.SelectByPKs(ctx, masterTx, wishBoardIDs) | ||
| if err != nil { | ||
| return nil, werrors.Stack(err) | ||
| } | ||
|
|
||
| return wishBoardSlice, nil | ||
| } | ||
|
|
||
| func (s *service) IsMember(ctx context.Context, masterTx repository.MasterTx, userID, wishBoardID int) (bool, error) { | ||
| exists, err := s.userWishBoardRepository.Exists(ctx, masterTx, userID, wishBoardID) | ||
| if err != nil { | ||
| return false, werrors.Stack(err) | ||
| } | ||
| return exists, nil | ||
| } | ||
|
|
||
| func (s *service) UpdateTitle(ctx context.Context, masterTx repository.MasterTx, wishBoardID int, title string) error { | ||
| now := time.Now() | ||
|
|
||
| if err := s.wishBoardRepository.UpdateTitle(ctx, masterTx, wishBoardID, title, &now); err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *service) UpdateBackgroundImageURL(ctx context.Context, masterTx repository.MasterTx, wishBoardID int, backgroundImageURL string) error { | ||
| now := time.Now() | ||
|
|
||
| if err := s.wishBoardRepository.UpdateBackgroundImageURL(ctx, masterTx, wishBoardID, backgroundImageURL, &now); err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *service) Delete(ctx context.Context, masterTx repository.MasterTx, wishBoardID int) error { | ||
| now := time.Now() | ||
|
|
||
| wishBoardEntity := &wishboard.Entity{ | ||
| ID: wishBoardID, | ||
| UpdatedAt: &now, | ||
| DeletedAt: &now, | ||
| } | ||
|
|
||
| if err := s.wishBoardRepository.Delete(ctx, masterTx, wishBoardEntity); err != nil { | ||
| return werrors.Stack(err) | ||
| } | ||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ただの気づきです。。。
interactor内のメソッドの引数、確かにauthIDのがいいかもな...!わたしuserIDで実装してたけど、userIDってよく考えたら取れないのか...!!( ゚д゚)