source: src/main/java/it/finki/tinki/web/controller/LoginController.java@ fc8421e

Last change on this file since fc8421e was 17abe5e, checked in by i-ina <76742075+i-ina@…>, 3 years ago

react components for details and bugfix

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