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