[29c0ed0] | 1 | package it.finki.tinki.service.impl;
|
---|
| 2 |
|
---|
[7edede9] | 3 | import it.finki.tinki.model.Address;
|
---|
[a8e8545] | 4 | import it.finki.tinki.model.Jobs.Internship;
|
---|
| 5 | import it.finki.tinki.model.Jobs.Job;
|
---|
| 6 | import it.finki.tinki.model.Jobs.Project;
|
---|
[7edede9] | 7 | import it.finki.tinki.model.Skill;
|
---|
[29c0ed0] | 8 | import it.finki.tinki.model.Users.Account;
|
---|
| 9 | import it.finki.tinki.model.Users.Company;
|
---|
| 10 | import it.finki.tinki.model.Users.Team;
|
---|
| 11 | import it.finki.tinki.model.Users.User;
|
---|
| 12 | import it.finki.tinki.model.enumerator.AccountType;
|
---|
[7edede9] | 13 | import it.finki.tinki.model.exception.InvalidArgumentsException;
|
---|
| 14 | import it.finki.tinki.model.exception.UserExistsException;
|
---|
[a8e8545] | 15 | import it.finki.tinki.repository.*;
|
---|
[29c0ed0] | 16 | import it.finki.tinki.service.AccountService;
|
---|
[a8e8545] | 17 | import it.finki.tinki.service.MatchmakerService;
|
---|
[29c0ed0] | 18 | import org.springframework.stereotype.Service;
|
---|
| 19 |
|
---|
[7edede9] | 20 | import java.util.List;
|
---|
[29c0ed0] | 21 |
|
---|
| 22 | @Service
|
---|
| 23 | public class AccountServiceImpl implements AccountService {
|
---|
| 24 |
|
---|
[7edede9] | 25 | AddressRepository addressRepository;
|
---|
[29c0ed0] | 26 | UserRepository userRepository;
|
---|
| 27 | TeamRepository teamRepository;
|
---|
| 28 | CompanyRepository companyRepository;
|
---|
[a8e8545] | 29 | JobRepository jobRepository;
|
---|
| 30 | ProjectRepository projectRepository;
|
---|
| 31 | InternshipRepository internshipRepository;
|
---|
| 32 | MatchmakerService matchmakerService;
|
---|
[29c0ed0] | 33 |
|
---|
[a8e8545] | 34 | public AccountServiceImpl(AddressRepository addressRepository,
|
---|
| 35 | UserRepository userRepository,
|
---|
| 36 | TeamRepository teamRepository,
|
---|
| 37 | CompanyRepository companyRepository,
|
---|
| 38 | MatchmakerService matchmakerService,
|
---|
| 39 | JobRepository jobRepository,
|
---|
| 40 | ProjectRepository projectRepository,
|
---|
| 41 | InternshipRepository internshipRepository) {
|
---|
[7edede9] | 42 | this.addressRepository = addressRepository;
|
---|
[29c0ed0] | 43 | this.userRepository = userRepository;
|
---|
| 44 | this.teamRepository = teamRepository;
|
---|
| 45 | this.companyRepository = companyRepository;
|
---|
[a8e8545] | 46 | this.jobRepository = jobRepository;
|
---|
| 47 | this.projectRepository = projectRepository;
|
---|
| 48 | this.internshipRepository = internshipRepository;
|
---|
| 49 | this.matchmakerService = matchmakerService;
|
---|
[29c0ed0] | 50 | }
|
---|
| 51 |
|
---|
| 52 | @Override
|
---|
| 53 | public Account findUser(String email, String password, AccountType type) {
|
---|
| 54 |
|
---|
| 55 | if(type.equals(AccountType.USER)){
|
---|
[4a15c9c] | 56 | User u1 = this.userRepository.findByEmailAndPassword(email, password);
|
---|
[7edede9] | 57 | return u1;
|
---|
[29c0ed0] | 58 | }
|
---|
| 59 | else if(type.equals(AccountType.TEAM)){
|
---|
[4a15c9c] | 60 | Team t1 = this.teamRepository.findByEmailAndPassword(email, password);
|
---|
[7edede9] | 61 | return t1;
|
---|
[29c0ed0] | 62 | }
|
---|
| 63 | else if(type.equals(AccountType.COMPANY)){
|
---|
[4a15c9c] | 64 | Company c1 = this.companyRepository.findByEmailAndPassword(email, password);
|
---|
[7edede9] | 65 | return c1;
|
---|
[29c0ed0] | 66 | }
|
---|
| 67 |
|
---|
| 68 | return null;
|
---|
| 69 | }
|
---|
[7edede9] | 70 |
|
---|
| 71 | public Account registerUser(String email, String password, String name, String surname, List<Skill> retainedSkills, List<Skill> skillsToLearn){
|
---|
| 72 | if(email==null || email.isEmpty() || password==null || password.isEmpty() || name==null || name.isEmpty() || surname==null || surname.isEmpty()){
|
---|
| 73 | throw new InvalidArgumentsException();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | if(this.userRepository.findByEmail(email).isPresent()){
|
---|
| 77 | throw new UserExistsException();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | User u = new User(email, password, name, AccountType.USER, surname, retainedSkills, skillsToLearn);
|
---|
[a8e8545] | 81 | User ru = this.userRepository.save(u);
|
---|
| 82 |
|
---|
| 83 | List<Job> jobs = this.jobRepository.findAll();
|
---|
| 84 | List<Project> projects = this.projectRepository.findAll();
|
---|
| 85 | List<Internship> internships = this.internshipRepository.findAll();
|
---|
| 86 |
|
---|
| 87 | if(jobs.size()!=0){
|
---|
| 88 | for (Job job : jobs) {
|
---|
| 89 | this.matchmakerService.setUpUserJobMatches(job, u);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | if(projects.size()!=0){
|
---|
| 94 | for (Project project : projects) {
|
---|
| 95 | this.matchmakerService.setUpUserProjectMatches(project, u);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | if(internships.size()!=0){
|
---|
| 100 | for(Internship internship : internships){
|
---|
| 101 | this.matchmakerService.setUpUserInternshipMatches(internship, u);
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | return ru;
|
---|
[7edede9] | 106 | }
|
---|
[504c3ef] | 107 |
|
---|
| 108 | public Account registerTeam(String email, String password, String name, int members){
|
---|
| 109 | if(email==null || email.isEmpty() || password==null || password.isEmpty() || name==null || name.isEmpty()){
|
---|
| 110 | throw new InvalidArgumentsException();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | if(this.teamRepository.findByEmail(email).isPresent()){
|
---|
| 114 | throw new UserExistsException();
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | Team t = new Team(email, password, name, AccountType.TEAM, members);
|
---|
| 118 | return this.teamRepository.save(t);
|
---|
| 119 | }
|
---|
[721cb87] | 120 |
|
---|
| 121 | public Account registerCompany(String email, String password, String name, String country, String city, String street){
|
---|
| 122 | if(email==null || email.isEmpty() || password==null || password.isEmpty() || name==null || name.isEmpty()
|
---|
| 123 | || country==null || country.isEmpty() || city==null || city.isEmpty() || street==null || street.isEmpty()){
|
---|
| 124 | throw new InvalidArgumentsException();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | if(this.companyRepository.findByEmail(email).isPresent()){
|
---|
| 128 | throw new UserExistsException();
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | Address a = new Address(country, city, street);
|
---|
| 132 | this.addressRepository.save(a);
|
---|
| 133 | Company c = new Company(email, password, name, AccountType.COMPANY, a);
|
---|
| 134 | return this.companyRepository.save(c);
|
---|
| 135 | }
|
---|
[509cb95] | 136 |
|
---|
| 137 | public Account findByIdAndType(Long id, AccountType type){
|
---|
| 138 | switch (type){
|
---|
| 139 | case COMPANY:
|
---|
| 140 | return this.companyRepository.findById(id).get();
|
---|
| 141 | case TEAM:
|
---|
| 142 | return this.teamRepository.findById(id).get();
|
---|
| 143 | case USER:
|
---|
| 144 | return this.userRepository.findById(id).get();
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | return null;
|
---|
| 148 | }
|
---|
[29c0ed0] | 149 | }
|
---|