Skip to content

Commit

Permalink
Merge pull request #185 from UnivApp/feature/notification-refactor23
Browse files Browse the repository at this point in the history
refactor: 알림 설정 반환 리팩토링
  • Loading branch information
nyeroni authored Nov 14, 2024
2 parents 2d3cbc1 + dbc844e commit 369aadf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
package yerong.wedle.calendar.service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yerong.wedle.calendar.domain.CalendarEvent;
import yerong.wedle.calendar.dto.CalendarEventResponse;
import yerong.wedle.calendar.exception.CalendarEventNotFoundException;
import yerong.wedle.calendar.repository.CalendarEventRepository;
import yerong.wedle.member.domain.Member;
import yerong.wedle.member.exception.MemberNotFoundException;
import yerong.wedle.member.repository.MemberRepository;
import yerong.wedle.notification.domain.Notification;
import yerong.wedle.notification.exception.NotificationNotFoundException;
import yerong.wedle.notification.repository.NotificationRepository;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -31,45 +27,59 @@ public class CalendarEventService {

public List<CalendarEventResponse> getAll() {
List<CalendarEvent> calendarEvents = calendarEventRepository.findAll();

return calendarEvents.stream()
.flatMap(event -> convertToDto(event).stream())
.collect(Collectors.toList());
return convertToDto(calendarEvents);
}

public List<CalendarEventResponse> convertToDto(CalendarEvent calendarEvent) {
LocalDate startDate = calendarEvent.getStartDate();
LocalDate endDate = calendarEvent.getEndDate();

public List<CalendarEventResponse> convertToDto(List<CalendarEvent> calendarEvents) {
List<CalendarEventResponse> calendarEventResponses = new ArrayList<>();
String socialId = getCurrentUserId();
Member member = memberRepository.findBySocialId(socialId)
.orElseThrow(MemberNotFoundException::new);
for (CalendarEvent calendarEvent : calendarEvents) {
LocalDate startDate = calendarEvent.getStartDate();
LocalDate endDate = calendarEvent.getEndDate();

Notification notification = notificationRepository.findByEventAndMember(calendarEvent, member).orElse(null);
Long notificationId = notification != null ? notification.getNotificationId() : null;

if (endDate == null) {
return List.of(new CalendarEventResponse(
calendarEvent.getId(),
calendarEvent.getTitle(),
startDate,
calendarEvent.getCalendarEventType().getDisplayName(),
notification != null && notification.isActive() && notification.getNotificationDate().equals(startDate),
notificationId
));
}

return startDate.datesUntil(endDate.plusDays(1))
.map(date -> new CalendarEventResponse(
if (endDate == null) {
Notification notification = notificationRepository.findByMemberAndEventAndNotificationDate(member,
calendarEvent, startDate).orElse(null);
Long notificationId = notification != null ? notification.getNotificationId() : null;
calendarEventResponses.add(new CalendarEventResponse(
calendarEvent.getId(),
calendarEvent.getTitle(),
date,
startDate,
calendarEvent.getCalendarEventType().getDisplayName(),
notification != null && notification.isActive() && notification.getNotificationDate().equals(date),
notification != null && notification.isActive() && notification.getNotificationDate()
.equals(startDate),
notificationId
))
.collect(Collectors.toList());
));
} else {
System.out.println("==============");
System.out.println("success");
System.out.println("==============");

startDate.datesUntil(endDate.plusDays(1))
.forEach(date -> {
Notification notification = notificationRepository.findByMemberAndEventAndNotificationDate(
member, calendarEvent, date).orElse(null);
Long notificationId = notification != null ? notification.getNotificationId() : null;
System.out.println("notificationId: " + notificationId);
boolean isActive = notification != null && notification.isActive()
&& notification.getNotificationDate().equals(date);

calendarEventResponses.add(new CalendarEventResponse(
calendarEvent.getId(),
calendarEvent.getTitle(),
date,
calendarEvent.getCalendarEventType().getDisplayName(),
isActive,
notificationId
));
});
}
}
return calendarEventResponses;
}

private String getCurrentUserId() {
String socialId = SecurityContextHolder.getContext().getAuthentication().getName();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package yerong.wedle.notification.repository;

import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

import jakarta.validation.constraints.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import yerong.wedle.calendar.domain.CalendarEvent;
import yerong.wedle.member.domain.Member;
Expand All @@ -14,10 +13,12 @@ public interface NotificationRepository extends JpaRepository<Notification, Long

List<Notification> findByNotificationDate(LocalDate notification);

Optional<Notification> findByEventAndMember(CalendarEvent calendarEvent, Member member);
List<Notification> findByEventAndMember(CalendarEvent calendarEvent, Member member);

List<Notification> findByMember(Member member);

List<Notification> findByMemberAndIsActiveTrue(Member member);
Optional<Notification> findByMemberAndEventAndNotificationDate(Member member, CalendarEvent calendarEvent, @NotNull LocalDate notificationDate);

Optional<Notification> findByMemberAndEventAndNotificationDate(Member member, CalendarEvent calendarEvent,
@NotNull LocalDate notificationDate);
}

0 comments on commit 369aadf

Please sign in to comment.