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

feat: 입시 달력 구현 #140

Merged
merged 1 commit into from
Oct 17, 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 @@ -27,42 +27,14 @@ public class CalendarEventApiController {

private final CalendarEventService calendarEventService;

@Operation(summary = "이벤트 ID로 조회", description = "특정 이벤트의 ID를 사용하여 해당 이벤트의 상세 정보를 조회합니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "이벤트 조회 성공"),
@ApiResponse(responseCode = "404", description = "해당 ID의 이벤트를 찾을 수 없음")
})
@GetMapping("/{eventId}")
public ResponseEntity<CalendarEventResponse> getEventById(@PathVariable Long eventId) {
try {
CalendarEventResponse response = calendarEventService.getEventById(eventId);
return ResponseEntity.ok(response);
} catch (CalendarEventNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}

@Operation(summary = "모든 이벤트 조회", description = "모든 달력 이벤트를 조회합니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "모든 이벤트 조회 성공")
})
@GetMapping
public ResponseEntity<List<CalendarEventResponse>> getAllEvents() {
List<CalendarEventResponse> responses = calendarEventService.getAllEvents();
List<CalendarEventResponse> responses = calendarEventService.getAll();
return ResponseEntity.ok(responses);
}

@Operation(summary = "날짜로 이벤트 조회", description = "특정 날짜에 해당하는 이벤트를 조회합니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "날짜로 이벤트 조회 성공"),
@ApiResponse(responseCode = "404", description = "해당 날짜에 이벤트가 없음")
})
@GetMapping("/by-date")
public ResponseEntity<List<CalendarEventResponse>> getEventsByDate(@RequestParam LocalDate date) {
List<CalendarEventResponse> responses = calendarEventService.getEventsByDate(date);
if (responses.isEmpty()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
return ResponseEntity.ok(responses);
}
}
10 changes: 7 additions & 3 deletions src/main/java/yerong/wedle/calendar/domain/CalendarEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ public class CalendarEvent extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String title;

@Column(nullable = false)
private LocalDate date;
private LocalDate startDate;
private LocalDate endDate;

@Column(nullable = false, length = 255)
private String content;
@Enumerated(EnumType.STRING)
@Column(name = "calendar_event_type", nullable = false)
private CalendarEventType calendarEventType;
}
19 changes: 19 additions & 0 deletions src/main/java/yerong/wedle/calendar/domain/CalendarEventType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package yerong.wedle.calendar.domain;

public enum CalendarEventType {

COLLEGE_ENTRANCE_EXAM("수능"),
UNIVERSITY_COOPERATION_EVENT("대학 연계 행사");



private final String displayName;

CalendarEventType(String displayName) {
this.displayName = displayName;
}

public String getDisplayName() {
return displayName;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package yerong.wedle.calendar.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDate;

@Getter
@Setter
@AllArgsConstructor
public class CalendarEventResponse {

private Long id;
private String title;
private LocalDate date;
private String content;
private String type;

public CalendarEventResponse(Long id, LocalDate date, String content) {
this.id = id;
this.date = date;
this.content = content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
import java.util.List;

public interface CalendarEventRepository extends JpaRepository<CalendarEvent, Long> {
List<CalendarEvent> findByDate(LocalDate date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,35 @@ public class CalendarEventService {

private final CalendarEventRepository calendarEventRepository;

public CalendarEventResponse getEventById(Long eventId) {
CalendarEvent event = calendarEventRepository.findById(eventId)
.orElseThrow(CalendarEventNotFoundException::new);
return convertToDto(event);
}
public List<CalendarEventResponse> getAll() {
List<CalendarEvent> calendarEvents = calendarEventRepository.findAll();

public List<CalendarEventResponse> getAllEvents() {
return calendarEventRepository.findAll().stream()
.map(this::convertToDto)
return calendarEvents.stream()
.flatMap(event -> convertToDto(event).stream()) // 각 이벤트를 날짜별로 처리
.collect(Collectors.toList());
}

public List<CalendarEventResponse> getEventsByDate(LocalDate date) {
return calendarEventRepository.findByDate(date).stream()
.map(this::convertToDto)
public List<CalendarEventResponse> convertToDto(CalendarEvent calendarEvent) {
LocalDate startDate = calendarEvent.getStartDate();
LocalDate endDate = calendarEvent.getEndDate();

if (endDate == null) {
return List.of(new CalendarEventResponse(
calendarEvent.getId(),
calendarEvent.getTitle(),
startDate,
calendarEvent.getCalendarEventType().getDisplayName()
));
}

return startDate.datesUntil(endDate.plusDays(1))
.map(date -> new CalendarEventResponse(
calendarEvent.getId(),
calendarEvent.getTitle(),
date,
calendarEvent.getCalendarEventType().getDisplayName()
))
.collect(Collectors.toList());
}

private CalendarEventResponse convertToDto(CalendarEvent event) {
return new CalendarEventResponse(
event.getId(),
event.getDate(),
event.getContent()
);
}
}
Loading