Skip to content

Commit

Permalink
Merge pull request #16 from Jibaru/feat-15
Browse files Browse the repository at this point in the history
Add tranfer item from one box to another box endpoint
  • Loading branch information
Jibaru authored Feb 12, 2024
2 parents 68b206d + ce164ef commit 3e32eac
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
33 changes: 33 additions & 0 deletions internal/app/application/services/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,36 @@ func (s *BoxService) makeGetAllQueryFilter(

return queryFilter
}

func (s *BoxService) TransferItem(
fromBoxID string,
toBoxID string,
itemID string,
) error {
fromBoxItem, err := s.boxRepository.GetBoxItem(fromBoxID, itemID)
if err != nil {
return err
}

quantity := fromBoxItem.Quantity

err = s.RemoveItemFromBox(
quantity,
fromBoxID,
itemID,
)
if err != nil {
return err
}

_, err = s.AddItemIntoBox(
quantity,
toBoxID,
itemID,
)
if err != nil {
return err
}

return nil
}
44 changes: 44 additions & 0 deletions internal/app/application/services/box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,47 @@ func TestBoxServiceCountAllErrorInBoxRepository(t *testing.T) {
itemRepository.AssertExpectations(t)
roomRepository.AssertExpectations(t)
}

func TestBoxServiceTransferItem(t *testing.T) {
boxRepository := new(stub.BoxRepositoryMock)
roomRepository := new(stub.RoomRepositoryMock)
itemRepository := new(stub.ItemRepositoryMock)

boxService := NewBoxService(boxRepository, itemRepository, roomRepository)

originBoxID := uuid.NewString()
destinationBoxID := uuid.NewString()
itemID := uuid.NewString()

itemRepository.On("GetByID", itemID).
Return(&entities.Item{
ID: itemID,
}, nil)
boxRepository.On("GetBoxItem", originBoxID, itemID).
Return(&entities.BoxItem{
BoxID: originBoxID,
ItemID: itemID,
Quantity: 10.0,
}, nil)
boxRepository.On("GetBoxItem", destinationBoxID, itemID).
Return(&entities.BoxItem{
BoxID: originBoxID,
ItemID: itemID,
Quantity: 10.0,
}, nil)
boxRepository.On("DeleteBoxItem", originBoxID, itemID).
Return(nil)
boxRepository.On("CreateBoxTransaction", mock.AnythingOfType("*entities.BoxTransaction")).
Return(nil)
boxRepository.On("UpdateBoxItem", mock.AnythingOfType("*entities.BoxItem")).
Return(nil)

err := boxService.TransferItem(originBoxID, destinationBoxID, itemID)

time.Sleep(2 * time.Second)

assert.NoError(t, err)
boxRepository.AssertExpectations(t)
itemRepository.AssertExpectations(t)
roomRepository.AssertExpectations(t)
}
47 changes: 47 additions & 0 deletions internal/app/infrastructure/controllers/transfer_item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package controllers

import (
"github.com/jibaru/home-inventory-api/m/internal/app/application/services"
"github.com/jibaru/home-inventory-api/m/internal/app/infrastructure/responses"
"github.com/labstack/echo/v4"
"net/http"
)

type TransferItemController struct {
boxService *services.BoxService
}

type TransferItemRequest struct {
BoxID string `param:"boxID"`
ItemID string `param:"itemID"`
BoxDestinationID string `json:"box_destination_id"`
}

func NewTransferItemController(boxService *services.BoxService) *TransferItemController {
return &TransferItemController{boxService}
}

func (c *TransferItemController) Handle(ctx echo.Context) error {
request := TransferItemRequest{}

err := (&echo.DefaultBinder{}).BindBody(ctx, &request)
if err != nil {
return ctx.JSON(http.StatusBadRequest, responses.NewMessageResponse(err.Error()))
}

err = (&echo.DefaultBinder{}).BindPathParams(ctx, &request)
if err != nil {
return ctx.JSON(http.StatusBadRequest, responses.NewMessageResponse(err.Error()))
}

err = c.boxService.TransferItem(
request.BoxID,
request.BoxDestinationID,
request.ItemID,
)
if err != nil {
return ctx.JSON(http.StatusBadRequest, responses.NewMessageResponse(err.Error()))
}

return ctx.JSON(http.StatusNoContent, nil)
}
2 changes: 2 additions & 0 deletions internal/app/infrastructure/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func RunServer(
getRoomsController := controllers.NewGetRoomsController(roomService)
getBoxesController := controllers.NewGetBoxesController(boxService)
getItemsController := controllers.NewGetItemsController(assetService, itemService)
transferItemController := controllers.NewTransferItemController(boxService)

loggerMiddleware := middlewares.NewLoggerMiddleware()
needsAuthMiddleware := middlewares.NewNeedsAuthMiddleware(authService)
Expand All @@ -77,6 +78,7 @@ func RunServer(
authApi.GET("/rooms", getRoomsController.Handle)
authApi.GET("/boxes", getBoxesController.Handle)
authApi.GET("/items", getItemsController.Handle)
authApi.POST("/boxes/:boxID/items/:itemID/transfer", transferItemController.Handle)

logger.LogError(e.Start(host + ":" + port))
}

0 comments on commit 3e32eac

Please sign in to comment.