Skip to content

Commit

Permalink
fix: should display an error instead of taking absolute (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcellodotgg authored Oct 22, 2024
1 parent 27989c6 commit dba5631
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions internal/api/handler/customer_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ func (h customerHandler) TransferMoney(c *gin.Context) {

if err := h.transactionService.TransferMoney(fromAccount, toAccount, amount); err != nil {
switch err := err.Error(); err {
case "amount must be greater than 0":
h.Form.Errors["amount"] = "Amount must be greater than 0"
c.HTML(http.StatusUnprocessableEntity, "account/transfer_money_form", h)
return
case "not enough money":
h.Form.Errors["from_account"] = "You do not have enough money in this account"
c.HTML(http.StatusUnprocessableEntity, "account/transfer_money_form", h)
Expand Down
4 changes: 3 additions & 1 deletion internal/service/transaction_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ func (ts transactionService) Create(transaction *domain.Transaction) error {

func (s transactionService) TransferMoney(from domain.Account, to domain.Account, amount float64) error {
return persistence.DB.Transaction(func(tx *gorm.DB) error {
amount = math.Abs(amount)
if amount <= 0 {
return errors.New("amount must be greater than 0")
}

if from.Balance < amount {
return errors.New("not enough money")
Expand Down

0 comments on commit dba5631

Please sign in to comment.