[ce6ad22] | 1 | package parkup.services;
|
---|
| 2 |
|
---|
| 3 | import java.util.List;
|
---|
| 4 | import java.util.Optional;
|
---|
| 5 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 6 | import org.springframework.stereotype.Service;
|
---|
| 7 | import parkup.entities.Guest;
|
---|
| 8 | import parkup.repositories.GuestRepository;
|
---|
| 9 |
|
---|
| 10 | @Service
|
---|
| 11 | public class GuestService {
|
---|
| 12 | private final GuestRepository guestRepository;
|
---|
| 13 |
|
---|
| 14 | @Autowired
|
---|
| 15 | public GuestService(GuestRepository guestRepository) {
|
---|
| 16 | this.guestRepository = guestRepository;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | public List<Guest> getGuests() {
|
---|
| 20 | return this.guestRepository.findAll();
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | public Guest findById(int guestId) {
|
---|
| 24 | Optional<Guest> guest = Optional.ofNullable(this.guestRepository.findByGuestId(guestId));
|
---|
| 25 | return guest.orElse(null);
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | public void addGuest(Guest guest) {
|
---|
| 29 | Optional<Guest> guestOpt = this.guestRepository.findGuestByEmail(guest.getEmail());
|
---|
| 30 | if (guestOpt.isPresent()) {
|
---|
| 31 | throw new IllegalStateException("Email already taken, try adding a guest with a different valid email address");
|
---|
| 32 | } else {
|
---|
| 33 | System.out.println(guest);
|
---|
| 34 | this.guestRepository.save(guest);
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public void deleteGuest(int guestId) {
|
---|
| 39 | Optional<Guest> guest = Optional.ofNullable(this.guestRepository.findByGuestId(guestId));
|
---|
| 40 | if (guest.isPresent()) {
|
---|
| 41 | this.guestRepository.deleteByGuestId(guestId);
|
---|
| 42 | } else {
|
---|
| 43 | throw new IllegalStateException("Guest doesn't exist, therefore can't be deleted");
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | }
|
---|