[29c0ed0] | 1 | package it.finki.tinki.service.impl;
|
---|
| 2 |
|
---|
[7edede9] | 3 | import it.finki.tinki.model.Address;
|
---|
| 4 | import it.finki.tinki.model.Skill;
|
---|
[29c0ed0] | 5 | import it.finki.tinki.model.Users.Account;
|
---|
| 6 | import it.finki.tinki.model.Users.Company;
|
---|
| 7 | import it.finki.tinki.model.Users.Team;
|
---|
| 8 | import it.finki.tinki.model.Users.User;
|
---|
| 9 | import it.finki.tinki.model.enumerator.AccountType;
|
---|
[7edede9] | 10 | import it.finki.tinki.model.exception.InvalidArgumentsException;
|
---|
| 11 | import it.finki.tinki.model.exception.UserExistsException;
|
---|
| 12 | import it.finki.tinki.repository.AddressRepository;
|
---|
[29c0ed0] | 13 | import it.finki.tinki.repository.CompanyRepository;
|
---|
| 14 | import it.finki.tinki.repository.TeamRepository;
|
---|
| 15 | import it.finki.tinki.repository.UserRepository;
|
---|
| 16 | import it.finki.tinki.service.AccountService;
|
---|
| 17 | import org.springframework.stereotype.Service;
|
---|
| 18 |
|
---|
| 19 | import java.util.HashMap;
|
---|
[7edede9] | 20 | import java.util.List;
|
---|
[29c0ed0] | 21 | import java.util.Map;
|
---|
| 22 |
|
---|
| 23 | @Service
|
---|
| 24 | public class AccountServiceImpl implements AccountService {
|
---|
| 25 |
|
---|
[7edede9] | 26 | AddressRepository addressRepository;
|
---|
[29c0ed0] | 27 | UserRepository userRepository;
|
---|
| 28 | TeamRepository teamRepository;
|
---|
| 29 | CompanyRepository companyRepository;
|
---|
| 30 |
|
---|
[7edede9] | 31 | public AccountServiceImpl(AddressRepository addressRepository, UserRepository userRepository, TeamRepository teamRepository, CompanyRepository companyRepository) {
|
---|
| 32 | this.addressRepository = addressRepository;
|
---|
[29c0ed0] | 33 | this.userRepository = userRepository;
|
---|
| 34 | this.teamRepository = teamRepository;
|
---|
| 35 | this.companyRepository = companyRepository;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | @Override
|
---|
| 39 | public Account findUser(String email, String password, AccountType type) {
|
---|
| 40 |
|
---|
| 41 | if(type.equals(AccountType.USER)){
|
---|
[4a15c9c] | 42 | User u1 = this.userRepository.findByEmailAndPassword(email, password);
|
---|
[7edede9] | 43 | return u1;
|
---|
[29c0ed0] | 44 | }
|
---|
| 45 | else if(type.equals(AccountType.TEAM)){
|
---|
[4a15c9c] | 46 | Team t1 = this.teamRepository.findByEmailAndPassword(email, password);
|
---|
[7edede9] | 47 | return t1;
|
---|
[29c0ed0] | 48 | }
|
---|
| 49 | else if(type.equals(AccountType.COMPANY)){
|
---|
[4a15c9c] | 50 | Company c1 = this.companyRepository.findByEmailAndPassword(email, password);
|
---|
[7edede9] | 51 | return c1;
|
---|
[29c0ed0] | 52 | }
|
---|
| 53 |
|
---|
| 54 | return null;
|
---|
| 55 | }
|
---|
[7edede9] | 56 |
|
---|
| 57 | public Account registerUser(String email, String password, String name, String surname, List<Skill> retainedSkills, List<Skill> skillsToLearn){
|
---|
| 58 | if(email==null || email.isEmpty() || password==null || password.isEmpty() || name==null || name.isEmpty() || surname==null || surname.isEmpty()){
|
---|
| 59 | throw new InvalidArgumentsException();
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | if(this.userRepository.findByEmail(email).isPresent()){
|
---|
| 63 | throw new UserExistsException();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | User u = new User(email, password, name, AccountType.USER, surname, retainedSkills, skillsToLearn);
|
---|
| 67 | return this.userRepository.save(u);
|
---|
| 68 | }
|
---|
[504c3ef] | 69 |
|
---|
| 70 | public Account registerTeam(String email, String password, String name, int members){
|
---|
| 71 | if(email==null || email.isEmpty() || password==null || password.isEmpty() || name==null || name.isEmpty()){
|
---|
| 72 | throw new InvalidArgumentsException();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | if(this.teamRepository.findByEmail(email).isPresent()){
|
---|
| 76 | throw new UserExistsException();
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | Team t = new Team(email, password, name, AccountType.TEAM, members);
|
---|
| 80 | return this.teamRepository.save(t);
|
---|
| 81 | }
|
---|
[721cb87] | 82 |
|
---|
| 83 | public Account registerCompany(String email, String password, String name, String country, String city, String street){
|
---|
| 84 | if(email==null || email.isEmpty() || password==null || password.isEmpty() || name==null || name.isEmpty()
|
---|
| 85 | || country==null || country.isEmpty() || city==null || city.isEmpty() || street==null || street.isEmpty()){
|
---|
| 86 | throw new InvalidArgumentsException();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | if(this.companyRepository.findByEmail(email).isPresent()){
|
---|
| 90 | throw new UserExistsException();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | Address a = new Address(country, city, street);
|
---|
| 94 | this.addressRepository.save(a);
|
---|
| 95 | Company c = new Company(email, password, name, AccountType.COMPANY, a);
|
---|
| 96 | return this.companyRepository.save(c);
|
---|
| 97 | }
|
---|
[509cb95] | 98 |
|
---|
| 99 | public Account findByIdAndType(Long id, AccountType type){
|
---|
| 100 | switch (type){
|
---|
| 101 | case COMPANY:
|
---|
| 102 | return this.companyRepository.findById(id).get();
|
---|
| 103 | case TEAM:
|
---|
| 104 | return this.teamRepository.findById(id).get();
|
---|
| 105 | case USER:
|
---|
| 106 | return this.userRepository.findById(id).get();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | return null;
|
---|
| 110 | }
|
---|
[29c0ed0] | 111 | }
|
---|