source: src/main/java/it/finki/tinki/service/impl/AccountServiceImpl.java@ 336d09e

Last change on this file since 336d09e was 336d09e, checked in by Vzdra <vladko.zdravkovski@…>, 3 years ago

cleanup

  • Property mode set to 100644
File size: 5.7 KB
Line 
1package it.finki.tinki.service.impl;
2
3import it.finki.tinki.model.Address;
4import it.finki.tinki.model.Jobs.Internship;
5import it.finki.tinki.model.Jobs.Job;
6import it.finki.tinki.model.Jobs.Project;
7import it.finki.tinki.model.Skill;
8import it.finki.tinki.model.Users.Account;
9import it.finki.tinki.model.Users.Company;
10import it.finki.tinki.model.Users.Team;
11import it.finki.tinki.model.Users.User;
12import it.finki.tinki.model.enumerator.AccountType;
13import it.finki.tinki.model.exception.InvalidArgumentsException;
14import it.finki.tinki.model.exception.UserExistsException;
15import it.finki.tinki.repository.*;
16import it.finki.tinki.service.AccountService;
17import it.finki.tinki.service.MatchmakerService;
18import org.springframework.stereotype.Service;
19
20import java.util.List;
21
22@Service
23public class AccountServiceImpl implements AccountService {
24
25 AddressRepository addressRepository;
26 UserRepository userRepository;
27 TeamRepository teamRepository;
28 CompanyRepository companyRepository;
29 JobRepository jobRepository;
30 ProjectRepository projectRepository;
31 InternshipRepository internshipRepository;
32 MatchmakerService matchmakerService;
33
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) {
42 this.addressRepository = addressRepository;
43 this.userRepository = userRepository;
44 this.teamRepository = teamRepository;
45 this.companyRepository = companyRepository;
46 this.jobRepository = jobRepository;
47 this.projectRepository = projectRepository;
48 this.internshipRepository = internshipRepository;
49 this.matchmakerService = matchmakerService;
50 }
51
52 @Override
53 public Account findUser(String email, String password, AccountType type) {
54
55 if(type.equals(AccountType.USER)){
56 User u1 = this.userRepository.findByEmailAndPassword(email, password);
57 return u1;
58 }
59 else if(type.equals(AccountType.TEAM)){
60 Team t1 = this.teamRepository.findByEmailAndPassword(email, password);
61 return t1;
62 }
63 else if(type.equals(AccountType.COMPANY)){
64 Company c1 = this.companyRepository.findByEmailAndPassword(email, password);
65 return c1;
66 }
67
68 return null;
69 }
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);
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;
106 }
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 }
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 }
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 }
149}
Note: See TracBrowser for help on using the repository browser.