Skip to content

Commit

Permalink
[FEAT] 가장 최근 일정 보기 기능 추가 (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
kseysh authored Nov 11, 2024
2 parents d345bfc + 6b8c08b commit e34d45b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.util.List;
import org.sopt.app.presentation.calendar.CalendarResponse;
import org.sopt.app.presentation.calendar.RecentCalendarResponse;

public interface CalendarService {
List<CalendarResponse> getAllCurrentGenerationCalendar();
List<CalendarResponse> getAllCurrentGenerationCalendarResponse();
RecentCalendarResponse getRecentCalendarResponse();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.sopt.app.application.calendar;

import java.util.List;
import java.util.Optional;
import java.time.*;
import java.util.*;
import lombok.RequiredArgsConstructor;
import org.sopt.app.domain.cache.CachedAllCalendarResponse;
import org.sopt.app.domain.cache.Calendars;
import org.sopt.app.domain.cache.*;
import org.sopt.app.domain.entity.Calendar;
import org.sopt.app.interfaces.postgres.CalendarRepository;
import org.sopt.app.interfaces.postgres.redis.CachedCalendarRepository;
import org.sopt.app.presentation.calendar.CalendarResponse;
import org.sopt.app.presentation.calendar.RecentCalendarResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -24,22 +25,37 @@ public class CalendarServiceImpl implements CalendarService {

@Override
@Transactional
public List<CalendarResponse> getAllCurrentGenerationCalendar() {
public List<CalendarResponse> getAllCurrentGenerationCalendarResponse() {

Optional<CachedAllCalendarResponse> cachedCalendar = cachedCalendarRepository.findById(currentGeneration);

return cachedCalendar.orElseGet(this::cacheAllCalendarResponse)
.getCalendars().calendars().stream()
return this.getAllCurrentGenerationCalendar().stream()
.map(CalendarResponse::of)
.toList();
}

private CachedAllCalendarResponse cacheAllCalendarResponse() {
return cachedCalendarRepository.save(
new CachedAllCalendarResponse(
currentGeneration,
new Calendars(calendarRepository.findAllByGenerationOrderByStartDate(currentGeneration))
)
);
private List<Calendar> getAllCurrentGenerationCalendar() {
Optional<CachedAllCalendarResponse> cachedCalendar = cachedCalendarRepository.findById(currentGeneration);

if (cachedCalendar.isPresent()) {
return cachedCalendar.get().getCalendars().calendars();
}

return this.cacheAllCalendarResponse();
}

private List<Calendar> cacheAllCalendarResponse() {
List<Calendar> calendars = calendarRepository.findAllByGenerationOrderByStartDate(currentGeneration);
cachedCalendarRepository.save(new CachedAllCalendarResponse(currentGeneration, new Calendars(calendars)));
return calendars;
}

@Override
@Transactional
public RecentCalendarResponse getRecentCalendarResponse() {
LocalDate now = ZonedDateTime.now(ZoneId.of("Asia/Seoul")).toLocalDate();

return this.getAllCurrentGenerationCalendar().stream()
.filter(calendar -> !calendar.getStartDate().isBefore(now))
.findFirst().map(RecentCalendarResponse::of)
.orElseGet(() -> RecentCalendarResponse.createEmptyCalendar(now));
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/sopt/app/domain/enums/CalendarType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.sopt.app.domain.enums;

public enum CalendarType {
EVENT, SEMINAR
EVENT, SEMINAR, ETC
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,19 @@ public class CalendarController {
@GetMapping("/all")
public ResponseEntity<List<CalendarResponse>> getAllCalendar() {
return ResponseEntity.ok(
calendarService.getAllCurrentGenerationCalendar()
calendarService.getAllCurrentGenerationCalendarResponse()
);
}

@Operation(summary = "최근 일정 보기")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "success"),
@ApiResponse(responseCode = "500", description = "server error", content = @Content)
})
@GetMapping("/recent")
public ResponseEntity<RecentCalendarResponse> getRecentCalendar() {
return ResponseEntity.ok(
calendarService.getRecentCalendarResponse()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.sopt.app.presentation.calendar;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import lombok.Getter;
import org.sopt.app.domain.entity.Calendar;
import org.sopt.app.domain.enums.CalendarType;

@Getter
public class RecentCalendarResponse{
private final String date;
private final CalendarType type;
private final String title;

public static RecentCalendarResponse of(Calendar calendar){
return new RecentCalendarResponse(
calendar.getStartDate(),
calendar.getType(),
calendar.getTitle()
);
}

public static RecentCalendarResponse createEmptyCalendar(LocalDate date){
return new RecentCalendarResponse(
date,
CalendarType.ETC,
"일정이 없습니다."
);
}

private RecentCalendarResponse(final LocalDate date, final CalendarType type, final String title){
this.date = date.format(DateTimeFormatter.ofPattern("MM-dd"));
this.type = type;
this.title = title;
}
}

0 comments on commit e34d45b

Please sign in to comment.