-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feature/#324 행사 검색 기능 구현 #686
The head ref may contain hidden characters: "Feature/#324-\uD589\uC0AC_\uAC80\uC0C9_\uAE30\uB2A5_\uAD6C\uD604"
Changes from all commits
b536159
cbcc8bb
6201a1b
1981b46
f763276
af9062f
b3d9944
2eb6d2d
ca21d99
ffe4b9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package com.emmsale.event.application; | ||
|
||
import static com.emmsale.event.domain.repository.EventSpecification.filterByCategory; | ||
import static com.emmsale.event.domain.repository.EventSpecification.filterByNameContainsSearchKeywords; | ||
import static com.emmsale.event.domain.repository.EventSpecification.filterByPeriod; | ||
import static com.emmsale.event.domain.repository.EventSpecification.filterByTags; | ||
import static com.emmsale.event.exception.EventExceptionType.NOT_FOUND_EVENT; | ||
import static com.emmsale.tag.exception.TagExceptionType.NOT_FOUND_TAG; | ||
|
@@ -15,7 +17,6 @@ | |
import com.emmsale.event.domain.EventStatus; | ||
import com.emmsale.event.domain.EventType; | ||
import com.emmsale.event.domain.repository.EventRepository; | ||
import com.emmsale.event.domain.repository.EventSpecification; | ||
import com.emmsale.event.domain.repository.EventTagRepository; | ||
import com.emmsale.event.exception.EventException; | ||
import com.emmsale.event.exception.EventExceptionType; | ||
|
@@ -92,20 +93,13 @@ private List<String> extractInformationImages(final List<String> imageUrls) { | |
@Transactional(readOnly = true) | ||
public List<EventResponse> findEvents(final EventType category, | ||
final LocalDate nowDate, final String startDate, final String endDate, | ||
final List<String> tagNames, final List<EventStatus> statuses) { | ||
final List<String> tagNames, final List<EventStatus> statuses, final String keyword) { | ||
Specification<Event> spec = Specification.where(filterByCategory(category)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specification을 잘 활용해주신 것 같아요. specification을 활용하는 코드가 많은데 별도의 클래스로 분리해보는 건 어떨까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 추후 EventService를 리팩토링할 때 고민해보면 좋을 것 같네요! |
||
|
||
if (isExistTagNames(tagNames)) { | ||
validateTags(tagNames); | ||
spec = spec.and(filterByTags(tagNames)); | ||
} | ||
spec = filterByTagIfExist(tagNames, spec); | ||
spec = filterByDateIfExist(startDate, endDate, spec); | ||
spec = filterByKeywordIfExist(keyword, spec); | ||
|
||
if (isExistFilterDate(startDate, endDate)) { | ||
final LocalDateTime startDateTime = validateStartDate(startDate); | ||
final LocalDateTime endDateTime = validateEndDate(endDate); | ||
validateEndDateAfterDateStart(startDateTime, endDateTime); | ||
spec = spec.and(EventSpecification.filterByPeriod(startDateTime, endDateTime)); | ||
} | ||
final List<Event> events = eventRepository.findAll(spec); | ||
|
||
final EnumMap<EventStatus, List<Event>> eventsForEventStatus | ||
|
@@ -114,6 +108,15 @@ public List<EventResponse> findEvents(final EventType category, | |
return filterByStatuses(statuses, eventsForEventStatus, makeImageUrlPerEventId(events)); | ||
} | ||
|
||
private Specification<Event> filterByTagIfExist(final List<String> tagNames, | ||
Specification<Event> spec) { | ||
if (isExistTagNames(tagNames)) { | ||
validateTags(tagNames); | ||
spec = spec.and(filterByTags(tagNames)); | ||
} | ||
return spec; | ||
} | ||
|
||
private boolean isExistTagNames(final List<String> tagNames) { | ||
return tagNames != null; | ||
} | ||
|
@@ -125,6 +128,17 @@ private void validateTags(final List<String> tagNames) { | |
} | ||
} | ||
|
||
private Specification<Event> filterByDateIfExist(final String startDate, final String endDate, | ||
Specification<Event> spec) { | ||
if (isExistFilterDate(startDate, endDate)) { | ||
final LocalDateTime startDateTime = validateStartDate(startDate); | ||
final LocalDateTime endDateTime = validateEndDate(endDate); | ||
validateEndDateAfterDateStart(startDateTime, endDateTime); | ||
spec = spec.and(filterByPeriod(startDateTime, endDateTime)); | ||
} | ||
return spec; | ||
} | ||
|
||
private boolean isExistFilterDate(final String startDate, final String endDate) { | ||
return startDate != null || endDate != null; | ||
} | ||
|
@@ -171,6 +185,19 @@ private Map<Long, String> makeImageUrlPerEventId(final List<Event> events) { | |
return imageUrlPerEventId; | ||
} | ||
|
||
private Specification<Event> filterByKeywordIfExist(final String keyword, | ||
Specification<Event> spec) { | ||
if (isExistKeyword(keyword)) { | ||
final String[] keywords = keyword.trim().split(" "); | ||
spec = spec.and(filterByNameContainsSearchKeywords(keywords)); | ||
} | ||
return spec; | ||
} | ||
|
||
private boolean isExistKeyword(final String keyword) { | ||
return keyword != null && !keyword.isBlank(); | ||
} | ||
|
||
private EnumMap<EventStatus, List<Event>> groupByEventStatus(final LocalDate nowDate, | ||
final List<Event> events) { | ||
return events.stream() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
나중에 필터링 관련 파라미터끼리 하나의 DTO로 묶어도 좋을 것 같네요.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
말씀을 듣고 고민해봤는데 DTO 객체로 매핑시켰을 때 required=false 설정을 어떻게 적용시킬 수 있을지 방법이 떠오르지 않아서, 추후 EventService 리팩토일 작업을 할 때 고민해봐야할 것 같아요.
좋은 의견 감사합니다~!