[29c0ed0] | 1 | package it.finki.tinki.service.impl;
|
---|
| 2 |
|
---|
[7edede9] | 3 | import it.finki.tinki.model.Address;
|
---|
[bd46dbb] | 4 | import it.finki.tinki.model.Work.Internship;
|
---|
| 5 | import it.finki.tinki.model.Work.Job;
|
---|
| 6 | import it.finki.tinki.model.Work.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;
|
---|
[14b648e] | 21 | import java.util.Optional;
|
---|
[29c0ed0] | 22 |
|
---|
| 23 | @Service
|
---|
| 24 | public class AccountServiceImpl implements AccountService {
|
---|
| 25 |
|
---|
[7edede9] | 26 | AddressRepository addressRepository;
|
---|
[29c0ed0] | 27 | UserRepository userRepository;
|
---|
| 28 | TeamRepository teamRepository;
|
---|
| 29 | CompanyRepository companyRepository;
|
---|
[a8e8545] | 30 | JobRepository jobRepository;
|
---|
| 31 | ProjectRepository projectRepository;
|
---|
| 32 | InternshipRepository internshipRepository;
|
---|
| 33 | MatchmakerService matchmakerService;
|
---|
[29c0ed0] | 34 |
|
---|
[a8e8545] | 35 | public AccountServiceImpl(AddressRepository addressRepository,
|
---|
| 36 | UserRepository userRepository,
|
---|
| 37 | TeamRepository teamRepository,
|
---|
| 38 | CompanyRepository companyRepository,
|
---|
| 39 | MatchmakerService matchmakerService,
|
---|
| 40 | JobRepository jobRepository,
|
---|
| 41 | ProjectRepository projectRepository,
|
---|
| 42 | InternshipRepository internshipRepository) {
|
---|
[7edede9] | 43 | this.addressRepository = addressRepository;
|
---|
[29c0ed0] | 44 | this.userRepository = userRepository;
|
---|
| 45 | this.teamRepository = teamRepository;
|
---|
| 46 | this.companyRepository = companyRepository;
|
---|
[a8e8545] | 47 | this.jobRepository = jobRepository;
|
---|
| 48 | this.projectRepository = projectRepository;
|
---|
| 49 | this.internshipRepository = internshipRepository;
|
---|
| 50 | this.matchmakerService = matchmakerService;
|
---|
[29c0ed0] | 51 | }
|
---|
| 52 |
|
---|
| 53 | @Override
|
---|
| 54 | public Account findUser(String email, String password, AccountType type) {
|
---|
| 55 |
|
---|
| 56 | if(type.equals(AccountType.USER)){
|
---|
[4a15c9c] | 57 | User u1 = this.userRepository.findByEmailAndPassword(email, password);
|
---|
[7edede9] | 58 | return u1;
|
---|
[29c0ed0] | 59 | }
|
---|
| 60 | else if(type.equals(AccountType.TEAM)){
|
---|
[4a15c9c] | 61 | Team t1 = this.teamRepository.findByEmailAndPassword(email, password);
|
---|
[7edede9] | 62 | return t1;
|
---|
[29c0ed0] | 63 | }
|
---|
| 64 | else if(type.equals(AccountType.COMPANY)){
|
---|
[4a15c9c] | 65 | Company c1 = this.companyRepository.findByEmailAndPassword(email, password);
|
---|
[7edede9] | 66 | return c1;
|
---|
[29c0ed0] | 67 | }
|
---|
| 68 |
|
---|
| 69 | return null;
|
---|
| 70 | }
|
---|
[7edede9] | 71 |
|
---|
| 72 | public Account registerUser(String email, String password, String name, String surname, List<Skill> retainedSkills, List<Skill> skillsToLearn){
|
---|
| 73 | if(email==null || email.isEmpty() || password==null || password.isEmpty() || name==null || name.isEmpty() || surname==null || surname.isEmpty()){
|
---|
| 74 | throw new InvalidArgumentsException();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | if(this.userRepository.findByEmail(email).isPresent()){
|
---|
| 78 | throw new UserExistsException();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | User u = new User(email, password, name, AccountType.USER, surname, retainedSkills, skillsToLearn);
|
---|
[a8e8545] | 82 | User ru = this.userRepository.save(u);
|
---|
| 83 |
|
---|
[14b648e] | 84 | setUpUser(ru);
|
---|
[a8e8545] | 85 |
|
---|
| 86 | return ru;
|
---|
[7edede9] | 87 | }
|
---|
[504c3ef] | 88 |
|
---|
| 89 | public Account registerTeam(String email, String password, String name, int members){
|
---|
| 90 | if(email==null || email.isEmpty() || password==null || password.isEmpty() || name==null || name.isEmpty()){
|
---|
| 91 | throw new InvalidArgumentsException();
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | if(this.teamRepository.findByEmail(email).isPresent()){
|
---|
| 95 | throw new UserExistsException();
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | Team t = new Team(email, password, name, AccountType.TEAM, members);
|
---|
| 99 | return this.teamRepository.save(t);
|
---|
| 100 | }
|
---|
[721cb87] | 101 |
|
---|
| 102 | public Account registerCompany(String email, String password, String name, String country, String city, String street){
|
---|
| 103 | if(email==null || email.isEmpty() || password==null || password.isEmpty() || name==null || name.isEmpty()
|
---|
| 104 | || country==null || country.isEmpty() || city==null || city.isEmpty() || street==null || street.isEmpty()){
|
---|
| 105 | throw new InvalidArgumentsException();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | if(this.companyRepository.findByEmail(email).isPresent()){
|
---|
| 109 | throw new UserExistsException();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | Address a = new Address(country, city, street);
|
---|
| 113 | this.addressRepository.save(a);
|
---|
| 114 | Company c = new Company(email, password, name, AccountType.COMPANY, a);
|
---|
| 115 | return this.companyRepository.save(c);
|
---|
| 116 | }
|
---|
[509cb95] | 117 |
|
---|
| 118 | public Account findByIdAndType(Long id, AccountType type){
|
---|
| 119 | switch (type){
|
---|
| 120 | case COMPANY:
|
---|
| 121 | return this.companyRepository.findById(id).get();
|
---|
| 122 | case TEAM:
|
---|
| 123 | return this.teamRepository.findById(id).get();
|
---|
| 124 | case USER:
|
---|
| 125 | return this.userRepository.findById(id).get();
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | return null;
|
---|
| 129 | }
|
---|
[14b648e] | 130 |
|
---|
| 131 | public Optional<?> findByIdAndEmail(Long id, String email, AccountType accountType){
|
---|
| 132 |
|
---|
| 133 | switch (accountType){
|
---|
| 134 | case USER:
|
---|
| 135 | return this.userRepository.findByIdAndEmail(id, email);
|
---|
| 136 | case TEAM:
|
---|
| 137 | return this.teamRepository.findByIdAndEmail(id, email);
|
---|
| 138 | case COMPANY:
|
---|
| 139 | return this.companyRepository.findByIdAndEmail(id, email);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | return Optional.empty();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | public User editUser(Long id, String email, String name, String surname, List<Skill> retainedSkills, List<Skill> skillsToLearn){
|
---|
| 146 | if(email==null || email.isEmpty() || name==null || name.isEmpty() || surname==null || surname.isEmpty()){
|
---|
| 147 | throw new InvalidArgumentsException();
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | User u = this.userRepository.findById(id).get();
|
---|
| 151 | Optional<User> t = this.userRepository.findByEmail(email);
|
---|
| 152 |
|
---|
| 153 | if(t.isPresent() && !t.get().equals(u)){
|
---|
| 154 | throw new UserExistsException();
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | u.setEmail(email);
|
---|
| 158 | u.setName(name);
|
---|
| 159 | u.setSurname(surname);
|
---|
| 160 | u.setRetainedSkills(retainedSkills);
|
---|
| 161 | u.setSkillsToLearn(skillsToLearn);
|
---|
| 162 |
|
---|
| 163 | User m = this.userRepository.save(u);
|
---|
| 164 |
|
---|
| 165 | setUpUser(m);
|
---|
| 166 |
|
---|
| 167 | return m;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | public Company editCompany(Long id, String email, String name, String country, String city, String street){
|
---|
| 171 | if(email==null || email.isEmpty() || name==null || name.isEmpty()
|
---|
| 172 | || country==null || country.isEmpty() || city==null || city.isEmpty() || street==null || street.isEmpty()){
|
---|
| 173 | throw new InvalidArgumentsException();
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | Company c = this.companyRepository.findById(id).get();
|
---|
| 177 | Optional<Company> t = this.companyRepository.findByEmail(email);
|
---|
| 178 |
|
---|
| 179 | if(t.isPresent() && !t.get().equals(c)){
|
---|
| 180 | throw new UserExistsException();
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | c.setEmail(email);
|
---|
| 184 | c.setName(name);
|
---|
| 185 |
|
---|
| 186 | Address ad = c.getAddress();
|
---|
| 187 | ad.setCountry(country);
|
---|
| 188 | ad.setCity(city);
|
---|
| 189 | ad.setStreet(street);
|
---|
| 190 |
|
---|
| 191 | this.addressRepository.save(ad);
|
---|
| 192 | return this.companyRepository.save(c);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | public Team editTeam(Long id, String email, String name, int members){
|
---|
| 196 | if(email==null || email.isEmpty() || name==null || name.isEmpty()){
|
---|
| 197 | throw new InvalidArgumentsException();
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | Team t = this.teamRepository.findById(id).get();
|
---|
| 201 | Optional<Team> tt = this.teamRepository.findByEmail(email);
|
---|
| 202 |
|
---|
| 203 | if(tt.isPresent() && !tt.get().equals(t)){
|
---|
| 204 | throw new UserExistsException();
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | t.setEmail(email);
|
---|
| 208 | t.setName(name);
|
---|
| 209 | t.setMembers(members);
|
---|
| 210 |
|
---|
| 211 | return this.teamRepository.save(t);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 |
|
---|
| 215 | private void setUpUser(User u){
|
---|
| 216 | List<Job> jobs = this.jobRepository.findAll();
|
---|
| 217 | List<Project> projects = this.projectRepository.findAll();
|
---|
| 218 | List<Internship> internships = this.internshipRepository.findAll();
|
---|
| 219 |
|
---|
| 220 | if(jobs.size()!=0){
|
---|
| 221 | for (Job job : jobs) {
|
---|
| 222 | this.matchmakerService.setUpUserJobMatches(job, u);
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | if(projects.size()!=0){
|
---|
| 227 | for (Project project : projects) {
|
---|
| 228 | this.matchmakerService.setUpUserProjectMatches(project, u);
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | if(internships.size()!=0){
|
---|
| 233 | for(Internship internship : internships){
|
---|
| 234 | this.matchmakerService.setUpUserInternshipMatches(internship, u);
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
[29c0ed0] | 238 | }
|
---|