1 | package it.finki.tinki.service.impl;
|
---|
2 |
|
---|
3 | import it.finki.tinki.model.Address;
|
---|
4 | import it.finki.tinki.model.Jobs.Internship;
|
---|
5 | import it.finki.tinki.model.Jobs.Job;
|
---|
6 | import it.finki.tinki.model.Jobs.Project;
|
---|
7 | import it.finki.tinki.model.Skill;
|
---|
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;
|
---|
13 | import it.finki.tinki.model.exception.InvalidArgumentsException;
|
---|
14 | import it.finki.tinki.model.exception.UserExistsException;
|
---|
15 | import it.finki.tinki.repository.*;
|
---|
16 | import it.finki.tinki.service.AccountService;
|
---|
17 | import it.finki.tinki.service.MatchmakerService;
|
---|
18 | import it.finki.tinki.service.WorkService;
|
---|
19 | import org.springframework.stereotype.Service;
|
---|
20 |
|
---|
21 | import java.util.HashMap;
|
---|
22 | import java.util.List;
|
---|
23 | import java.util.Map;
|
---|
24 |
|
---|
25 | @Service
|
---|
26 | public class AccountServiceImpl implements AccountService {
|
---|
27 |
|
---|
28 | AddressRepository addressRepository;
|
---|
29 | UserRepository userRepository;
|
---|
30 | TeamRepository teamRepository;
|
---|
31 | CompanyRepository companyRepository;
|
---|
32 | JobRepository jobRepository;
|
---|
33 | ProjectRepository projectRepository;
|
---|
34 | InternshipRepository internshipRepository;
|
---|
35 | MatchmakerService matchmakerService;
|
---|
36 |
|
---|
37 | public AccountServiceImpl(AddressRepository addressRepository,
|
---|
38 | UserRepository userRepository,
|
---|
39 | TeamRepository teamRepository,
|
---|
40 | CompanyRepository companyRepository,
|
---|
41 | MatchmakerService matchmakerService,
|
---|
42 | JobRepository jobRepository,
|
---|
43 | ProjectRepository projectRepository,
|
---|
44 | InternshipRepository internshipRepository) {
|
---|
45 | this.addressRepository = addressRepository;
|
---|
46 | this.userRepository = userRepository;
|
---|
47 | this.teamRepository = teamRepository;
|
---|
48 | this.companyRepository = companyRepository;
|
---|
49 | this.jobRepository = jobRepository;
|
---|
50 | this.projectRepository = projectRepository;
|
---|
51 | this.internshipRepository = internshipRepository;
|
---|
52 | this.matchmakerService = matchmakerService;
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public Account findUser(String email, String password, AccountType type) {
|
---|
57 |
|
---|
58 | if(type.equals(AccountType.USER)){
|
---|
59 | User u1 = this.userRepository.findByEmailAndPassword(email, password);
|
---|
60 | return u1;
|
---|
61 | }
|
---|
62 | else if(type.equals(AccountType.TEAM)){
|
---|
63 | Team t1 = this.teamRepository.findByEmailAndPassword(email, password);
|
---|
64 | return t1;
|
---|
65 | }
|
---|
66 | else if(type.equals(AccountType.COMPANY)){
|
---|
67 | Company c1 = this.companyRepository.findByEmailAndPassword(email, password);
|
---|
68 | return c1;
|
---|
69 | }
|
---|
70 |
|
---|
71 | return null;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public Account registerUser(String email, String password, String name, String surname, List<Skill> retainedSkills, List<Skill> skillsToLearn){
|
---|
75 | if(email==null || email.isEmpty() || password==null || password.isEmpty() || name==null || name.isEmpty() || surname==null || surname.isEmpty()){
|
---|
76 | throw new InvalidArgumentsException();
|
---|
77 | }
|
---|
78 |
|
---|
79 | if(this.userRepository.findByEmail(email).isPresent()){
|
---|
80 | throw new UserExistsException();
|
---|
81 | }
|
---|
82 |
|
---|
83 | User u = new User(email, password, name, AccountType.USER, surname, retainedSkills, skillsToLearn);
|
---|
84 | User ru = this.userRepository.save(u);
|
---|
85 |
|
---|
86 | List<Job> jobs = this.jobRepository.findAll();
|
---|
87 | List<Project> projects = this.projectRepository.findAll();
|
---|
88 | List<Internship> internships = this.internshipRepository.findAll();
|
---|
89 |
|
---|
90 | if(jobs.size()!=0){
|
---|
91 | for (Job job : jobs) {
|
---|
92 | this.matchmakerService.setUpUserJobMatches(job, u);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | if(projects.size()!=0){
|
---|
97 | for (Project project : projects) {
|
---|
98 | this.matchmakerService.setUpUserProjectMatches(project, u);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | if(internships.size()!=0){
|
---|
103 | for(Internship internship : internships){
|
---|
104 | this.matchmakerService.setUpUserInternshipMatches(internship, u);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | return ru;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public Account registerTeam(String email, String password, String name, int members){
|
---|
112 | if(email==null || email.isEmpty() || password==null || password.isEmpty() || name==null || name.isEmpty()){
|
---|
113 | throw new InvalidArgumentsException();
|
---|
114 | }
|
---|
115 |
|
---|
116 | if(this.teamRepository.findByEmail(email).isPresent()){
|
---|
117 | throw new UserExistsException();
|
---|
118 | }
|
---|
119 |
|
---|
120 | Team t = new Team(email, password, name, AccountType.TEAM, members);
|
---|
121 | return this.teamRepository.save(t);
|
---|
122 | }
|
---|
123 |
|
---|
124 | public Account registerCompany(String email, String password, String name, String country, String city, String street){
|
---|
125 | if(email==null || email.isEmpty() || password==null || password.isEmpty() || name==null || name.isEmpty()
|
---|
126 | || country==null || country.isEmpty() || city==null || city.isEmpty() || street==null || street.isEmpty()){
|
---|
127 | throw new InvalidArgumentsException();
|
---|
128 | }
|
---|
129 |
|
---|
130 | if(this.companyRepository.findByEmail(email).isPresent()){
|
---|
131 | throw new UserExistsException();
|
---|
132 | }
|
---|
133 |
|
---|
134 | Address a = new Address(country, city, street);
|
---|
135 | this.addressRepository.save(a);
|
---|
136 | Company c = new Company(email, password, name, AccountType.COMPANY, a);
|
---|
137 | return this.companyRepository.save(c);
|
---|
138 | }
|
---|
139 |
|
---|
140 | public Account findByIdAndType(Long id, AccountType type){
|
---|
141 | switch (type){
|
---|
142 | case COMPANY:
|
---|
143 | return this.companyRepository.findById(id).get();
|
---|
144 | case TEAM:
|
---|
145 | return this.teamRepository.findById(id).get();
|
---|
146 | case USER:
|
---|
147 | return this.userRepository.findById(id).get();
|
---|
148 | }
|
---|
149 |
|
---|
150 | return null;
|
---|
151 | }
|
---|
152 | }
|
---|