-
Notifications
You must be signed in to change notification settings - Fork 0
Ownershipの実装 #63
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
Merged
Merged
Ownershipの実装 #63
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9ed2755
feat: ownership domain model
kavos113 0d028ff
feat: ownership usecase
kavos113 cbb16f4
mockgen
kavos113 d21cff2
chore : typo
kavos113 f57318b
feat: ownership repository
kavos113 5ce196d
feat: ownership handler
kavos113 75a445d
mockgen
kavos113 bcd9357
mockgen
kavos113 03419f0
chore: move converter
kavos113 396fe11
fix ignore
kavos113 19fff27
feat: ownership usecase test
kavos113 7791a97
feat: ownership handler test
kavos113 0eca2d6
feat: ownership repository test
kavos113 964cebf
fix: add test case
kavos113 8b93db5
fix: need item id to update ownership
kavos113 7c84caa
fix: check user id when create ownership
kavos113 930c801
fix: itemid is same as path paramater
kavos113 85fbdc4
fix: usecase paramater
kavos113 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,4 +26,7 @@ booQ-v3 | |
| !/uploads/.gitkeep | ||
| /uploads/* | ||
|
|
||
| .tool-versions | ||
| .tool-versions | ||
| .idea | ||
| cover.html | ||
| cover.out | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package domain | ||
|
|
||
| type Ownership struct { | ||
| ID int | ||
| ItemID int | ||
| UserID string | ||
| Rentable bool | ||
| Memo string | ||
| } | ||
|
|
||
| type OwnershipRepository interface { | ||
| GetByID(id int) (*Ownership, error) | ||
| GetByItemID(itemID int) ([]*Ownership, error) | ||
| GetByUserID(userID string) ([]*Ownership, error) | ||
| Create(ownership *Ownership) (*Ownership, error) | ||
| Update(ownership *Ownership) (*Ownership, error) | ||
| Delete(id int) error | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/labstack/echo/v4" | ||
| "github.com/traPtitech/booQ-v3/domain" | ||
| "github.com/traPtitech/booQ-v3/handler/openapi" | ||
| "github.com/traPtitech/booQ-v3/middleware" | ||
| "github.com/traPtitech/booQ-v3/usecase" | ||
| ) | ||
|
|
||
| func (h *handler) PostItemOwners(ctx echo.Context, itemId openapi.ItemIdInPath) error { | ||
| req := openapi.PostItemOwnersJSONRequestBody{} | ||
| if err := ctx.Bind(&req); err != nil { | ||
| return ctx.JSON(http.StatusBadRequest, fmt.Sprintf("invalid request body: %v", err)) | ||
| } | ||
|
|
||
| userID, ok := middleware.GetUserID(ctx.Request().Context()) | ||
| if !ok { | ||
| return ctx.JSON(http.StatusUnauthorized, "user ID not found in context") | ||
| } | ||
| if userID != req.UserId { | ||
| return ctx.JSON(http.StatusForbidden, "you cannot create ownership for another user") | ||
| } | ||
|
|
||
| ownership := &domain.Ownership{ | ||
| ItemID: itemId, | ||
| UserID: req.UserId, | ||
| Rentable: req.Rentalable, | ||
| Memo: req.Memo, | ||
| } | ||
|
|
||
| created, err := h.ou.CreateOwnership(ownership) | ||
| if err != nil { | ||
| return ctx.JSON(http.StatusInternalServerError, fmt.Sprintf("failed to create ownership: %v", err)) | ||
kavos113 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return ctx.JSON(http.StatusCreated, toOpenAPIOwnership(created)) | ||
| } | ||
|
|
||
| func (h *handler) EditItemOwners(ctx echo.Context, itemId openapi.ItemIdInPath, ownershipId openapi.OwnershipIdInPath) error { | ||
| req := openapi.EditItemOwnersJSONRequestBody{} | ||
| if err := ctx.Bind(&req); err != nil { | ||
| return ctx.JSON(http.StatusBadRequest, fmt.Sprintf("invalid request body: %v", err)) | ||
| } | ||
|
|
||
| userID, ok := middleware.GetUserID(ctx.Request().Context()) | ||
| if !ok { | ||
| return ctx.JSON(http.StatusUnauthorized, "user ID not found in context") | ||
| } | ||
|
|
||
| ownership := &domain.Ownership{ | ||
| ID: ownershipId, | ||
| ItemID: itemId, | ||
| UserID: req.UserId, | ||
| Rentable: req.Rentalable, | ||
| Memo: req.Memo, | ||
| } | ||
|
|
||
| updated, err := h.ou.UpdateOwnership(ownership, userID) | ||
| if err != nil { | ||
| if errors.Is(err, domain.ErrNotFound) { | ||
| return ctx.NoContent(http.StatusNotFound) | ||
| } else if errors.Is(err, usecase.ErrForbidden) { | ||
| return ctx.JSON(http.StatusForbidden, "you cannot update this ownership") | ||
| } | ||
| return ctx.JSON(http.StatusInternalServerError, fmt.Sprintf("failed to update ownership: %v", err)) | ||
| } | ||
|
|
||
| return ctx.JSON(http.StatusOK, toOpenAPIOwnership(updated)) | ||
| } | ||
|
|
||
| func (h *handler) DeleteItemOwners(ctx echo.Context, itemId openapi.ItemIdInPath, ownershipId openapi.OwnershipIdInPath) error { | ||
| userID, ok := middleware.GetUserID(ctx.Request().Context()) | ||
| if !ok { | ||
| return ctx.JSON(http.StatusUnauthorized, "user ID not found in context") | ||
| } | ||
|
|
||
| err := h.ou.DeleteOwnership(ownershipId, itemId, userID) | ||
| if err != nil { | ||
| if errors.Is(err, domain.ErrNotFound) { | ||
| return ctx.NoContent(http.StatusNotFound) | ||
| } else if errors.Is(err, usecase.ErrForbidden) { | ||
| return ctx.JSON(http.StatusForbidden, "you cannot delete this ownership") | ||
| } | ||
| return ctx.JSON(http.StatusInternalServerError, fmt.Sprintf("failed to delete ownership: %v", err)) | ||
| } | ||
|
|
||
| return ctx.NoContent(http.StatusOK) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.