Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: 기념일 날짜 반환 수정 #43

Merged
merged 9 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public Anniversary addAnniversary(Long memberId, AnniversaryRequestDTO requestDT
throw new IllegalArgumentException("Invalid anniversary type");
}

LocalDate requestDate = requestDTO.getDate();
LocalDate currentDate = LocalDate.now(ZoneId.of("UTC"));
if (requestDate.isAfter(currentDate)) {
throw new IllegalArgumentException("Date cannot be in the future");
}

ZonedDateTime zonedDateTime = requestDTO.getDate().atStartOfDay(ZoneId.of("UTC"));
Timestamp timestamp = Timestamp.from(zonedDateTime.toInstant());

Expand All @@ -63,6 +69,12 @@ public Anniversary updateAnniversary(Long memberId, Long anniversaryId, Annivers

validateAnniversaryType(requestDTO.getType());

LocalDate requestDate = requestDTO.getDate();
LocalDate currentDate = LocalDate.now(ZoneId.of("UTC"));
if (requestDate.isAfter(currentDate)) {
throw new IllegalArgumentException("Date cannot be in the future");
}

ZonedDateTime zonedDateTime = requestDTO.getDate().atStartOfDay(ZoneId.of("UTC"));
Timestamp timestamp = Timestamp.from(zonedDateTime.toInstant());

Expand Down Expand Up @@ -96,50 +108,51 @@ public List<Map<String, Integer>> calculateDDay(Anniversary anniversary) {
LocalDate today = LocalDate.now();
LocalDateTime anniversaryDateTime = anniversary.getAnniversaryDate().toLocalDateTime();
LocalDate anniversaryDate = anniversaryDateTime.toLocalDate();
long yearsDifference = ChronoUnit.YEARS.between(anniversaryDate, today);

Map<String, Integer> closestDDay = null;
int minDays = Integer.MAX_VALUE;

if (anniversary.getType() == AnniversaryType.연인) {
int daysPassed = (int) ChronoUnit.DAYS.between(anniversaryDate, today);
int nextDay = ((daysPassed / 100) + 1) * 100;

for (int i = 0; i < 2; i++) {
for (int i = 0; i < 1; i++) { // Only find the next closest 100-day anniversary
LocalDate hundredDays = anniversaryDate.plusDays(nextDay + i * 100);
if (!hundredDays.isBefore(today)) {
Map<String, Integer> dDay = new HashMap<>();
dDay.put((nextDay + i * 100) + "days", (int) ChronoUnit.DAYS.between(today, hundredDays) - 1);
dDayList.add(dDay);
int daysToHundredDays = (int) ChronoUnit.DAYS.between(today, hundredDays) - 1;
if (daysToHundredDays < minDays) {
minDays = daysToHundredDays;
closestDDay = new HashMap<>();
closestDDay.put((nextDay + i * 100) + "days", daysToHundredDays);
}
break;
}
}
}

boolean isTodayAnniversary = false;
long yearsDifference = ChronoUnit.YEARS.between(anniversaryDate, today);
for (int i = 1; i <= yearsDifference + 1; i++) {
LocalDate yearAnniversary = anniversaryDate.plusYears(i);
int daysBetween = (int) ChronoUnit.DAYS.between(today, yearAnniversary);
if (daysBetween == 0) {
Map<String, Integer> dDay = new HashMap<>();
dDay.put("year", daysBetween);
dDayList.add(dDay);
isTodayAnniversary = true;
if (!yearAnniversary.isBefore(today)) {
int daysToYearAnniversary = (int) ChronoUnit.DAYS.between(today, yearAnniversary);
if (daysToYearAnniversary < minDays) {
minDays = daysToYearAnniversary;
closestDDay = new HashMap<>();
closestDDay.put("year", daysToYearAnniversary);
}
break;
}
}

if (!isTodayAnniversary) {
for (int i = 1; i <= yearsDifference + 1; i++) {
LocalDate yearAnniversary = anniversaryDate.plusYears(i);
if (!yearAnniversary.isBefore(today)) {
Map<String, Integer> dDay = new HashMap<>();
dDay.put("year", (int) ChronoUnit.DAYS.between(today, yearAnniversary));
dDayList.add(dDay);
break;
}
}
if (closestDDay != null) {
dDayList.add(closestDDay);
}


return dDayList;
}


public List<AnniversaryResponseDTO> getDDayZeroAnniversaries(List<Anniversary> anniversaries) {
List<AnniversaryResponseDTO> dDayZeroList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ public static MemberResponseDTO of(Member member) {
.profileImage(member.getProfileImage())
.alarm(member.isAlarm())
.anniversaries(member.getAnniversaries().stream()
.map(anniversary -> Map.<String, Object>of(
"id", anniversary.getId(),
"name", anniversary.getName(),
"anniversaryDate", anniversary.getAnniversaryDate().toString().substring(0,10),
"type", anniversary.getType().name(),
"dDays", anniversaryService.calculateDDay(anniversary)
))
.map(anniversary -> {
List<Map<String, Integer>> dDays = anniversaryService.calculateDDay(anniversary);
return Map.of(
"id", anniversary.getId(),
"name", anniversary.getName(),
"anniversaryDate", anniversary.getAnniversaryDate().toString().substring(0, 10),
"type", anniversary.getType().name(),
"dDays", dDays
);
})
.sorted((a1, a2) -> {
Integer dDay1 = (Integer) ((Map<String, Integer>) ((List<?>) a1.get("dDays")).get(0)).values().iterator().next();
Integer dDay2 = (Integer) ((Map<String, Integer>) ((List<?>) a2.get("dDays")).get(0)).values().iterator().next();
return dDay1.compareTo(dDay2);
})
.collect(Collectors.toList()))
.build();
}
Expand Down
Loading