source: src/main/java/project/educatum/web/RegisterController.java@ d3cf3a1

Last change on this file since d3cf3a1 was d3cf3a1, checked in by Marija Micevska <marija_micevska@…>, 23 months ago

Initial commit

  • Property mode set to 100644
File size: 3.7 KB
Line 
1package project.educatum.web;
2
3
4import org.springframework.security.core.userdetails.UserDetails;
5import org.springframework.stereotype.Controller;
6import org.springframework.ui.Model;
7import org.springframework.web.bind.annotation.GetMapping;
8import org.springframework.web.bind.annotation.PostMapping;
9import org.springframework.web.bind.annotation.RequestMapping;
10import org.springframework.web.bind.annotation.RequestParam;
11import project.educatum.model.exceptions.InvalidArgumentsException;
12import project.educatum.model.exceptions.PasswordsDoNotMatchException;
13import project.educatum.model.exceptions.UserNotEnabledException;
14import project.educatum.model.exceptions.UsernameAlreadyExistsException;
15import project.educatum.service.*;
16
17import javax.servlet.http.HttpServletRequest;
18
19@Controller
20@RequestMapping("/register")
21public class RegisterController {
22
23 private final TeacherService teacherService;
24 private final StudentService studentService;
25 private final AdminService adminService;
26 private final SubjectService subjectService;
27 private final AuthService authService;
28
29 public RegisterController(TeacherService teacherService, StudentService studentService, AdminService adminService, SubjectService subjectService, AuthService authService) {
30 this.teacherService = teacherService;
31 this.studentService = studentService;
32 this.adminService = adminService;
33 this.subjectService = subjectService;
34 this.authService = authService;
35 }
36
37
38 @GetMapping
39 public String getRegisterPage(@RequestParam(required = false) String error, Model model) {
40 if (error != null && !error.isEmpty()) {
41 model.addAttribute("hasErrors", true);
42 model.addAttribute("errors", error);
43
44 }
45 return "/register";
46 }
47
48 @PostMapping("/registrirajSe")
49 public String register(@RequestParam String ime,
50 @RequestParam String prezime,
51 @RequestParam String email,
52 @RequestParam String password,
53 @RequestParam String repeatPassword,
54 @RequestParam String role,
55 @RequestParam String telBroj,
56 @RequestParam(required = false) String opis,
57 HttpServletRequest request) {
58
59
60 if (role.equals("ROLE_NASTAVNIK")) {
61 try {
62 this.teacherService.register(ime, prezime, email, password, repeatPassword, telBroj, opis);
63 UserDetails user = authService.loginTeacher(email, password);
64 request.getSession().setAttribute("user", user);
65 return "redirect:/home/chooseSubject";
66
67 } catch (PasswordsDoNotMatchException | InvalidArgumentsException | UsernameAlreadyExistsException exception) {
68 return "redirect:/register?error=" + exception.getMessage();
69
70 } catch (UserNotEnabledException e) {
71 request.getSession().setAttribute("user", teacherService.findByEmail(email));
72 return "redirect:/home/chooseSubject";
73 }
74 } else if (role.equals("ROLE_UCENIK")) {
75 try {
76 this.studentService.register(ime, prezime, email, password, repeatPassword, telBroj, opis);
77 UserDetails user = authService.loginStudent(email, password);
78 request.getSession().setAttribute("user", user);
79 return "redirect:/home/zainteresiran";
80
81 } catch (PasswordsDoNotMatchException | InvalidArgumentsException exception) {
82 return "redirect:/register?error=" + exception.getMessage();
83
84 }
85 }
86
87 return "redirect:/login";
88 }
89
90}
91
Note: See TracBrowser for help on using the repository browser.