Skip to content

Commit

Permalink
Merge pull request #130 from AndLetgo/develop
Browse files Browse the repository at this point in the history
[DEPLOY] : 전시회 랜덤 로직 수정 배포
  • Loading branch information
gyehwan24 authored May 9, 2024
2 parents 702ecf3 + 1209264 commit efa1506
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import depth.jeonsilog.domain.exhibition.converter.ExhibitionConverter;
import depth.jeonsilog.domain.exhibition.domain.Exhibition;
import depth.jeonsilog.domain.exhibition.domain.OperatingKeyword;
import depth.jeonsilog.domain.exhibition.domain.repository.ExhibitionRepository;
import depth.jeonsilog.domain.exhibition.dto.ExhibitionRequestDto;
import depth.jeonsilog.domain.exhibition.dto.ExhibitionResponseDto;
Expand Down Expand Up @@ -35,6 +36,8 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand Down Expand Up @@ -110,18 +113,15 @@ public ResponseEntity<?> findColorfulExhibitionList() {
count--;
}

if (!exhibitions.isEmpty()) {
// 랜덤 수 10개 추리기
Set<Integer> randomNum = new HashSet<>();
while(randomNum.size() < count){
randomNum.add((int)(Math.random() * exhibitions.size()));
if (exhibitions.size() > count) {
List<Integer> randomNum = pickRandomIndices(exhibitions, count);
for (Integer i : randomNum) {
exhibitionList.add(exhibitions.get(i));
}

Iterator<Integer> iter = randomNum.iterator();
while(iter.hasNext()){
int num = iter.next();
exhibitionList.add(exhibitions.get(num));
}
} else {
for (Exhibition exhibition : exhibitions)
exhibitionList.add(exhibition);
}

List<Place> places = new ArrayList<>();
Expand All @@ -143,33 +143,43 @@ public ResponseEntity<?> findColorfulExhibitionList() {
// Description : 전시회 목록 조회 - 곧 종료되는 전시
public ResponseEntity<?> findEndingSoonExhibitionList() {

List<Exhibition> exhibitions = exhibitionRepository.findExhibitionsWithinTwoWeeks();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
List<Exhibition> findExhibitions = exhibitionRepository.findByOperatingKeyword(OperatingKeyword.ON_DISPLAY);

LocalDate now = LocalDate.now();
LocalDate endDate;
List<Exhibition> exhibitionList = new ArrayList<>();
List<Exhibition> exhibitions = new ArrayList<>();

if (!exhibitions.isEmpty()) {
// 랜덤 수 10개 추리기
Set<Integer> randomNum = new HashSet<>();
while(randomNum.size() < 10){
randomNum.add((int)(Math.random() * exhibitions.size()));
}
for (Exhibition exhibition : findExhibitions) {
endDate = LocalDate.parse(exhibition.getStartDate(), formatter);
LocalDate date = endDate.minusDays(14);

Iterator<Integer> iter = randomNum.iterator();
while(iter.hasNext()){
int num = iter.next();
exhibitionList.add(exhibitions.get(num));
if (!now.isBefore(date))
exhibitionList.add(exhibition);
}

if (exhibitionList.size() > 10) {
List<Integer> randomNum = pickRandomIndices(exhibitionList, 10);
for (Integer i : randomNum) {
exhibitions.add(exhibitionList.get(i));
}

} else {
for (Exhibition exhibition : exhibitionList)
exhibitions.add(exhibition);
}

List<Place> places = new ArrayList<>();

for (Exhibition exhibition : exhibitionList) {
for (Exhibition exhibition : exhibitions) {
Place place = exhibition.getPlace();
places.add(place);
}

List<PlaceResponseDto.PlaceInfoRes> placeInfoResList = PlaceConverter.toPlaceInfoListRes(places);

List<ExhibitionResponseDto.ExhibitionRes> exhibitionResList = ExhibitionConverter.toExhibitionListRes(exhibitionList, placeInfoResList);
List<ExhibitionResponseDto.ExhibitionRes> exhibitionResList = ExhibitionConverter.toExhibitionListRes(exhibitions, placeInfoResList);

ApiResponse apiResponse = ApiResponse.toApiResponse(exhibitionResList);

Expand All @@ -179,33 +189,43 @@ public ResponseEntity<?> findEndingSoonExhibitionList() {
// Description : 전시회 목록 조회 - 새로 시작한 전시
public ResponseEntity<?> findNewExhibitionList() {

List<Exhibition> exhibitions = exhibitionRepository.findRecentExhibitions();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
List<Exhibition> findExhibitions = exhibitionRepository.findByOperatingKeyword(OperatingKeyword.ON_DISPLAY);

LocalDate now = LocalDate.now();
LocalDate startDate;
List<Exhibition> exhibitionList = new ArrayList<>();
List<Exhibition> exhibitions = new ArrayList<>();

if (!exhibitions.isEmpty()) {
// 랜덤 수 10개 추리기
Set<Integer> randomNum = new HashSet<>();
while(randomNum.size() < 10){
randomNum.add((int)(Math.random() * exhibitions.size()));
}
for (Exhibition exhibition : findExhibitions) {
startDate = LocalDate.parse(exhibition.getStartDate(), formatter);
LocalDate date = startDate.plusDays(7);

Iterator<Integer> iter = randomNum.iterator();
while(iter.hasNext()){
int num = iter.next();
exhibitionList.add(exhibitions.get(num));
if (!now.isAfter(date))
exhibitionList.add(exhibition);
}

if (exhibitionList.size() > 10) {
List<Integer> randomNum = pickRandomIndices(exhibitionList, 10);
for (Integer i : randomNum) {
exhibitions.add(exhibitionList.get(i));
}

} else {
for (Exhibition exhibition : exhibitionList)
exhibitions.add(exhibition);
}

List<Place> places = new ArrayList<>();

for (Exhibition exhibition : exhibitionList) {
for (Exhibition exhibition : exhibitions) {
Place place = exhibition.getPlace();
places.add(place);
}

List<PlaceResponseDto.PlaceInfoRes> placeInfoResList = PlaceConverter.toPlaceInfoListRes(places);

List<ExhibitionResponseDto.ExhibitionRes> exhibitionResList = ExhibitionConverter.toExhibitionListRes(exhibitionList, placeInfoResList);
List<ExhibitionResponseDto.ExhibitionRes> exhibitionResList = ExhibitionConverter.toExhibitionListRes(exhibitions, placeInfoResList);

ApiResponse apiResponse = ApiResponse.toApiResponse(exhibitionResList);

Expand Down Expand Up @@ -418,4 +438,19 @@ public Exhibition validateExhibitionById(Long exhibitionId) {
return exhibition.get();
}

// Description : 랜덤 뽑기
public List<Integer> pickRandomIndices(List<Exhibition> exhibitionList, int count) {
Random random = new Random();
int exhibitionSize = exhibitionList.size();

// 10개의 무작위 인덱스를 생성하고, 중복을 피하기 위해 distinct()를 사용
List<Integer> randomIndices = IntStream.generate(() -> random.nextInt(exhibitionSize))
.distinct()
.limit(count)
.boxed()
.collect(Collectors.toList());

return randomIndices;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ public interface ExhibitionRepository extends JpaRepository<Exhibition, Long> {
Optional<Exhibition> findByExhibitionSeq(Integer exhibitionSeq);

// 2주 이하
@Query(value = "SELECT * FROM exhibition e WHERE DATEDIFF(STR_TO_DATE(e.end_date, '%Y%m%d'), CURDATE()) < 14 AND CURDATE() <= STR_TO_DATE(e.end_date, '%Y%m%d')", nativeQuery = true)
List<Exhibition> findExhibitionsWithinTwoWeeks();

// 8일 미만
@Query(value = "SELECT * FROM exhibition e WHERE e.operating_keyword = 'ON_DISPLAY' AND DATEDIFF(STR_TO_DATE(e.start_date, '%Y%m%d'), CURDATE()) >= 0 AND DATEDIFF(STR_TO_DATE(e.start_date, '%Y%m%d'), CURDATE()) < 8", nativeQuery = true)
List<Exhibition> findRecentExhibitions();


@Query(value = "SELECT e FROM Exhibition e JOIN e.place p WHERE p.address LIKE %:keyword%")
List<Exhibition> findExhibitionsByAddressContainingKeyword(@Param("keyword") String keyword);
Expand Down

0 comments on commit efa1506

Please sign in to comment.