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 | @RequestParam (required = false) String freeTime, @RequestParam (required = false) String funds,
|
---|
45 | @RequestParam (required = false) boolean hasOtherPets, @RequestParam (required = false) boolean hasKids,
|
---|
46 | @RequestParam (required = false) String housing, @RequestParam (required = false) String physicalActivity,
|
---|
47 | @RequestParam (required = false) boolean willFoster, @RequestParam (required = false) Integer shelter,
|
---|
48 | @RequestParam (required = false) String position, HttpServletRequest request) {
|
---|
49 |
|
---|
50 | if(role.equals("adopter") & password.equals(repeatPassword)){
|
---|
51 |
|
---|
52 | FreeTime ft = FreeTime.valueOf(freeTime);
|
---|
53 | Funds f = Funds.valueOf(funds);
|
---|
54 | Housing h = Housing.valueOf(housing);
|
---|
55 | PhysicalActivity pa = PhysicalActivity.valueOf(physicalActivity);
|
---|
56 |
|
---|
57 | Adopter adopterUser = authService.registerAdopter(name, email, password, telephone,ft,f,hasOtherPets,hasKids,h,pa,willFoster);
|
---|
58 |
|
---|
59 | request.getSession().setAttribute("user",adopterUser);
|
---|
60 | return "redirect:/home";
|
---|
61 | }
|
---|
62 | else if(role.equals("employee") & password.equals(repeatPassword)){
|
---|
63 |
|
---|
64 | Employee employeeUser = authService.registerEmployee(name, email, password, telephone,position,shelter);
|
---|
65 |
|
---|
66 | request.getSession().setAttribute("user",employeeUser);
|
---|
67 | return "redirect:/home";
|
---|
68 | }
|
---|
69 |
|
---|
70 | return "redirect:/register";
|
---|
71 | }
|
---|
72 |
|
---|
73 | }
|
---|