[b101b69] | 1 | package mk.ukim.finki.busngo.service.impl;
|
---|
| 2 |
|
---|
[e272096] | 3 | import mk.ukim.finki.busngo.model.entities.Kondukter;
|
---|
[b101b69] | 4 | import mk.ukim.finki.busngo.model.entities.Korisnik;
|
---|
[bde8b13] | 5 | import mk.ukim.finki.busngo.model.entities.Patnik;
|
---|
[e272096] | 6 | import mk.ukim.finki.busngo.model.entities.Vozac;
|
---|
| 7 | import mk.ukim.finki.busngo.model.enums.VrabotenType;
|
---|
[b101b69] | 8 | import mk.ukim.finki.busngo.model.exceptions.InvalidCredentialsException;
|
---|
| 9 | import mk.ukim.finki.busngo.model.exceptions.UserAlreadyExistsException;
|
---|
[e272096] | 10 | import mk.ukim.finki.busngo.repository.KondukterRepository;
|
---|
[b101b69] | 11 | import mk.ukim.finki.busngo.repository.KorisnikRepository;
|
---|
[bde8b13] | 12 | import mk.ukim.finki.busngo.repository.PatnikRepository;
|
---|
[e272096] | 13 | import mk.ukim.finki.busngo.repository.VozacRepository;
|
---|
| 14 | import mk.ukim.finki.busngo.service.*;
|
---|
[b101b69] | 15 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
| 16 | import org.springframework.stereotype.Service;
|
---|
| 17 |
|
---|
[e272096] | 18 | import java.sql.Date;
|
---|
| 19 | import java.time.LocalDate;
|
---|
| 20 |
|
---|
[b101b69] | 21 | @Service
|
---|
| 22 | public class AuthServiceImpl implements AuthService {
|
---|
| 23 | private final KorisnikRepository korisnikRepository;
|
---|
[bde8b13] | 24 | private final PatnikRepository patnikRepository;
|
---|
[e272096] | 25 | private final VozacRepository vozacRepository;
|
---|
| 26 | private final KondukterRepository kondukterRepository;
|
---|
[b101b69] | 27 | private final PasswordEncoder passwordEncoder;
|
---|
[e272096] | 28 | private final PatnikService patnikService;
|
---|
| 29 | private final KondukterService kondukterService;
|
---|
| 30 | private final VozacService vozacService;
|
---|
[b101b69] | 31 |
|
---|
[e272096] | 32 | public AuthServiceImpl(KorisnikRepository korisnikRepository, PatnikRepository patnikRepository, VozacRepository vozacRepository, KondukterRepository kondukterRepository, PasswordEncoder passwordEncoder, PatnikService patnikService, KondukterService kondukterService, VozacService vozacService) {
|
---|
[b101b69] | 33 | this.korisnikRepository = korisnikRepository;
|
---|
[bde8b13] | 34 | this.patnikRepository = patnikRepository;
|
---|
[e272096] | 35 | this.vozacRepository = vozacRepository;
|
---|
| 36 | this.kondukterRepository = kondukterRepository;
|
---|
[b101b69] | 37 | this.passwordEncoder = passwordEncoder;
|
---|
[e272096] | 38 | this.patnikService = patnikService;
|
---|
| 39 | this.kondukterService = kondukterService;
|
---|
| 40 | this.vozacService = vozacService;
|
---|
[b101b69] | 41 | }
|
---|
| 42 |
|
---|
| 43 | @Override
|
---|
| 44 | public Korisnik login(String email, String password) {
|
---|
| 45 | if(email == null || password == null || email.isEmpty() || password.isEmpty())
|
---|
| 46 | throw new InvalidCredentialsException();
|
---|
| 47 | return korisnikRepository.findByKEmailAndKLozinka(email, password).orElseThrow(InvalidCredentialsException::new);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | @Override
|
---|
[bde8b13] | 51 | public Korisnik registerPatnik(String ime, String email, String password, String confirmPassword, String address, String telefon) {
|
---|
[b101b69] | 52 | if (email == null || password == null || email.isEmpty() || password.isEmpty()) {
|
---|
| 53 | throw new InvalidCredentialsException();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | if (!password.equals(confirmPassword)) {
|
---|
| 57 | throw new InvalidCredentialsException();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | if(this.korisnikRepository.findByKEmail(email).isPresent()) {
|
---|
| 61 | throw new UserAlreadyExistsException(email);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[bde8b13] | 64 | Patnik korisnik = new Patnik();
|
---|
[b101b69] | 65 | korisnik.setKIme(ime);
|
---|
| 66 | korisnik.setKAdresa(address);
|
---|
| 67 | korisnik.setKLozinka(passwordEncoder.encode(password));
|
---|
| 68 | korisnik.setKEmail(email);
|
---|
| 69 | korisnik.setKTelefon(telefon);
|
---|
| 70 | korisnik.setKIsAdmin(false);
|
---|
| 71 |
|
---|
[bde8b13] | 72 | return patnikRepository.save(korisnik);
|
---|
[b101b69] | 73 | }
|
---|
| 74 |
|
---|
[e272096] | 75 | @Override
|
---|
| 76 | public Korisnik registerVraboten(String ime, String email, String password, String confirmPassword, String address, String telefon, VrabotenType type, Double salary) {
|
---|
| 77 | if (email == null || password == null || email.isEmpty() || password.isEmpty()) {
|
---|
| 78 | throw new InvalidCredentialsException();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | if (!password.equals(confirmPassword)) {
|
---|
| 82 | throw new InvalidCredentialsException();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | if(this.korisnikRepository.findByKEmail(email).isPresent()) {
|
---|
| 86 | throw new UserAlreadyExistsException(email);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | switch (type){
|
---|
| 91 | case ADMIN:
|
---|
| 92 | Korisnik korisnik = new Korisnik();
|
---|
| 93 | korisnik.setKIme(ime);
|
---|
| 94 | korisnik.setKAdresa(address);
|
---|
| 95 | korisnik.setKLozinka(passwordEncoder.encode(password));
|
---|
| 96 | korisnik.setKEmail(email);
|
---|
| 97 | korisnik.setKTelefon(telefon);
|
---|
| 98 | korisnik.setKIsAdmin(true);
|
---|
| 99 | return korisnikRepository.save(korisnik);
|
---|
| 100 | case VOZAC:
|
---|
| 101 | Vozac vozac = new Vozac();
|
---|
| 102 | vozac.setKIme(ime);
|
---|
| 103 | vozac.setKAdresa(address);
|
---|
| 104 | vozac.setKLozinka(passwordEncoder.encode(password));
|
---|
| 105 | vozac.setKEmail(email);
|
---|
| 106 | vozac.setKTelefon(telefon);
|
---|
| 107 | vozac.setKIsAdmin(false);
|
---|
| 108 | vozac.setVPlata(salary);
|
---|
| 109 | vozac.setVDatumNaVrabotuvanje(Date.valueOf(LocalDate.now()));
|
---|
| 110 | this.korisnikRepository.save(vozac);
|
---|
| 111 |
|
---|
| 112 | return vozacRepository.save(vozac);
|
---|
| 113 | case KONDUKTER:
|
---|
| 114 | Kondukter kondukter = new Kondukter();
|
---|
| 115 | kondukter.setKIme(ime);
|
---|
| 116 | kondukter.setKAdresa(address);
|
---|
| 117 | kondukter.setKLozinka(passwordEncoder.encode(password));
|
---|
| 118 | kondukter.setKEmail(email);
|
---|
| 119 | kondukter.setKTelefon(telefon);
|
---|
| 120 | kondukter.setKIsAdmin(false);
|
---|
| 121 | kondukter.setVPlata(salary);
|
---|
| 122 | kondukter.setVDatumNaVrabotuvanje(Date.valueOf(LocalDate.now()));
|
---|
| 123 | this.korisnikRepository.save(kondukter);
|
---|
| 124 |
|
---|
| 125 | return kondukterRepository.save(kondukter);
|
---|
| 126 | }
|
---|
| 127 | return null;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[b101b69] | 130 |
|
---|
| 131 | }
|
---|