1 | package com.example.demo.controller;
|
---|
2 |
|
---|
3 | import com.example.demo.exceptions.InvalidArgumentsException;
|
---|
4 | import com.example.demo.exceptions.PasswordsDoNotMatchException;
|
---|
5 | import com.example.demo.model.Role;
|
---|
6 | import com.example.demo.service.AuthService;
|
---|
7 | import com.example.demo.service.UserService;
|
---|
8 | import org.springframework.stereotype.Controller;
|
---|
9 | import org.springframework.ui.Model;
|
---|
10 | import org.springframework.web.bind.annotation.*;
|
---|
11 |
|
---|
12 | @Controller
|
---|
13 | @RequestMapping("/register")
|
---|
14 | public class RegisterController {
|
---|
15 |
|
---|
16 | private final AuthService authService;
|
---|
17 | private final UserService userService;
|
---|
18 |
|
---|
19 | public RegisterController(AuthService authService, UserService userService) {
|
---|
20 | this.authService = authService;
|
---|
21 | this.userService = userService;
|
---|
22 | }
|
---|
23 |
|
---|
24 | @GetMapping
|
---|
25 | public String getRegisterPage(@RequestParam(required = false) String error, Model model) {
|
---|
26 | if(error != null && !error.isEmpty()) {
|
---|
27 | model.addAttribute("hasError", true);
|
---|
28 | model.addAttribute("error", error);
|
---|
29 | }
|
---|
30 | model.addAttribute("bodyContent","register");
|
---|
31 | return "master-template";
|
---|
32 | }
|
---|
33 |
|
---|
34 | @PostMapping
|
---|
35 | public String register(@RequestParam(required = false) Integer user_id,
|
---|
36 | @RequestParam String user_username,
|
---|
37 | @RequestParam String user_password,
|
---|
38 | @RequestParam String user_repeatedPassword,
|
---|
39 | @RequestParam String user_phone_number,
|
---|
40 | @RequestParam String user_email,
|
---|
41 | @RequestParam String user_name,
|
---|
42 | @RequestParam String user_surname,
|
---|
43 | @RequestParam Role user_role) {
|
---|
44 | try{
|
---|
45 | this.userService.register( user_id, user_username, user_password,user_repeatedPassword,
|
---|
46 | user_phone_number, user_name, user_surname, user_email, user_role);
|
---|
47 | return "redirect:/login";
|
---|
48 | } catch (InvalidArgumentsException | PasswordsDoNotMatchException exception) {
|
---|
49 | return "redirect:/register?error=" + exception.getMessage();
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|