1 | package project.fmo.app.projcetfmo.Service.impl;
|
---|
2 |
|
---|
3 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
---|
4 | import org.springframework.security.core.userdetails.User;
|
---|
5 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
6 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
---|
7 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
8 | import org.springframework.stereotype.Service;
|
---|
9 | import project.fmo.app.projcetfmo.Model.*;
|
---|
10 | import project.fmo.app.projcetfmo.Repository.*;
|
---|
11 | import project.fmo.app.projcetfmo.Service.KorisnikService;
|
---|
12 |
|
---|
13 | import java.sql.Timestamp;
|
---|
14 | import java.time.LocalDateTime;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Objects;
|
---|
17 | import java.util.stream.Collectors;
|
---|
18 | import java.util.stream.Stream;
|
---|
19 |
|
---|
20 | @Service
|
---|
21 | public class KorisnikServiceImpl implements KorisnikService {
|
---|
22 |
|
---|
23 | private final PasswordEncoder passwordEncoder;
|
---|
24 | private final AdminRepository adminRepository;
|
---|
25 | private final DostavuvacRepository dostavuvacRepository;
|
---|
26 |
|
---|
27 | private final TelefnoskiBrojRepository telefnoskiBrojRepository;
|
---|
28 | private final KupuvacRepository kupuvacRepository;
|
---|
29 | private final KorisnikRepository korisnikRepository;
|
---|
30 |
|
---|
31 | public KorisnikServiceImpl(PasswordEncoder passwordEncoder, AdminRepository adminRepository, DostavuvacRepository dostavuvacRepository, TelefnoskiBrojRepository telefnoskiBrojRepository, KupuvacRepository kupuvacRepository, KorisnikRepository korisnikRepository) {
|
---|
32 | this.passwordEncoder = passwordEncoder;
|
---|
33 | this.adminRepository = adminRepository;
|
---|
34 | this.dostavuvacRepository = dostavuvacRepository;
|
---|
35 | this.telefnoskiBrojRepository = telefnoskiBrojRepository;
|
---|
36 | this.kupuvacRepository = kupuvacRepository;
|
---|
37 | this.korisnikRepository = korisnikRepository;
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
---|
42 | Korisnik korisnik = korisnikRepository.findByImePrezime(username);
|
---|
43 | for (AdminKorisnik a: adminRepository.findAll()) {
|
---|
44 | if (a.getIdKorisnik() == korisnik.getIdKorisnik()) {
|
---|
45 | return new User(korisnik.getImePrezime(), korisnik.getPasswordKorisnik(), Stream.of(new SimpleGrantedAuthority("ROLE_ADMIN")).collect(Collectors.toList()));
|
---|
46 | }
|
---|
47 | }
|
---|
48 | for (Dostavuvac a: dostavuvacRepository.findAll()) {
|
---|
49 | if (a.getIdKorisnik() == korisnik.getIdKorisnik()) {
|
---|
50 | return new User(korisnik.getImePrezime(), korisnik.getPasswordKorisnik(), Stream.of(new SimpleGrantedAuthority("ROLE_DELIVER")).collect(Collectors.toList()));
|
---|
51 | }
|
---|
52 | }
|
---|
53 | for (Kupuvac a: kupuvacRepository.findAll()){
|
---|
54 | if (a.getIdKorisnik() == korisnik.getIdKorisnik()){
|
---|
55 | return new User(korisnik.getImePrezime(),korisnik.getPasswordKorisnik(), Stream.of(new SimpleGrantedAuthority("ROLE_USER")).collect(Collectors.toList()) );
|
---|
56 | }
|
---|
57 | }
|
---|
58 | return null;
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public Korisnik findbyImePrezime(String ime) {
|
---|
63 | return korisnikRepository.findByImePrezime(ime);
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public Korisnik findById(Integer id) {
|
---|
68 | return korisnikRepository.findById(id).orElse(null);
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public Korisnik register(String username, String password, String repeatPassword, String mail, String number) {
|
---|
73 | if (username==null || username.isEmpty() || password==null || password.isEmpty())
|
---|
74 | throw new RuntimeException();
|
---|
75 | if (!password.equals(repeatPassword))
|
---|
76 | throw new RuntimeException();
|
---|
77 |
|
---|
78 | Korisnik korisnik = new Korisnik(username,password, Timestamp.valueOf(LocalDateTime.now()));
|
---|
79 | korisnikRepository.save(korisnik);
|
---|
80 | Kupuvac k = new Kupuvac(korisnik.getIdKorisnik(), mail);
|
---|
81 | kupuvacRepository.save(k);
|
---|
82 | Telefonskibroj t = new Telefonskibroj(new TelefonskibrojPK(korisnik.getIdKorisnik(),number));
|
---|
83 | telefnoskiBrojRepository.save(t);
|
---|
84 | return korisnik;
|
---|
85 |
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public List<Object> statistics2() {
|
---|
90 | return korisnikRepository.statistics2();
|
---|
91 | }
|
---|
92 | }
|
---|