@@ -3,30 +3,36 @@ package handler
3
3
import (
4
4
"fmt"
5
5
"net/http"
6
+ "strconv"
6
7
"strings"
7
8
8
9
"github.com/bytebury/fun-banking/internal/domain"
10
+ "github.com/bytebury/fun-banking/internal/infrastructure/persistence"
9
11
"github.com/bytebury/fun-banking/internal/service"
10
12
"github.com/gin-gonic/gin"
11
13
)
12
14
13
15
type customerHandler struct {
14
16
pageObject
15
- bankService service.BankService
16
- customerService service.CustomerService
17
- accountService service.AccountService
18
- userService service.UserService
19
- Bank domain.Bank
20
- Customer domain.Customer
17
+ bankService service.BankService
18
+ customerService service.CustomerService
19
+ accountService service.AccountService
20
+ transactionService service.TransactionService
21
+ userService service.UserService
22
+ Bank domain.Bank
23
+ Customer domain.Customer
24
+ MAX_TRANSACTION_AMOUNT int
21
25
}
22
26
23
27
func NewCustomerHandler () customerHandler {
24
28
return customerHandler {
25
- bankService : service .NewBankService (),
26
- customerService : service .NewCustomerService (),
27
- userService : service .NewUserService (),
28
- accountService : service .NewAccountService (),
29
- Bank : domain.Bank {},
29
+ bankService : service .NewBankService (),
30
+ customerService : service .NewCustomerService (),
31
+ userService : service .NewUserService (),
32
+ accountService : service .NewAccountService (),
33
+ transactionService : service .NewTransactionService (),
34
+ Bank : domain.Bank {},
35
+ MAX_TRANSACTION_AMOUNT : domain .MAX_TRANSACTION_AMOUNT ,
30
36
}
31
37
}
32
38
@@ -165,6 +171,76 @@ func (h customerHandler) OpenAccount(c *gin.Context) {
165
171
c .Header ("HX-Redirect" , fmt .Sprintf ("/accounts/%d" , account .ID ))
166
172
}
167
173
174
+ func (h customerHandler ) OpenTransferMoneyModal (c * gin.Context ) {
175
+ h .Reset (c )
176
+ h .ModalType = "transfer_money_modal"
177
+
178
+ if err := h .customerService .FindByID (c .Param ("id" ), & h .Customer ); err != nil {
179
+ c .HTML (http .StatusNotFound , "not-found" , nil )
180
+ return
181
+ }
182
+
183
+ c .HTML (http .StatusOK , "modal" , h )
184
+ }
185
+
186
+ func (h customerHandler ) TransferMoney (c * gin.Context ) {
187
+ h .Reset (c )
188
+
189
+ if err := h .customerService .FindByID (c .Param ("id" ), & h .Customer ); err != nil {
190
+ h .Form .Errors ["general" ] = "Could not find customer"
191
+ c .HTML (http .StatusNotFound , "account/transfer-money-form" , h )
192
+ return
193
+ }
194
+
195
+ var fromAccount domain.Account
196
+ var toAccount domain.Account
197
+
198
+ if err := persistence .DB .First (& fromAccount , "id = ?" , h .Form .Data ["from_account" ]).Error ; err != nil {
199
+ h .Form .Errors ["general" ] = "Account does not exist"
200
+ c .HTML (http .StatusNotFound , "account/transfer-money-form" , h )
201
+ return
202
+ }
203
+
204
+ if err := persistence .DB .First (& toAccount , "id = ?" , h .Form .Data ["to_account" ]).Error ; err != nil {
205
+ h .Form .Errors ["general" ] = "Account does not exist"
206
+ c .HTML (http .StatusNotFound , "account/transfer_money_form" , h )
207
+ return
208
+ }
209
+
210
+ amount , err := strconv .ParseFloat (h .Form .Data ["amount" ], 64 )
211
+
212
+ if err != nil {
213
+ h .Form .Errors ["amount" ] = "Invalid currency value"
214
+ c .HTML (http .StatusUnprocessableEntity , "account/transfer_money_form" , h )
215
+ }
216
+
217
+ if err := h .transactionService .TransferMoney (fromAccount , toAccount , amount ); err != nil {
218
+ switch err := err .Error (); err {
219
+ case "not enough money" :
220
+ h .Form .Errors ["from_account" ] = "You do not have enough money in this account"
221
+ c .HTML (http .StatusUnprocessableEntity , "account/transfer_money_form" , h )
222
+ return
223
+ case "cannot transfer to same account" :
224
+ h .Form .Errors ["to_account" ] = "You cannot transfer money to the same account"
225
+ c .HTML (http .StatusUnprocessableEntity , "account/transfer_money_form" , h )
226
+ return
227
+ case "cannot transfer to other customers accounts" :
228
+ h .Form .Errors ["general" ] = "You do not have enough money in this account"
229
+ c .HTML (http .StatusUnprocessableEntity , "account/transfer_money_form" , h )
230
+ return
231
+ }
232
+ }
233
+
234
+ if err := h .customerService .FindByID (c .Param ("id" ), & h .Customer ); err != nil {
235
+ h .Form .Errors ["general" ] = "Could not find customer"
236
+ c .HTML (http .StatusNotFound , "account/transfer-money-form" , h )
237
+ return
238
+ }
239
+
240
+ c .Header ("HX-Trigger" , "closeModal" )
241
+ c .HTML (http .StatusOK , "account/transfer_money_oob" , h )
242
+ }
243
+
168
244
func (h customerHandler ) isOwner (customerID , userID string ) bool {
169
245
var customer domain.Customer
170
246
if err := h .customerService .FindByID (customerID , & customer ); err != nil {
0 commit comments