Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ booQ-v3
!/uploads/.gitkeep
/uploads/*

.tool-versions
.tool-versions
.idea
cover.html
cover.out
123 changes: 123 additions & 0 deletions domain/mock/mock_ownership_repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions domain/ownership.go
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
}
10 changes: 10 additions & 0 deletions handler/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,13 @@ func postRequestToDomainItem(request *openapi.ItemPostRequest) (*domain.Item, er

return item, nil
}

func toOpenAPIOwnership(d *domain.Ownership) openapi.Ownership {
return openapi.Ownership{
Id: &d.ID,
ItemId: &d.ItemID,
UserId: d.UserID,
Rentalable: d.Rentable,
Memo: d.Memo,
}
}
4 changes: 2 additions & 2 deletions handler/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestHandler_PostFile(t *testing.T) {
mockFileUseCase := mock_usecase.NewMockFileUseCase(ctrl)
tc.setupMock(mockFileUseCase)

h := NewHandler(mockItemUseCase, mockFileUseCase)
h := NewHandler(mockItemUseCase, mockFileUseCase, nil)

e := echo.New()
openapi.RegisterHandlers(e, h)
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestHandler_GetFile(t *testing.T) {
mockFileUseCase := mock_usecase.NewMockFileUseCase(ctrl)
tc.setupMock(mockFileUseCase)

h := NewHandler(mockItemUseCase, mockFileUseCase)
h := NewHandler(mockItemUseCase, mockFileUseCase, nil)

e := echo.New()
openapi.RegisterHandlers(e, h)
Expand Down
23 changes: 5 additions & 18 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
)

type handler struct {
iu usecase.ItemUseCase
fu usecase.FileUseCase
iu usecase.ItemUseCase
ou usecase.OwnershipUseCase
}

func NewHandler(iu usecase.ItemUseCase, fu usecase.FileUseCase) openapi.ServerInterface {
func NewHandler(iu usecase.ItemUseCase, fu usecase.FileUseCase, ou usecase.OwnershipUseCase) openapi.ServerInterface {
return &handler{
iu: iu,
fu: fu,
iu: iu,
ou: ou,
}
}

Expand Down Expand Up @@ -43,21 +45,6 @@ func (h *handler) AddLike(ctx echo.Context, itemId openapi.ItemIdInPath) error {
panic("implement me")
}

func (h *handler) PostItemOwners(ctx echo.Context, itemId openapi.ItemIdInPath) error {
//TODO implement me
panic("implement me")
}

func (h *handler) DeleteItemOwners(ctx echo.Context, itemId openapi.ItemIdInPath, ownershipId openapi.OwnershipIdInPath) error {
//TODO implement me
panic("implement me")
}

func (h *handler) EditItemOwners(ctx echo.Context, itemId openapi.ItemIdInPath, ownershipId openapi.OwnershipIdInPath) error {
//TODO implement me
panic("implement me")
}

func (h *handler) PostBorrow(ctx echo.Context, itemId openapi.ItemIdInPath, ownershipId openapi.OwnershipIdInPath) error {
//TODO implement me
panic("implement me")
Expand Down
10 changes: 5 additions & 5 deletions handler/item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func TestHandler_GetItem(t *testing.T) {
mockItemUseCase := mock_usecase.NewMockItemUseCase(ctrl)
tc.setupMock(mockItemUseCase)

h := NewHandler(mockItemUseCase, nil)
h := NewHandler(mockItemUseCase, nil, nil)

e := echo.New()
openapi.RegisterHandlers(e, h)
Expand Down Expand Up @@ -386,7 +386,7 @@ func TestHandler_GetItems(t *testing.T) {
mockItemUseCase := mock_usecase.NewMockItemUseCase(ctrl)
tc.setupMock(mockItemUseCase)

h := NewHandler(mockItemUseCase, nil)
h := NewHandler(mockItemUseCase, nil, nil)

e := echo.New()
openapi.RegisterHandlers(e, h)
Expand Down Expand Up @@ -598,7 +598,7 @@ func TestHandler_CreateItem(t *testing.T) {
mockItemUseCase := mock_usecase.NewMockItemUseCase(ctrl)
tc.setupMock(mockItemUseCase)

h := NewHandler(mockItemUseCase, nil)
h := NewHandler(mockItemUseCase, nil, nil)

e := echo.New()
openapi.RegisterHandlers(e, h)
Expand Down Expand Up @@ -779,7 +779,7 @@ func TestHandler_UpdateItem(t *testing.T) {
mockItemUseCase := mock_usecase.NewMockItemUseCase(ctrl)
tc.setupMock(mockItemUseCase)

h := NewHandler(mockItemUseCase, nil)
h := NewHandler(mockItemUseCase, nil, nil)

e := echo.New()
openapi.RegisterHandlers(e, h)
Expand Down Expand Up @@ -849,7 +849,7 @@ func TestHandler_DeleteItem(t *testing.T) {
mockItemUseCase := mock_usecase.NewMockItemUseCase(ctrl)
tc.setupMock(mockItemUseCase)

h := NewHandler(mockItemUseCase, nil)
h := NewHandler(mockItemUseCase, nil, nil)

e := echo.New()
openapi.RegisterHandlers(e, h)
Expand Down
93 changes: 93 additions & 0 deletions handler/ownership.go
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))
}

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)
}
Loading
Loading