source: src/main/java/it/finki/tinki/service/impl/AccountServiceImpl.java@ 4a15c9c

Last change on this file since 4a15c9c was 4a15c9c, checked in by i-ina <76742075+i-ina@…>, 3 years ago

added user registration

  • Property mode set to 100644
File size: 3.9 KB
Line 
1package it.finki.tinki.service.impl;
2
3import it.finki.tinki.model.Address;
4import it.finki.tinki.model.Skill;
5import it.finki.tinki.model.Users.Account;
6import it.finki.tinki.model.Users.Company;
7import it.finki.tinki.model.Users.Team;
8import it.finki.tinki.model.Users.User;
9import it.finki.tinki.model.enumerator.AccountType;
10import it.finki.tinki.model.exception.InvalidArgumentsException;
11import it.finki.tinki.model.exception.UserExistsException;
12import it.finki.tinki.repository.AddressRepository;
13import it.finki.tinki.repository.CompanyRepository;
14import it.finki.tinki.repository.TeamRepository;
15import it.finki.tinki.repository.UserRepository;
16import it.finki.tinki.service.AccountService;
17import org.springframework.stereotype.Service;
18
19import java.util.HashMap;
20import java.util.List;
21import java.util.Map;
22
23@Service
24public class AccountServiceImpl implements AccountService {
25
26 AddressRepository addressRepository;
27 UserRepository userRepository;
28 TeamRepository teamRepository;
29 CompanyRepository companyRepository;
30
31 public AccountServiceImpl(AddressRepository addressRepository, UserRepository userRepository, TeamRepository teamRepository, CompanyRepository companyRepository) {
32 this.addressRepository = addressRepository;
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 = this.userRepository.findByEmailAndPassword(email, password);
43 return u1;
44 }
45 else if(type.equals(AccountType.TEAM)){
46 Team t1 = this.teamRepository.findByEmailAndPassword(email, password);
47 return t1;
48 }
49 else if(type.equals(AccountType.COMPANY)){
50 Company c1 = this.companyRepository.findByEmailAndPassword(email, password);
51 return c1;
52 }
53
54 return null;
55 }
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 }
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 }
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 }
98}
Note: See TracBrowser for help on using the repository browser.