diff --git a/event-planner/api/demo/.gitignore b/event-planner/api/demo/.gitignore index c2065bc2..c9a9ee79 100644 --- a/event-planner/api/demo/.gitignore +++ b/event-planner/api/demo/.gitignore @@ -35,3 +35,17 @@ out/ ### VS Code ### .vscode/ + +### macOS ### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db + +### Event Planner Specific ### +.DS_Store +event-planner/.DS_Store +event-planner/api/.DS_Store diff --git a/event-planner/api/demo/src/main/java/com/api/demo/services/EventGuestService.java b/event-planner/api/demo/src/main/java/com/api/demo/services/EventGuestService.java index b3a1a612..5b6304ee 100644 --- a/event-planner/api/demo/src/main/java/com/api/demo/services/EventGuestService.java +++ b/event-planner/api/demo/src/main/java/com/api/demo/services/EventGuestService.java @@ -17,99 +17,83 @@ public class EventGuestService { private final EventGuestRepo eventGuestRepo; - private final UserService userService; - private final EventService eventService; - @Autowired - public EventGuestService( - EventGuestRepo eventGuestRepo, UserService userService, EventService eventService) { + public EventGuestService(EventGuestRepo eventGuestRepo) { this.eventGuestRepo = eventGuestRepo; - this.userService = userService; - this.eventService = eventService; } - // create a new event guest set the rsvp status by default - @Transactional - public EventModel createEventWithGuests(Long userId, EventModel event, Set emails) { - User organizer = userService.getUserById(userId); - Set usersFromEmails = userService.getAllUsersFromEmails(emails); - event.setIsPublic(false); - - Set eventGuests = new HashSet<>(); - - for (User user : usersFromEmails) { - EventGuest newGuest = - EventGuest.builder() - .eventGuestKey(new EventGuestKey(user.getId(), event.getId())) - .rsvpStatus(RsvpStatus.PENDING) - .event(event) - .guest(user) - .build(); - eventGuestRepo.save(newGuest); - eventGuests.add(newGuest); - } - - event.setOrganizer(organizer); - event.setEventGuests(eventGuests); - return eventService.createEvent(event); + /** + * Retrieves all guests for a specific event. + * + * @param eventId the ID of the event + * @return list of EventGuest entities for the specified event + */ + public List getGuestsByEventId(Long eventId) { + return StreamSupport.stream(eventGuestRepo.findAllByEventGuestKeyEventId(eventId).spliterator(), false) + .collect(Collectors.toList()); } - /* + /** + * Retrieves all events for a specific guest. * - * Get the Event by the Id - * Get the Organizer - * Create a new Event Guest using those Ids and save it to the database and update the event - * Will be a post mapping in controller + * @param guestId the ID of the guest + * @return list of EventGuest entities for the specified guest */ - public EventGuest addNewGuestToEvent(Long eventId, String guestEmail) { - // Get the event - EventModel event = eventService.getEventById(eventId); - - // Get the guest user by email - User guest = userService.getUserByEmail(guestEmail); - - // Create EventGuestKey - EventGuestKey key = new EventGuestKey(eventId, guest.getId()); - - // Create new EventGuest - EventGuest newGuest = - EventGuest.builder() - .eventGuestKey(key) - .rsvpStatus(RsvpStatus.PENDING) - .event(event) - .guest(guest) - .build(); - - return eventGuestRepo.save(newGuest); + public List getEventsByGuestId(Long guestId) { + return StreamSupport.stream(eventGuestRepo.findAllByEventGuestKeyGuestId(guestId).spliterator(), false) + .collect(Collectors.toList()); } - public Boolean removeGuestFromEvent(String email, Long eventId) { - // Get user by email - User user = userService.getUserByEmail(email); + /** + * Retrieves a specific EventGuest by composite key. + * + * @param eventId the event ID + * @param guestId the guest ID + * @return Optional containing the EventGuest if found + */ + public Optional getEventGuest(Long eventId, Long guestId) { + EventGuestKey key = new EventGuestKey(eventId, guestId); + return eventGuestRepo.findById(key); + } - // Create the composite key - EventGuestKey key = new EventGuestKey(eventId, user.getId()); + /** + * Saves or updates an EventGuest. + * + * @param eventGuest the EventGuest to save + * @return the saved EventGuest + * @throws IllegalArgumentException if eventGuest is null + */ + public EventGuest saveEventGuest(EventGuest eventGuest) { + if (eventGuest == null) { + throw new IllegalArgumentException("EventGuest cannot be null"); + } + return eventGuestRepo.save(eventGuest); + } - // Check if the event guest exists - if (eventGuestRepo.existsById(key)) { - eventGuestRepo.deleteById(key); - return true; + /** + * Deletes an EventGuest by composite key. + * + * @param eventId the event ID + * @param guestId the guest ID + * @throws IllegalArgumentException if eventId or guestId is null + */ + public void deleteEventGuest(Long eventId, Long guestId) { + if (eventId == null || guestId == null) { + throw new IllegalArgumentException("EventId and GuestId cannot be null"); } - return false; + EventGuestKey key = new EventGuestKey(eventId, guestId); + eventGuestRepo.deleteById(key); } - /** Update RSVP status for an event guest */ - @Transactional - public RsvpStatus setStatus(EventGuestKey guestKey, RsvpStatus status) { - return eventGuestRepo - .findById(guestKey) - .map( - eventGuest -> { - eventGuest.setRsvpStatus(status); - EventGuest saved = eventGuestRepo.save(eventGuest); - return saved.getRsvpStatus(); - }) - .orElseThrow( - () -> new EventGuestNotFoundException("EventGuest not found with key: " + guestKey)); + /** + * Checks if an EventGuest exists. + * + * @param eventId the event ID + * @param guestId the guest ID + * @return true if the EventGuest exists, false otherwise + */ + public boolean existsEventGuest(Long eventId, Long guestId) { + EventGuestKey key = new EventGuestKey(eventId, guestId); + return eventGuestRepo.existsById(key); } }