source: src/main/java/com/example/eatys_app/service/KorisnikServiceImpl.java

Last change on this file was b3f2adb, checked in by Aleksandar Siljanoski <acewow3@…>, 14 months ago

Adding project to repo

  • Property mode set to 100644
File size: 2.7 KB
Line 
1package com.example.eatys_app.service;
2
3import com.example.eatys_app.model.Korisnik;
4import com.example.eatys_app.model.Kupuvac;
5import com.example.eatys_app.model.exceptions.InvalidUsernameException;
6import com.example.eatys_app.model.exceptions.InvalidUsernameOrPasswordException;
7import com.example.eatys_app.model.exceptions.PasswordsDoNotMatchException;
8import com.example.eatys_app.model.exceptions.UsernameAlreadyExistsException;
9import com.example.eatys_app.repository.KorisnikRepository;
10import com.example.eatys_app.repository.KupuvacRepository;
11import org.springframework.security.core.userdetails.UserDetails;
12import org.springframework.security.core.userdetails.UsernameNotFoundException;
13import org.springframework.security.crypto.password.PasswordEncoder;
14import org.springframework.stereotype.Service;
15
16@Service
17public 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}
Note: See TracBrowser for help on using the repository browser.