source: source/MovieZilla-master/src/main/java/com/example/demo/controller/RegisterController.java@ fc7ec52

Last change on this file since fc7ec52 was fc7ec52, checked in by darkopopovski <darkopopovski39@…>, 22 months ago

all files

  • Property mode set to 100644
File size: 2.1 KB
Line 
1package com.example.demo.controller;
2
3import com.example.demo.exceptions.InvalidArgumentsException;
4import com.example.demo.exceptions.PasswordsDoNotMatchException;
5import com.example.demo.model.Role;
6import com.example.demo.service.AuthService;
7import com.example.demo.service.UserService;
8import org.springframework.stereotype.Controller;
9import org.springframework.ui.Model;
10import org.springframework.web.bind.annotation.*;
11
12@Controller
13@RequestMapping("/register")
14public 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}
Note: See TracBrowser for help on using the repository browser.