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

Last change on this file since b101b69 was b101b69, checked in by ppaunovski <paunovskipavel@…>, 6 months ago

initial classes, no inheritance yet v2

  • Property mode set to 100644
File size: 2.0 KB
Line 
1package mk.ukim.finki.busngo.web;
2
3import mk.ukim.finki.busngo.model.enums.Role;
4import mk.ukim.finki.busngo.model.exceptions.InvalidCredentialsException;
5import mk.ukim.finki.busngo.model.exceptions.UserAlreadyExistsException;
6import mk.ukim.finki.busngo.service.AuthService;
7import mk.ukim.finki.busngo.service.KorisnikService;
8import org.springframework.stereotype.Controller;
9import org.springframework.ui.Model;
10import org.springframework.web.bind.annotation.GetMapping;
11import org.springframework.web.bind.annotation.PostMapping;
12import org.springframework.web.bind.annotation.RequestMapping;
13import org.springframework.web.bind.annotation.RequestParam;
14
15@Controller
16@RequestMapping("/register")
17public 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}
Note: See TracBrowser for help on using the repository browser.