source: src/main/java/mk/ukim/finki/busngo/web/RegisterController.java@ 8b875e6

Last change on this file since 8b875e6 was 8b875e6, checked in by ppaunovski <paunovskipavel@…>, 2 months ago

Added roles to Users and some minor changes

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[b101b69]1package mk.ukim.finki.busngo.web;
2
[bde8b13]3import mk.ukim.finki.busngo.model.entities.Korisnik;
[8b875e6]4import mk.ukim.finki.busngo.model.enums.Role;
[e272096]5import mk.ukim.finki.busngo.model.enums.VrabotenType;
[b101b69]6import mk.ukim.finki.busngo.model.exceptions.InvalidCredentialsException;
7import mk.ukim.finki.busngo.model.exceptions.UserAlreadyExistsException;
8import mk.ukim.finki.busngo.service.AuthService;
9import mk.ukim.finki.busngo.service.KorisnikService;
[bde8b13]10import mk.ukim.finki.busngo.service.PatnikService;
[b101b69]11import org.springframework.stereotype.Controller;
12import org.springframework.ui.Model;
13import org.springframework.web.bind.annotation.GetMapping;
14import org.springframework.web.bind.annotation.PostMapping;
15import org.springframework.web.bind.annotation.RequestMapping;
16import org.springframework.web.bind.annotation.RequestParam;
17
18@Controller
19@RequestMapping("/register")
20public class RegisterController {
21 private final AuthService authService;
22 private final KorisnikService korisnikService;
[bde8b13]23 private final PatnikService patnikService;
[b101b69]24
[bde8b13]25 public RegisterController(AuthService authService, KorisnikService korisnikService, PatnikService patnikService) {
[b101b69]26 this.authService = authService;
27 this.korisnikService = korisnikService;
[bde8b13]28 this.patnikService = patnikService;
[b101b69]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
[8b875e6]38 model.addAttribute("roles", Role.values());
[e272096]39
[b101b69]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,
[e272096]50 @RequestParam String phone,
[8b875e6]51 @RequestParam Role role,
[e272096]52 @RequestParam(required = false) Double salary
53 ) {
[b101b69]54 try{
[8b875e6]55 Korisnik korisnik = authService.register(name, email, password, confirmPassword, address, phone, role, salary);
[e272096]56
[b101b69]57 return "redirect:/login";
58 } catch (InvalidCredentialsException | UserAlreadyExistsException exception) {
59 return "redirect:/register?error=" + exception.getMessage();
60 }
61 }
62
[bde8b13]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
[b101b69]73}
Note: See TracBrowser for help on using the repository browser.