1 | package finki.paw5.web.controllers;
|
---|
2 | import finki.paw5.model.entities.Adopter;
|
---|
3 | import finki.paw5.model.entities.Employee;
|
---|
4 | import finki.paw5.model.enumerations.FreeTime;
|
---|
5 | import finki.paw5.model.enumerations.Funds;
|
---|
6 | import finki.paw5.model.enumerations.Housing;
|
---|
7 | import finki.paw5.model.enumerations.PhysicalActivity;
|
---|
8 | import finki.paw5.service.AuthService;
|
---|
9 | import finki.paw5.service.OrganisationService;
|
---|
10 | import finki.paw5.service.ShelterService;
|
---|
11 | import jakarta.servlet.http.HttpServletRequest;
|
---|
12 | import org.springframework.stereotype.Controller;
|
---|
13 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
14 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
15 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
16 | import org.springframework.ui.Model;
|
---|
17 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
18 |
|
---|
19 | @Controller
|
---|
20 | @RequestMapping("/register")
|
---|
21 | public class RegisterController {
|
---|
22 |
|
---|
23 | private final OrganisationService organisationService;
|
---|
24 | private final ShelterService shelterService;
|
---|
25 | private final AuthService authService;
|
---|
26 |
|
---|
27 | public RegisterController(OrganisationService organisationService, ShelterService shelterService, AuthService authService) {
|
---|
28 | this.organisationService = organisationService;
|
---|
29 | this.shelterService = shelterService;
|
---|
30 | this.authService = authService;
|
---|
31 | }
|
---|
32 |
|
---|
33 | @GetMapping
|
---|
34 | public String getRegisterPage(Model model) {
|
---|
35 | model.addAttribute("shelters", this.shelterService.listShelters());
|
---|
36 | model.addAttribute("organisations", this.organisationService.findAll());
|
---|
37 | return "register";
|
---|
38 | }
|
---|
39 |
|
---|
40 | @PostMapping
|
---|
41 | public String registerUser(@RequestParam String name, @RequestParam String email,
|
---|
42 | @RequestParam String password, @RequestParam String repeatPassword,
|
---|
43 | @RequestParam String telephone,@RequestParam String role,
|
---|
44 | HttpServletRequest request) {
|
---|
45 |
|
---|
46 | if(role.equals("adopter") & password.equals(repeatPassword)){
|
---|
47 | String freeTime = request.getParameter("freeTime");
|
---|
48 | String funds = request.getParameter("funds");
|
---|
49 | boolean hasOtherPets = Boolean.parseBoolean(request.getParameter("hasOtherPets"));
|
---|
50 | boolean hasKids = Boolean.parseBoolean(request.getParameter("hasKids"));
|
---|
51 | String housing = request.getParameter("housing");
|
---|
52 | String physicalActivity = request.getParameter("physicalActivity");
|
---|
53 | boolean willFoster = Boolean.parseBoolean(request.getParameter("willFoster"));
|
---|
54 |
|
---|
55 | FreeTime ft = FreeTime.valueOf(freeTime);
|
---|
56 | Funds f = Funds.valueOf(funds);
|
---|
57 | Housing h = Housing.valueOf(housing);
|
---|
58 | PhysicalActivity pa = PhysicalActivity.valueOf(physicalActivity);
|
---|
59 |
|
---|
60 | Adopter adopterUser = authService.registerAdopter(name, email, password, telephone,ft,f,hasOtherPets,hasKids,h,pa,willFoster);
|
---|
61 |
|
---|
62 | request.getSession().setAttribute("user",adopterUser);
|
---|
63 | return "redirect:/home";
|
---|
64 | }
|
---|
65 | else if(role.equals("employee") & password.equals(repeatPassword)){
|
---|
66 | Integer shelter = Integer.valueOf(request.getParameter("shelter"));
|
---|
67 | String position = request.getParameter("position");
|
---|
68 |
|
---|
69 | Employee employeeUser = authService.registerEmployee(name, email, password, telephone,position,shelter);
|
---|
70 |
|
---|
71 | request.getSession().setAttribute("user",employeeUser);
|
---|
72 | return "redirect:/home";
|
---|
73 | }
|
---|
74 |
|
---|
75 | return "redirect:/register";
|
---|
76 | }
|
---|
77 |
|
---|
78 | }
|
---|