Skip to content

Commit

Permalink
Merge pull request #29 from Central-MakeUs/fix/recommendVote
Browse files Browse the repository at this point in the history
[fix]: 팝콘작 (추천) 랜덤으로 3개 반환하기, 무한 스크롤 구현
  • Loading branch information
AlmondBreez3 authored Jan 27, 2024
2 parents 48bf61e + bab91f1 commit dcc1897
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.example.api.recommendedPopcorn.dto.request.RecommendedPopcornRequest;
import com.example.api.recommendedPopcorn.dto.response.RecommendedPopcornResponse;
import com.example.api.recommendedPopcorn.service.GetRecommendedPopocornUseCase;
import com.example.api.recommendedPopcorn.service.GetRecommendedRandomPopcornUseCase;
import com.example.api.recommendedPopcorn.service.PostRecommendPopcornUseCase;
import com.example.api.recommendedPopcorn.service.PostVoteRecommendedPopcorn;
import com.example.domains.recommendedPopcorn.entity.RecommendedPopcorn;
Expand All @@ -26,21 +27,24 @@ public class RecommendedPopcornController {
private final PostRecommendPopcornUseCase recommendedPopcornUseCase;
private final GetRecommendedPopocornUseCase getRecommendedPopocornUseCase;
private final PostVoteRecommendedPopcorn postVoteRecommendedPopcorn;
private final GetRecommendedRandomPopcornUseCase getRecommendedRandomPopcornUseCase;
@PostMapping
public void postRecommendPopcorn(@RequestBody RecommendedPopcornRequest request) throws IOException {
recommendedPopcornUseCase.execute(request);
}
//

@GetMapping
public List<RecommendedPopcorn> getRandomThree() {
return getRecommendedRandomPopcornUseCase.execute();
}
@GetMapping("/all")
public List<RecommendedPopcorn> getRecommendPopcornList() {
return getRecommendedPopocornUseCase.execute();
}

//TODO 4. 예비 팝콘작 투표하기
@PostMapping("/vote")
public void voteByIdRecommendPopcorn(@RequestParam("recommendedPopcorn") Long recommendedPopcorn) {
postVoteRecommendedPopcorn.execute(recommendedPopcorn);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.api.recommendedPopcorn.service;

import com.example.adaptor.UseCase;
import com.example.domains.recommendedPopcorn.adaptor.RecommendedPopcornAdaptor;
import com.example.domains.recommendedPopcorn.entity.RecommendedPopcorn;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@UseCase
@RequiredArgsConstructor
public class GetRecommendedRandomPopcornUseCase {
private final RecommendedPopcornAdaptor recommendedPopcornAdaptor;


public List<RecommendedPopcorn> execute() {
List<RecommendedPopcorn> recommendedPopcornList = recommendedPopcornAdaptor.findByThreeIds();
return recommendedPopcornList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
public class SliceUtil {
public static <T> Slice<T> toSlice(List<T> contents, Pageable pageable) {
boolean hasNext = hasNext(contents, pageable);
System.out.println(hasNext);
return new SliceImpl<>(
hasNext ? getContent(contents, pageable) : contents, pageable, hasNext);
}

// 다음 페이지 있는지 확인
private static <T> boolean hasNext(List<T> content, Pageable pageable) {
System.out.println(content.size());
System.out.println(pageable.getPageSize());
System.out.println(pageable.isPaged());
return pageable.isPaged() && content.size() > pageable.getPageSize();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.domains.recommendedPopcorn.entity.QRecommendedPopcorn;
import com.example.domains.recommendedPopcorn.entity.RecommendedPopcorn;
import com.example.domains.recommendedPopcorn.exceptions.DuplicateMovieId;
import com.example.domains.recommendedPopcorn.exceptions.NoRecommendedPopcorn;
import com.example.domains.recommendedPopcorn.repository.RecommendedPopcornRepository;
import com.example.domains.screening.entity.QScreening;
import com.example.domains.screening.entity.Screening;
Expand All @@ -12,7 +13,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.*;

@Adaptor
@RequiredArgsConstructor
Expand Down Expand Up @@ -44,4 +45,42 @@ public void incrementVoteCount(Long recommendedPopcorn) {
.where(qRecommendedPopcorn.id.eq(recommendedPopcorn))
.execute();
}

public List<RecommendedPopcorn> findByThreeIds() {
List<RecommendedPopcorn> result = new ArrayList<>();
if(recommendedPopcornRepository.findAll().size()<=3) {
return recommendedPopcornRepository.findAll();
}
Set<Long> numberArray = generate();
for (Long number : numberArray) {
Optional<RecommendedPopcorn> foundItem = recommendedPopcornRepository.findById(number);

// findById에서 가져온 값이 존재할 경우에만 result에 추가
foundItem.ifPresent(result::add);
}
return result;
}
public Set<Long> generate() {
Set<Long> arr = new HashSet<>();
Random random = new Random();
while (arr.size()!=3) {
Long randomIndex = random.nextLong(1,recommendedPopcornRepository.findAll().size()+1);
arr.add(randomIndex);
}
return arr;
}

//TODO 로직 다시
private void validateNumber(Long randomIndex, List<Long> arr) {
if(arr.contains(randomIndex)) {
throw NoRecommendedPopcorn.EXCEPTION;
}
}


// private void validateRecommendedPopcorn() {
// if(recommendedPopcornRepository.findAll().size() == 0) {
// throw NoRecommendedPopcorn.EXCEPTION;
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ public class RecommendedPopcorn extends BaseTimeEntity {
private String movieDirector;

@Builder
private RecommendedPopcorn(String movieId, String movieTitle, String imageUrl, String movieDetail, String movieDirector, String recommendationReason) {
private RecommendedPopcorn(String movieId, String movieTitle, String imageUrl, String movieDetail, String movieDirector, int recommendationCount,String recommendationReason) {
this.movieId = movieId;
this.movieTitle = movieTitle;
this.imageUrl = imageUrl;
this.movieDetail = movieDetail;
this.movieDirector = movieDirector;
this.recommendationCount = recommendationCount;
this.recommendationReason=recommendationReason;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.domains.recommendedPopcorn.exceptions;

import com.example.error.BaseErrorException;

public class NoRecommendedPopcorn extends BaseErrorException {
public static final BaseErrorException EXCEPTION = new NoRecommendedPopcorn();

private NoRecommendedPopcorn() {
super(RecommendedErrorCode.NO_RECOMMENDED_POPCORN);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@AllArgsConstructor
public enum RecommendedErrorCode implements BaseErrorCode {
No_MOVIE_FOUND(404,"MOVIE_404", "영화가 존재하지 않습니다"),
DUPLICATE_MOVIE(409,"MOVIE_409", "중복된 영화가 존재합니다");
DUPLICATE_MOVIE(409,"MOVIE_409", "중복된 영화가 존재합니다"), NO_RECOMMENDED_POPCORN(404,"MOVIE_404" ,"추천작이 존재하지 않습니다" );

private int status;
private String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ public class RecommendedPopcornUser {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private boolean isAgreed;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;
@ManyToOne
@JoinColumn(name = "recommended_popcorn_id")
private RecommendedPopcorn recommendedPopcorn;

@Builder
private RecommendedPopcornUser(boolean isAgreed, User user, RecommendedPopcorn recommendedPopcorn) {
this.isAgreed = isAgreed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public SliceResponse<Screening> querySliceScreening(String title, Category categ
)
.orderBy(QScreening.screening.createdAt.asc()) // Adjust the sorting as needed
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.limit(pageable.getPageSize()+1)
.fetch();

return SliceResponse.of(SliceUtil.toSlice(query, pageable));
Expand All @@ -41,7 +41,7 @@ public SliceResponse<Screening> querySliceScreeningByDate(String title, Category
)
.orderBy(QScreening.screening.screeningStartDate.asc()) // Adjust the sorting as needed
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.limit(pageable.getPageSize()+1)
.fetch();

return SliceResponse.of(SliceUtil.toSlice(query, pageable));
Expand Down

0 comments on commit dcc1897

Please sign in to comment.