[ce6ad22] | 1 | package parkup.services;
|
---|
| 2 |
|
---|
| 3 | import java.util.List;
|
---|
| 4 | import java.util.Optional;
|
---|
| 5 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
[bfc49f4] | 6 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
| 7 | import org.springframework.security.core.userdetails.UserDetailsService;
|
---|
| 8 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
---|
[3753356] | 9 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
[ce6ad22] | 10 | import org.springframework.stereotype.Service;
|
---|
| 11 | import parkup.entities.Guest;
|
---|
| 12 | import parkup.repositories.GuestRepository;
|
---|
| 13 |
|
---|
| 14 | @Service
|
---|
[bfc49f4] | 15 | public class GuestService implements UserDetailsService {
|
---|
[ce6ad22] | 16 | private final GuestRepository guestRepository;
|
---|
[3753356] | 17 | private final BCryptPasswordEncoder passwordEncoder;
|
---|
[ce6ad22] | 18 |
|
---|
| 19 | @Autowired
|
---|
[3753356] | 20 | public GuestService(GuestRepository guestRepository, BCryptPasswordEncoder passwordEncoder) {
|
---|
[ce6ad22] | 21 | this.guestRepository = guestRepository;
|
---|
[3753356] | 22 | this.passwordEncoder = passwordEncoder;
|
---|
[ce6ad22] | 23 | }
|
---|
| 24 |
|
---|
| 25 | public List<Guest> getGuests() {
|
---|
| 26 | return this.guestRepository.findAll();
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public Guest findById(int guestId) {
|
---|
| 30 | Optional<Guest> guest = Optional.ofNullable(this.guestRepository.findByGuestId(guestId));
|
---|
| 31 | return guest.orElse(null);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | public void addGuest(Guest guest) {
|
---|
| 35 | Optional<Guest> guestOpt = this.guestRepository.findGuestByEmail(guest.getEmail());
|
---|
| 36 | if (guestOpt.isPresent()) {
|
---|
| 37 | throw new IllegalStateException("Email already taken, try adding a guest with a different valid email address");
|
---|
| 38 | } else {
|
---|
[bfc49f4] | 39 | double random = Math.random()*100;
|
---|
| 40 | guest.setPassword(passwordEncoder.encode(Double.toString(random)));
|
---|
[ce6ad22] | 41 | this.guestRepository.save(guest);
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public void deleteGuest(int guestId) {
|
---|
| 46 | Optional<Guest> guest = Optional.ofNullable(this.guestRepository.findByGuestId(guestId));
|
---|
| 47 | if (guest.isPresent()) {
|
---|
| 48 | this.guestRepository.deleteByGuestId(guestId);
|
---|
| 49 | } else {
|
---|
| 50 | throw new IllegalStateException("Guest doesn't exist, therefore can't be deleted");
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
[bfc49f4] | 53 |
|
---|
| 54 | @Override
|
---|
| 55 | public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
|
---|
| 56 | return guestRepository.findGuestByEmail(s)
|
---|
| 57 | .orElseThrow(() ->
|
---|
| 58 | new UsernameNotFoundException("Email not found"));
|
---|
| 59 | }
|
---|
[ce6ad22] | 60 | }
|
---|