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
Line 
1package mk.ukim.finki.busngo.web;
2
3import mk.ukim.finki.busngo.model.entities.Korisnik;
4import mk.ukim.finki.busngo.model.enums.Role;
5import mk.ukim.finki.busngo.model.enums.VrabotenType;
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;
10import mk.ukim.finki.busngo.service.PatnikService;
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;
23 private final PatnikService patnikService;
24
25 public RegisterController(AuthService authService, KorisnikService korisnikService, PatnikService patnikService) {
26 this.authService = authService;
27 this.korisnikService = korisnikService;
28 this.patnikService = patnikService;
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
38 model.addAttribute("roles", Role.values());
39
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,
50 @RequestParam String phone,
51 @RequestParam Role role,
52 @RequestParam(required = false) Double salary
53 ) {
54 try{
55 Korisnik korisnik = authService.register(name, email, password, confirmPassword, address, phone, role, salary);
56
57 return "redirect:/login";
58 } catch (InvalidCredentialsException | UserAlreadyExistsException exception) {
59 return "redirect:/register?error=" + exception.getMessage();
60 }
61 }
62
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
73}
Note: See TracBrowser for help on using the repository browser.