1 | package it.finki.tinki.web.controller;
|
---|
2 |
|
---|
3 | import it.finki.tinki.model.Jobs.Internship;
|
---|
4 | import it.finki.tinki.model.Jobs.Job;
|
---|
5 | import it.finki.tinki.model.Jobs.Project;
|
---|
6 | import it.finki.tinki.model.Users.Account;
|
---|
7 | import it.finki.tinki.model.Users.Company;
|
---|
8 | import it.finki.tinki.model.Users.Team;
|
---|
9 | import it.finki.tinki.model.Users.User;
|
---|
10 | import it.finki.tinki.model.dto.*;
|
---|
11 | import it.finki.tinki.model.enumerator.AccountType;
|
---|
12 | import it.finki.tinki.service.AccountService;
|
---|
13 | import it.finki.tinki.service.MatchmakerService;
|
---|
14 | import it.finki.tinki.service.WorkService;
|
---|
15 | import org.springframework.web.bind.annotation.*;
|
---|
16 | import org.springframework.web.server.ResponseStatusException;
|
---|
17 |
|
---|
18 | import java.util.List;
|
---|
19 |
|
---|
20 | @RestController
|
---|
21 | @CrossOrigin(origins = "http://localhost:3000")
|
---|
22 | @RequestMapping("/api")
|
---|
23 | public class LoginController {
|
---|
24 |
|
---|
25 | AccountService accountService;
|
---|
26 | MatchmakerService matchmakerService;
|
---|
27 | WorkService workService;
|
---|
28 |
|
---|
29 | public LoginController(AccountService accountService, MatchmakerService matchmakerService, WorkService workService) {
|
---|
30 | this.accountService = accountService;
|
---|
31 | this.matchmakerService = matchmakerService;
|
---|
32 | this.workService = workService;
|
---|
33 | }
|
---|
34 |
|
---|
35 | @PostMapping(path = "/login")
|
---|
36 | public LoginResponseDTO loginProcess(@RequestBody AccountLoginDTO body) throws ResponseStatusException {
|
---|
37 |
|
---|
38 | Account a1 = accountService.findUser(body.getEmail(), body.getPassword(), body.getType());
|
---|
39 |
|
---|
40 | if(a1!=null){
|
---|
41 | if(a1.getClass().equals(User.class)){
|
---|
42 | return processUser(a1);
|
---|
43 | }else if(a1.getClass().equals(Team.class)){
|
---|
44 | return processTeam(a1);
|
---|
45 | }else{
|
---|
46 | return processCompany(a1);
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | return new LoginResponseDTO();
|
---|
51 | }
|
---|
52 |
|
---|
53 | private UserResponseDTO processUser(Account a1){
|
---|
54 | UserResponseDTO uDto = new UserResponseDTO();
|
---|
55 |
|
---|
56 | uDto.setError(null);
|
---|
57 |
|
---|
58 | uDto.setId(a1.getId());
|
---|
59 | uDto.setEmail(a1.getEmail());
|
---|
60 | uDto.setName(a1.getName());
|
---|
61 | uDto.setType(AccountType.USER);
|
---|
62 | uDto.setSurname(((User) a1).getSurname());
|
---|
63 |
|
---|
64 | uDto.setRetained(((User) a1).getRetainedSkills());
|
---|
65 | uDto.setToLearn(((User) a1).getSkillsToLearn());
|
---|
66 |
|
---|
67 | List<Job> matchedJobs = this.matchmakerService.getMatchingJobsForUser((User) a1);
|
---|
68 | List<Project> matchedProjects = this.matchmakerService.getMatchingProjectsForUser((User) a1);
|
---|
69 | List<Internship> matchedInternships = this.matchmakerService.getMatchingInternshipsForUser((User) a1);
|
---|
70 |
|
---|
71 | matchedJobs.forEach(job -> {
|
---|
72 | JobResponseDTO dto = new JobResponseDTO(job);
|
---|
73 | uDto.getJobs().add(dto);
|
---|
74 | });
|
---|
75 |
|
---|
76 | matchedProjects.forEach(project -> {
|
---|
77 | ProjectResponseDTO dto = new ProjectResponseDTO(project);
|
---|
78 | uDto.getProjects().add(dto);
|
---|
79 | });
|
---|
80 |
|
---|
81 | matchedInternships.forEach(internship -> {
|
---|
82 | InternshipResponseDTO dto = new InternshipResponseDTO(internship);
|
---|
83 | uDto.getInternships().add(dto);
|
---|
84 | });
|
---|
85 |
|
---|
86 |
|
---|
87 | return uDto;
|
---|
88 | }
|
---|
89 |
|
---|
90 | private TeamResponseDTO processTeam(Account a1){
|
---|
91 | TeamResponseDTO tDto = new TeamResponseDTO();
|
---|
92 |
|
---|
93 | tDto.setError(null);
|
---|
94 |
|
---|
95 | tDto.setId(a1.getId());
|
---|
96 | tDto.setEmail(a1.getEmail());
|
---|
97 | tDto.setName(a1.getName());
|
---|
98 | tDto.setType(AccountType.TEAM);
|
---|
99 | tDto.setMembers(((Team) a1).getMembers());
|
---|
100 |
|
---|
101 | List<Job> jobs = this.workService.getAllJobsByAccount(a1.getId());
|
---|
102 | List<Project> projects = this.workService.getAllProjectsByAccount(a1.getId());
|
---|
103 |
|
---|
104 | jobs.forEach(job -> {
|
---|
105 | JobResponseDTO dto = new JobResponseDTO(job);
|
---|
106 | tDto.getJobs().add(dto);
|
---|
107 | });
|
---|
108 |
|
---|
109 | projects.forEach(project -> {
|
---|
110 | ProjectResponseDTO dto = new ProjectResponseDTO(project);
|
---|
111 | tDto.getProjects().add(dto);
|
---|
112 | });
|
---|
113 |
|
---|
114 | return tDto;
|
---|
115 | }
|
---|
116 |
|
---|
117 | private CompanyResponseDTO processCompany(Account a1){
|
---|
118 | CompanyResponseDTO cDto = new CompanyResponseDTO();
|
---|
119 |
|
---|
120 | cDto.setError(null);
|
---|
121 |
|
---|
122 | cDto.setId(a1.getId());
|
---|
123 | cDto.setEmail(a1.getEmail());
|
---|
124 | cDto.setName(a1.getName());
|
---|
125 | cDto.setType(AccountType.COMPANY);
|
---|
126 | cDto.setAddress(((Company) a1).getAddress());
|
---|
127 |
|
---|
128 | List<Job> jobs = this.workService.getAllJobsByAccount(a1.getId());
|
---|
129 | List<Internship> internships = this.workService.getAllInternshipsByAccount(a1.getId());
|
---|
130 |
|
---|
131 | jobs.forEach(job -> {
|
---|
132 | JobResponseDTO dto = new JobResponseDTO(job);
|
---|
133 | cDto.getJobs().add(dto);
|
---|
134 | });
|
---|
135 |
|
---|
136 | internships.forEach(internship -> {
|
---|
137 | InternshipResponseDTO dto = new InternshipResponseDTO(internship);
|
---|
138 | cDto.getInternships().add(dto);
|
---|
139 | });
|
---|
140 |
|
---|
141 | return cDto;
|
---|
142 | }
|
---|
143 |
|
---|
144 | }
|
---|