1 | package mk.ukim.finki.busngo.web;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.busngo.model.entities.Korisnik;
|
---|
4 | import mk.ukim.finki.busngo.model.enums.Role;
|
---|
5 | import mk.ukim.finki.busngo.model.enums.VrabotenType;
|
---|
6 | import mk.ukim.finki.busngo.model.exceptions.InvalidCredentialsException;
|
---|
7 | import mk.ukim.finki.busngo.model.exceptions.UserAlreadyExistsException;
|
---|
8 | import mk.ukim.finki.busngo.service.AuthService;
|
---|
9 | import mk.ukim.finki.busngo.service.KorisnikService;
|
---|
10 | import mk.ukim.finki.busngo.service.PatnikService;
|
---|
11 | import org.springframework.stereotype.Controller;
|
---|
12 | import org.springframework.ui.Model;
|
---|
13 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
14 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
15 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
16 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
17 |
|
---|
18 | @Controller
|
---|
19 | @RequestMapping("/register")
|
---|
20 | public class RegisterController {
|
---|
21 | private final AuthService authService;
|
---|
22 | private final KorisnikService korisnikService;
|
---|
23 | private final PatnikService patnikService;
|
---|
24 |
|
---|
25 | public RegisterController(AuthService authService, KorisnikService korisnikService, PatnikService patnikService) {
|
---|
26 | this.authService = authService;
|
---|
27 | this.korisnikService = korisnikService;
|
---|
28 | this.patnikService = patnikService;
|
---|
29 | }
|
---|
30 |
|
---|
31 | @GetMapping
|
---|
32 | public String getRegisterPage(@RequestParam(required = false) String error, Model model) {
|
---|
33 | if(error != null && !error.isEmpty()) {
|
---|
34 | model.addAttribute("hasError", true);
|
---|
35 | model.addAttribute("error", error);
|
---|
36 | }
|
---|
37 |
|
---|
38 | model.addAttribute("roles", Role.values());
|
---|
39 |
|
---|
40 | model.addAttribute("bodyContent", "register");
|
---|
41 | return "master-template";
|
---|
42 | }
|
---|
43 |
|
---|
44 | @PostMapping
|
---|
45 | public String register(@RequestParam String name,
|
---|
46 | @RequestParam String email,
|
---|
47 | @RequestParam String password,
|
---|
48 | @RequestParam String confirmPassword,
|
---|
49 | @RequestParam String address,
|
---|
50 | @RequestParam String phone,
|
---|
51 | @RequestParam Role role,
|
---|
52 | @RequestParam(required = false) Double salary
|
---|
53 | ) {
|
---|
54 | try{
|
---|
55 | Korisnik korisnik = authService.register(name, email, password, confirmPassword, address, phone, role, salary);
|
---|
56 |
|
---|
57 | return "redirect:/login";
|
---|
58 | } catch (InvalidCredentialsException | UserAlreadyExistsException exception) {
|
---|
59 | return "redirect:/register?error=" + exception.getMessage();
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | // @PostMapping
|
---|
64 | // public String registerVraboten(@RequestParam String name,
|
---|
65 | // @RequestParam String email,
|
---|
66 | // @RequestParam String password,
|
---|
67 | // @RequestParam String confirmPassword,
|
---|
68 | // @RequestParam String address,
|
---|
69 | // @RequestParam String phone){
|
---|
70 | //
|
---|
71 | // }
|
---|
72 |
|
---|
73 | }
|
---|