1 | package mk.ukim.finki.wp.bankappfinal.controller;
|
---|
2 |
|
---|
3 |
|
---|
4 | import mk.ukim.finki.wp.bankappfinal.model.Account;
|
---|
5 | import mk.ukim.finki.wp.bankappfinal.service.AccountService;
|
---|
6 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
7 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
8 | import org.springframework.stereotype.Controller;
|
---|
9 | import org.springframework.ui.Model;
|
---|
10 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
11 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
12 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
13 |
|
---|
14 | import java.math.BigDecimal;
|
---|
15 |
|
---|
16 | @Controller
|
---|
17 | public class BankController {
|
---|
18 |
|
---|
19 | @Autowired
|
---|
20 | private AccountService accountService;
|
---|
21 |
|
---|
22 | @GetMapping("/dashboard")
|
---|
23 | public String dashboard(Model model) {
|
---|
24 | String username = SecurityContextHolder.getContext().getAuthentication().getName();
|
---|
25 | Account account = accountService.findAccountByUsername(username);
|
---|
26 | model.addAttribute("account", account);
|
---|
27 | return "dashboard";
|
---|
28 | }
|
---|
29 |
|
---|
30 | @GetMapping("/register")
|
---|
31 | public String showRegistrationForm() {
|
---|
32 | return "register";
|
---|
33 | }
|
---|
34 |
|
---|
35 | @PostMapping("/register")
|
---|
36 | public String registerAccount(@RequestParam String username,@RequestParam String email, @RequestParam String password, Model model) {
|
---|
37 | try {
|
---|
38 | accountService.registerAccount(username,email, password);
|
---|
39 | return "redirect:/login";
|
---|
40 | } catch (RuntimeException e) {
|
---|
41 | model.addAttribute("error", e.getMessage());
|
---|
42 | return "register";
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | @GetMapping("/login")
|
---|
47 | public String login() {
|
---|
48 | return "login";
|
---|
49 | }
|
---|
50 |
|
---|
51 | @PostMapping("/deposit")
|
---|
52 | public String deposit(@RequestParam BigDecimal amount) {
|
---|
53 | String username = SecurityContextHolder.getContext().getAuthentication().getName();
|
---|
54 | Account account = accountService.findAccountByUsername(username);
|
---|
55 | accountService.deposit(account, amount);
|
---|
56 | return "redirect:/dashboard";
|
---|
57 | }
|
---|
58 |
|
---|
59 | @PostMapping("/withdraw")
|
---|
60 | public String withdraw(@RequestParam BigDecimal amount, Model model) {
|
---|
61 | String username = SecurityContextHolder.getContext().getAuthentication().getName();
|
---|
62 | Account account = accountService.findAccountByUsername(username);
|
---|
63 |
|
---|
64 | try {
|
---|
65 | accountService.withdraw(account, amount);
|
---|
66 | } catch (RuntimeException e) {
|
---|
67 | model.addAttribute("error", e.getMessage());
|
---|
68 | model.addAttribute("account", account);
|
---|
69 | return "dashboard";
|
---|
70 | }
|
---|
71 |
|
---|
72 | return "redirect:/dashboard";
|
---|
73 | }
|
---|
74 |
|
---|
75 | @GetMapping("/transactions")
|
---|
76 | public String transactionHistory(Model model) {
|
---|
77 | String username = SecurityContextHolder.getContext().getAuthentication().getName();
|
---|
78 | Account account = accountService.findAccountByUsername(username);
|
---|
79 | model.addAttribute("transactions", accountService.getTransactionHistory(account));
|
---|
80 | return "transactions";
|
---|
81 | }
|
---|
82 |
|
---|
83 | @PostMapping("/transfer")
|
---|
84 | public String transferAmount(@RequestParam String toUsername, @RequestParam BigDecimal amount, @RequestParam String currency, Model model) {
|
---|
85 | String username = SecurityContextHolder.getContext().getAuthentication().getName();
|
---|
86 | Account fromAccount = accountService.findAccountByUsername(username);
|
---|
87 |
|
---|
88 | try {
|
---|
89 | accountService.transferAmount(fromAccount, toUsername, amount, currency);
|
---|
90 | } catch (RuntimeException e) {
|
---|
91 | model.addAttribute("error", e.getMessage());
|
---|
92 | model.addAttribute("account", fromAccount);
|
---|
93 | return "dashboard";
|
---|
94 | }
|
---|
95 |
|
---|
96 | return "redirect:/dashboard";
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | } |
---|