1 | package com.example.eatys_app.service;
|
---|
2 |
|
---|
3 | import com.example.eatys_app.model.Korisnik;
|
---|
4 | import com.example.eatys_app.model.Kupuvac;
|
---|
5 | import com.example.eatys_app.model.exceptions.InvalidUsernameException;
|
---|
6 | import com.example.eatys_app.model.exceptions.InvalidUsernameOrPasswordException;
|
---|
7 | import com.example.eatys_app.model.exceptions.PasswordsDoNotMatchException;
|
---|
8 | import com.example.eatys_app.model.exceptions.UsernameAlreadyExistsException;
|
---|
9 | import com.example.eatys_app.repository.KorisnikRepository;
|
---|
10 | import com.example.eatys_app.repository.KupuvacRepository;
|
---|
11 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
12 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
---|
13 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
14 | import org.springframework.stereotype.Service;
|
---|
15 |
|
---|
16 | @Service
|
---|
17 | public class KorisnikServiceImpl implements KorisnikService{
|
---|
18 |
|
---|
19 | private final KorisnikRepository korisnikRepository;
|
---|
20 | private final PasswordEncoder passwordEncoder;
|
---|
21 | private final KupuvacRepository kupuvacRepository;
|
---|
22 |
|
---|
23 | public KorisnikServiceImpl(KorisnikRepository korisnikRepository, PasswordEncoder passwordEncoder, KupuvacRepository kupuvacRepository) {
|
---|
24 | this.korisnikRepository = korisnikRepository;
|
---|
25 | this.passwordEncoder = passwordEncoder;
|
---|
26 | this.kupuvacRepository = kupuvacRepository;
|
---|
27 | }
|
---|
28 |
|
---|
29 |
|
---|
30 | @Override
|
---|
31 | public Korisnik register(String ime, String prezime, String password, String repeatPassword) {
|
---|
32 | if (ime==null || ime.isEmpty() || password==null || password.isEmpty())
|
---|
33 | throw new InvalidUsernameOrPasswordException();
|
---|
34 | if (!password.equals(repeatPassword))
|
---|
35 | throw new PasswordsDoNotMatchException();
|
---|
36 | if(this.korisnikRepository.findByIme(ime).isPresent())
|
---|
37 | throw new UsernameAlreadyExistsException(ime);
|
---|
38 | Korisnik user = new Korisnik(ime,prezime,passwordEncoder.encode(password));
|
---|
39 | Kupuvac kupuvac= new Kupuvac();
|
---|
40 | kupuvac.setIme(ime);
|
---|
41 | kupuvac.setPrezime(prezime);
|
---|
42 | kupuvac.setPassword(passwordEncoder.encode(password));
|
---|
43 | return kupuvacRepository.save(kupuvac);
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public Korisnik FindByName(String ime) {
|
---|
48 | Korisnik user= this.korisnikRepository.findByIme(ime).orElseThrow(InvalidUsernameException::new);
|
---|
49 | return user;
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public UserDetails loadUserByUsername(String ime) throws UsernameNotFoundException {
|
---|
54 |
|
---|
55 | Korisnik user= this.korisnikRepository.findByIme(ime).orElseThrow(InvalidUsernameException::new);
|
---|
56 | UserDetails userDetails= new org.springframework.security.core.userdetails.User(
|
---|
57 | user.getUsername(), user.getPassword(),user.getAuthorities());
|
---|
58 | return userDetails;
|
---|
59 | }
|
---|
60 |
|
---|
61 | }
|
---|