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