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