1 | package mk.ukim.finki.busngo.service.impl;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.busngo.model.entities.Kondukter;
|
---|
4 | import mk.ukim.finki.busngo.model.entities.Korisnik;
|
---|
5 | import mk.ukim.finki.busngo.model.entities.Patnik;
|
---|
6 | import mk.ukim.finki.busngo.model.entities.Vozac;
|
---|
7 | import mk.ukim.finki.busngo.model.enums.Role;
|
---|
8 | import mk.ukim.finki.busngo.model.enums.VrabotenType;
|
---|
9 | import mk.ukim.finki.busngo.model.exceptions.InvalidCredentialsException;
|
---|
10 | import mk.ukim.finki.busngo.model.exceptions.UserAlreadyExistsException;
|
---|
11 | import mk.ukim.finki.busngo.repository.KondukterRepository;
|
---|
12 | import mk.ukim.finki.busngo.repository.KorisnikRepository;
|
---|
13 | import mk.ukim.finki.busngo.repository.PatnikRepository;
|
---|
14 | import mk.ukim.finki.busngo.repository.VozacRepository;
|
---|
15 | import mk.ukim.finki.busngo.service.*;
|
---|
16 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
17 | import org.springframework.stereotype.Service;
|
---|
18 |
|
---|
19 | import java.sql.Date;
|
---|
20 | import java.time.LocalDate;
|
---|
21 |
|
---|
22 | @Service
|
---|
23 | public class AuthServiceImpl implements AuthService {
|
---|
24 | private final KorisnikRepository korisnikRepository;
|
---|
25 | private final PatnikRepository patnikRepository;
|
---|
26 | private final VozacRepository vozacRepository;
|
---|
27 | private final KondukterRepository kondukterRepository;
|
---|
28 | private final PasswordEncoder passwordEncoder;
|
---|
29 | private final PatnikService patnikService;
|
---|
30 | private final KondukterService kondukterService;
|
---|
31 | private final VozacService vozacService;
|
---|
32 |
|
---|
33 | public AuthServiceImpl(KorisnikRepository korisnikRepository, PatnikRepository patnikRepository, VozacRepository vozacRepository, KondukterRepository kondukterRepository, PasswordEncoder passwordEncoder, PatnikService patnikService, KondukterService kondukterService, VozacService vozacService) {
|
---|
34 | this.korisnikRepository = korisnikRepository;
|
---|
35 | this.patnikRepository = patnikRepository;
|
---|
36 | this.vozacRepository = vozacRepository;
|
---|
37 | this.kondukterRepository = kondukterRepository;
|
---|
38 | this.passwordEncoder = passwordEncoder;
|
---|
39 | this.patnikService = patnikService;
|
---|
40 | this.kondukterService = kondukterService;
|
---|
41 | this.vozacService = vozacService;
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public Korisnik login(String email, String password) {
|
---|
46 | if(email == null || password == null || email.isEmpty() || password.isEmpty())
|
---|
47 | throw new InvalidCredentialsException();
|
---|
48 | return korisnikRepository.findByKEmailAndKLozinka(email, password).orElseThrow(InvalidCredentialsException::new);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public Korisnik registerPatnik(String ime, String email, String password, String confirmPassword, String address, String telefon) {
|
---|
53 | if (email == null || password == null || email.isEmpty() || password.isEmpty()) {
|
---|
54 | throw new InvalidCredentialsException();
|
---|
55 | }
|
---|
56 |
|
---|
57 | if (!password.equals(confirmPassword)) {
|
---|
58 | throw new InvalidCredentialsException();
|
---|
59 | }
|
---|
60 |
|
---|
61 | if(this.korisnikRepository.findByKEmail(email).isPresent()) {
|
---|
62 | throw new UserAlreadyExistsException(email);
|
---|
63 | }
|
---|
64 |
|
---|
65 | Patnik korisnik = new Patnik();
|
---|
66 | korisnik.setKIme(ime);
|
---|
67 | korisnik.setKAdresa(address);
|
---|
68 | korisnik.setKLozinka(passwordEncoder.encode(password));
|
---|
69 | korisnik.setKEmail(email);
|
---|
70 | korisnik.setKTelefon(telefon);
|
---|
71 | korisnik.setKIsAdmin(false);
|
---|
72 |
|
---|
73 | return patnikRepository.save(korisnik);
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public Korisnik registerVraboten(String ime, String email, String password, String confirmPassword, String address, String telefon, VrabotenType type, Double salary) {
|
---|
78 | if (email == null || password == null || email.isEmpty() || password.isEmpty()) {
|
---|
79 | throw new InvalidCredentialsException();
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (!password.equals(confirmPassword)) {
|
---|
83 | throw new InvalidCredentialsException();
|
---|
84 | }
|
---|
85 |
|
---|
86 | if(this.korisnikRepository.findByKEmail(email).isPresent()) {
|
---|
87 | throw new UserAlreadyExistsException(email);
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | switch (type){
|
---|
92 | case ADMIN:
|
---|
93 | Korisnik korisnik = new Korisnik();
|
---|
94 | korisnik.setKIme(ime);
|
---|
95 | korisnik.setKAdresa(address);
|
---|
96 | korisnik.setKLozinka(passwordEncoder.encode(password));
|
---|
97 | korisnik.setKEmail(email);
|
---|
98 | korisnik.setKTelefon(telefon);
|
---|
99 | korisnik.setKIsAdmin(true);
|
---|
100 | return korisnikRepository.save(korisnik);
|
---|
101 | case VOZAC:
|
---|
102 | Vozac vozac = new Vozac();
|
---|
103 | vozac.setKIme(ime);
|
---|
104 | vozac.setKAdresa(address);
|
---|
105 | vozac.setKLozinka(passwordEncoder.encode(password));
|
---|
106 | vozac.setKEmail(email);
|
---|
107 | vozac.setKTelefon(telefon);
|
---|
108 | vozac.setKIsAdmin(false);
|
---|
109 | vozac.setVPlata(salary);
|
---|
110 | vozac.setVDatumNaVrabotuvanje(Date.valueOf(LocalDate.now()));
|
---|
111 | this.korisnikRepository.save(vozac);
|
---|
112 |
|
---|
113 | return vozacRepository.save(vozac);
|
---|
114 | case KONDUKTER:
|
---|
115 | Kondukter kondukter = new Kondukter();
|
---|
116 | kondukter.setKIme(ime);
|
---|
117 | kondukter.setKAdresa(address);
|
---|
118 | kondukter.setKLozinka(passwordEncoder.encode(password));
|
---|
119 | kondukter.setKEmail(email);
|
---|
120 | kondukter.setKTelefon(telefon);
|
---|
121 | kondukter.setKIsAdmin(false);
|
---|
122 | kondukter.setVPlata(salary);
|
---|
123 | kondukter.setVDatumNaVrabotuvanje(Date.valueOf(LocalDate.now()));
|
---|
124 | this.korisnikRepository.save(kondukter);
|
---|
125 |
|
---|
126 | return kondukterRepository.save(kondukter);
|
---|
127 | }
|
---|
128 | return null;
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Override
|
---|
132 | public Korisnik register(String name, String email, String password, String confirmPassword, String address, String phone, Role role, Double salary) {
|
---|
133 | if (email == null || password == null || email.isEmpty() || password.isEmpty()) {
|
---|
134 | throw new InvalidCredentialsException();
|
---|
135 | }
|
---|
136 |
|
---|
137 | if(!password.equals(confirmPassword)){
|
---|
138 | throw new InvalidCredentialsException();
|
---|
139 | }
|
---|
140 |
|
---|
141 | if(role.equals(Role.ROLE_DRIVER)){
|
---|
142 | Vozac vozac = new Vozac();
|
---|
143 | vozac.setKIme(name);
|
---|
144 | vozac.setKAdresa(address);
|
---|
145 | vozac.setKLozinka(passwordEncoder.encode(password));
|
---|
146 | vozac.setKEmail(email);
|
---|
147 | vozac.setKTelefon(phone);
|
---|
148 | vozac.setKIsAdmin(role.equals(Role.ROLE_ADMIN));
|
---|
149 | vozac.setKRole(role);
|
---|
150 | vozac.setVPlata(salary != null ? salary : 0.0);
|
---|
151 | vozac.setVDatumNaVrabotuvanje(Date.valueOf(LocalDate.now()));
|
---|
152 | this.korisnikRepository.save(vozac);
|
---|
153 | return vozacRepository.save(vozac);
|
---|
154 | } else if(role.equals(Role.ROLE_CONDUCTOR)){
|
---|
155 | Kondukter kondukter = new Kondukter();
|
---|
156 | kondukter.setKIme(name);
|
---|
157 | kondukter.setKAdresa(address);
|
---|
158 | kondukter.setKLozinka(passwordEncoder.encode(password));
|
---|
159 | kondukter.setKEmail(email);
|
---|
160 | kondukter.setKTelefon(phone);
|
---|
161 | kondukter.setKIsAdmin(role.equals(Role.ROLE_ADMIN));
|
---|
162 | kondukter.setKRole(role);
|
---|
163 | kondukter.setVPlata(salary != null ? salary : 0.0);
|
---|
164 | kondukter.setVDatumNaVrabotuvanje(Date.valueOf(LocalDate.now()));
|
---|
165 | this.korisnikRepository.save(kondukter);
|
---|
166 | return kondukterRepository.save(kondukter);
|
---|
167 | } else {
|
---|
168 | Patnik patnik = new Patnik();
|
---|
169 | patnik.setKIme(name);
|
---|
170 | patnik.setKAdresa(address);
|
---|
171 | patnik.setKLozinka(passwordEncoder.encode(password));
|
---|
172 | patnik.setKEmail(email);
|
---|
173 | patnik.setKTelefon(phone);
|
---|
174 | patnik.setKIsAdmin(role.equals(Role.ROLE_ADMIN));
|
---|
175 | patnik.setKRole(role);
|
---|
176 | this.korisnikRepository.save(patnik);
|
---|
177 | return patnikRepository.save(patnik);
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | }
|
---|