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