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