[b101b69] | 1 | package mk.ukim.finki.busngo.web;
|
---|
| 2 |
|
---|
| 3 | import mk.ukim.finki.busngo.model.enums.Role;
|
---|
| 4 | import mk.ukim.finki.busngo.model.exceptions.InvalidCredentialsException;
|
---|
| 5 | import mk.ukim.finki.busngo.model.exceptions.UserAlreadyExistsException;
|
---|
| 6 | import mk.ukim.finki.busngo.service.AuthService;
|
---|
| 7 | import mk.ukim.finki.busngo.service.KorisnikService;
|
---|
| 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.RequestMapping;
|
---|
| 13 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
| 14 |
|
---|
| 15 | @Controller
|
---|
| 16 | @RequestMapping("/register")
|
---|
| 17 | public class RegisterController {
|
---|
| 18 | private final AuthService authService;
|
---|
| 19 | private final KorisnikService korisnikService;
|
---|
| 20 |
|
---|
| 21 | public RegisterController(AuthService authService, KorisnikService korisnikService) {
|
---|
| 22 | this.authService = authService;
|
---|
| 23 | this.korisnikService = korisnikService;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | @GetMapping
|
---|
| 27 | public String getRegisterPage(@RequestParam(required = false) String error, Model model) {
|
---|
| 28 | if(error != null && !error.isEmpty()) {
|
---|
| 29 | model.addAttribute("hasError", true);
|
---|
| 30 | model.addAttribute("error", error);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | model.addAttribute("bodyContent", "register");
|
---|
| 34 | return "master-template";
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | @PostMapping
|
---|
| 38 | public String register(@RequestParam String name,
|
---|
| 39 | @RequestParam String email,
|
---|
| 40 | @RequestParam String password,
|
---|
| 41 | @RequestParam String confirmPassword,
|
---|
| 42 | @RequestParam String address,
|
---|
| 43 | @RequestParam String phone
|
---|
| 44 | ) {
|
---|
| 45 | try{
|
---|
| 46 | this.authService.register(name, email, password, confirmPassword, address, phone);
|
---|
| 47 | return "redirect:/login";
|
---|
| 48 | } catch (InvalidCredentialsException | UserAlreadyExistsException exception) {
|
---|
| 49 | return "redirect:/register?error=" + exception.getMessage();
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | }
|
---|