[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)){
|
---|
| 42 | User u1 = userRepository.findByEmailAndPassword(email, password);
|
---|
[7edede9] | 43 | return u1;
|
---|
[29c0ed0] | 44 | }
|
---|
| 45 | else if(type.equals(AccountType.TEAM)){
|
---|
| 46 | Team t1 = teamRepository.findByEmailAndPassword(email, password);
|
---|
[7edede9] | 47 | return t1;
|
---|
[29c0ed0] | 48 | }
|
---|
| 49 | else if(type.equals(AccountType.COMPANY)){
|
---|
| 50 | Company c1 = 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 | }
|
---|
[29c0ed0] | 69 | }
|
---|