Skip to content

Commit

Permalink
Refactor & improvements 🧹🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-iaco committed Feb 6, 2023
1 parent dbab32b commit b51fd72
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 7 additions & 9 deletions service/FoodConsumptionService.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,17 @@ func (s FoodConsumptionService) CreateFoodConsumptionForMeal(mealId uuid.UUID, f
return dto.FoodConsumptionDto{}, err
}
foodConsumption.Cost = (transactionDto.Price / transactionDto.Quantity) * foodConsumptionDto.QuantityUsed
defer func() {
if err != nil {
transactionDto.AvailableQuantity -= foodConsumptionDto.QuantityUsed
_, err = s.groceryService.UpdateFoodTransaction(foodConsumptionDto.FoodId, transactionDto, token)
println(err)
}
}()
}

_, err = s.repository.Create(&foodConsumption)

if foodConsumption.FoodId != uuid.Nil && foodConsumption.TransactionId != uuid.Nil {
if err != nil {
transactionDto.AvailableQuantity -= foodConsumptionDto.QuantityUsed
_, err = s.groceryService.UpdateFoodTransaction(foodConsumptionDto.FoodId, transactionDto, token)
println(err)
return dto.FoodConsumptionDto{}, err
}
}

foodConsumptionDto, err = s.mapMealConsumptionToDto(&foodConsumption)
if err != nil {
log.Println(err)
Expand Down
2 changes: 2 additions & 0 deletions service/GroceryService.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/google/uuid"
"github.com/sony/gobreaker"
"io"
"log"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -108,6 +109,7 @@ func (s *GroceryService) GetTransactionDetail(foodId uuid.UUID, transactionId uu

func (s *GroceryService) UpdateFoodTransaction(foodId uuid.UUID, foodTransactionDto dto.FoodTransactionDto, token string) (dto.FoodTransactionDto, error) {
var response dto.BaseResponse[dto.FoodTransactionDto]
log.Println("Updating food transaction with id: ", foodTransactionDto.ID.String(), " for food with id: ", foodId.String(), " with body: ", foodTransactionDto)
result, err := s.patchCall(s.baseUrl+"/api/item/"+foodId.String()+"/transaction", foodTransactionDto, token)
if err != nil {
return dto.FoodTransactionDto{}, err
Expand Down

0 comments on commit b51fd72

Please sign in to comment.