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

Last change on this file since 4b1c93d was 4b1c93d, checked in by Vzdra <vladko.zdravkovski@…>, 3 years ago

added response types to team and company

  • Property mode set to 100644
File size: 4.8 KB
Line 
1package it.finki.tinki.web.controller;
2
3import it.finki.tinki.model.Jobs.Internship;
4import it.finki.tinki.model.Jobs.Job;
5import it.finki.tinki.model.Jobs.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.enumerator.AccountType;
12import it.finki.tinki.service.AccountService;
13import it.finki.tinki.service.MatchmakerService;
14import it.finki.tinki.service.WorkService;
15import org.springframework.web.bind.annotation.*;
16import org.springframework.web.server.ResponseStatusException;
17
18import java.util.List;
19
20@RestController
21@CrossOrigin(origins = "http://localhost:3000")
22@RequestMapping("/api")
23public 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 testPage(@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
43 UserResponseDTO uDto = new UserResponseDTO();
44
45 uDto.setError(null);
46
47 uDto.setId(a1.getId());
48 uDto.setEmail(a1.getEmail());
49 uDto.setName(a1.getName());
50 uDto.setType(AccountType.USER);
51 uDto.setSurname(((User) a1).getSurname());
52
53 uDto.setRetained(((User) a1).getRetainedSkills());
54 uDto.setToLearn(((User) a1).getSkillsToLearn());
55
56 List<Job> matchedJobs = this.matchmakerService.getMatchingJobsForUser((User) a1);
57 List<Project> matchedProjects = this.matchmakerService.getMatchingProjectsForUser((User) a1);
58 List<Internship> matchedInternships = this.matchmakerService.getMatchingInternshipsForUser((User) a1);
59
60 matchedJobs.forEach(job -> {
61 JobResponseDTO dto = new JobResponseDTO(job);
62 uDto.getJobs().add(dto);
63 });
64
65 matchedProjects.forEach(project -> {
66 ProjectResponseDTO dto = new ProjectResponseDTO(project);
67 uDto.getProjects().add(dto);
68 });
69
70 matchedInternships.forEach(internship -> {
71 InternshipResponseDTO dto = new InternshipResponseDTO(internship);
72 uDto.getInternships().add(dto);
73 });
74
75
76 return uDto;
77
78 }else if(a1.getClass().equals(Team.class)){
79
80 TeamResponseDTO tDto = new TeamResponseDTO();
81
82 tDto.setError(null);
83
84 tDto.setId(a1.getId());
85 tDto.setEmail(a1.getEmail());
86 tDto.setName(a1.getName());
87 tDto.setType(AccountType.TEAM);
88 tDto.setMembers(((Team) a1).getMembers());
89
90 List<Job> jobs = this.workService.getAllJobsByAccount(a1.getId());
91 List<Project> projects = this.workService.getAllProjectsByAccount(a1.getId());
92
93 jobs.forEach(job -> {
94 JobResponseDTO dto = new JobResponseDTO(job);
95 tDto.getJobs().add(dto);
96 });
97
98 projects.forEach(project -> {
99 ProjectResponseDTO dto = new ProjectResponseDTO(project);
100 tDto.getProjects().add(dto);
101 });
102
103 return tDto;
104
105 }else{
106
107 CompanyResponseDTO cDto = new CompanyResponseDTO();
108
109 cDto.setError(null);
110
111 cDto.setId(a1.getId());
112 cDto.setEmail(a1.getEmail());
113 cDto.setName(a1.getName());
114 cDto.setType(AccountType.COMPANY);
115 cDto.setAddress(((Company) a1).getAddress());
116
117 List<Job> jobs = this.workService.getAllJobsByAccount(a1.getId());
118 List<Internship> internships = this.workService.getAllInternshipsByAccount(a1.getId());
119
120 jobs.forEach(job -> {
121 JobResponseDTO dto = new JobResponseDTO(job);
122 cDto.getJobs().add(dto);
123 });
124
125 internships.forEach(internship -> {
126 InternshipResponseDTO dto = new InternshipResponseDTO(internship);
127 cDto.getInternships().add(dto);
128 });
129
130 return cDto;
131
132 }
133 }
134
135 return new LoginResponseDTO();
136 }
137
138}
Note: See TracBrowser for help on using the repository browser.