[b101b69] | 1 | package mk.ukim.finki.busngo.web;
|
---|
| 2 |
|
---|
[bde8b13] | 3 | import mk.ukim.finki.busngo.model.entities.Korisnik;
|
---|
[e272096] | 4 | import mk.ukim.finki.busngo.model.enums.VrabotenType;
|
---|
[b101b69] | 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;
|
---|
[bde8b13] | 9 | import mk.ukim.finki.busngo.service.PatnikService;
|
---|
[b101b69] | 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;
|
---|
[bde8b13] | 22 | private final PatnikService patnikService;
|
---|
[b101b69] | 23 |
|
---|
[bde8b13] | 24 | public RegisterController(AuthService authService, KorisnikService korisnikService, PatnikService patnikService) {
|
---|
[b101b69] | 25 | this.authService = authService;
|
---|
| 26 | this.korisnikService = korisnikService;
|
---|
[bde8b13] | 27 | this.patnikService = patnikService;
|
---|
[b101b69] | 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 |
|
---|
[e272096] | 37 | model.addAttribute("types", VrabotenType.values());
|
---|
| 38 |
|
---|
[b101b69] | 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,
|
---|
[e272096] | 49 | @RequestParam String phone,
|
---|
| 50 | @RequestParam(required = false)VrabotenType vrabotenType,
|
---|
| 51 | @RequestParam(required = false) Double salary
|
---|
| 52 | ) {
|
---|
[b101b69] | 53 | try{
|
---|
[e272096] | 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);
|
---|
[b101b69] | 61 | return "redirect:/login";
|
---|
| 62 | } catch (InvalidCredentialsException | UserAlreadyExistsException exception) {
|
---|
| 63 | return "redirect:/register?error=" + exception.getMessage();
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[bde8b13] | 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 |
|
---|
[b101b69] | 77 | }
|
---|