diff --git a/src/main/java/co/kr/jurumarble/config/WebConfig.java b/src/main/java/co/kr/jurumarble/config/WebConfig.java index 8dedb0a..61068f8 100644 --- a/src/main/java/co/kr/jurumarble/config/WebConfig.java +++ b/src/main/java/co/kr/jurumarble/config/WebConfig.java @@ -23,6 +23,7 @@ public void addInterceptors(InterceptorRegistry registry) { .addPathPatterns("/api/users/additional-info") .addPathPatterns("/api/users") .addPathPatterns("/api/votes/normal") + .addPathPatterns("/api/votes/v2") .addPathPatterns("/api/votes/drink") .addPathPatterns("/api/votes/participated") .addPathPatterns("/api/votes/my-vote") diff --git a/src/main/java/co/kr/jurumarble/interceptor/TokenInterceptor.java b/src/main/java/co/kr/jurumarble/interceptor/TokenInterceptor.java index 3e46420..edbe3cf 100644 --- a/src/main/java/co/kr/jurumarble/interceptor/TokenInterceptor.java +++ b/src/main/java/co/kr/jurumarble/interceptor/TokenInterceptor.java @@ -12,6 +12,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; +import java.util.List; @Slf4j @RequiredArgsConstructor @@ -20,14 +21,27 @@ public class TokenInterceptor implements HandlerInterceptor { private final JwtTokenProvider jwtTokenProvider; + private static final List PERMIT_JWT_URL_ARRAY = List.of("/api/votes/v2"); + + @Override // 컨트롤러 호출전에 호출 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { if (request.getMethod().equals(HttpMethod.OPTIONS.name())) { //preflight 통과하도록 설정 return true; } + String requestURI = request.getRequestURI(); + + if(PERMIT_JWT_URL_ARRAY.contains(requestURI) && request.getHeader(HttpHeaders.AUTHORIZATION) == null) { + request.setAttribute("userId", null); + log.info("%%%%%%%%%%%%%%%%%" + "if문 진입"); + log.info("%%%%%%%%%%%%%%%%%" + request.getAttribute("userId")); + return true; + } + String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION); parseTokenAndTransferUserId(request, authorizationHeader); + log.info("%%%%%%%%%%%%%%%%%" + request.getAttribute("userId")); return true; } @@ -35,6 +49,7 @@ private void parseTokenAndTransferUserId(HttpServletRequest request, String auth HashMap parseJwtTokenMap = jwtTokenProvider.parseJwtToken(request ,authorizationHeader); Long userId = getUserIdFromToken(parseJwtTokenMap); request.setAttribute("userId", userId); + log.info("%%%%%%%%%%%%%%%%%" + request.getAttribute("userId")); } private Long getUserIdFromToken(HashMap parseJwtTokenMap) { diff --git a/src/main/java/co/kr/jurumarble/token/domain/JwtTokenProvider.java b/src/main/java/co/kr/jurumarble/token/domain/JwtTokenProvider.java index a1aad5a..431b48e 100644 --- a/src/main/java/co/kr/jurumarble/token/domain/JwtTokenProvider.java +++ b/src/main/java/co/kr/jurumarble/token/domain/JwtTokenProvider.java @@ -32,7 +32,7 @@ public class JwtTokenProvider { @Value("${jwt.tokenPrefix}") private String tokenPrefix; - private final List notFilteredRoutes = List.of("/api/votes", "/api/drinks"); + private final List notFilteredRoutes = List.of("/api/votes/v2", "/api/drinks"); /** * JwtToken 생성 메서드 diff --git a/src/main/java/co/kr/jurumarble/vote/controller/VoteController.java b/src/main/java/co/kr/jurumarble/vote/controller/VoteController.java index 5129801..3455afb 100644 --- a/src/main/java/co/kr/jurumarble/vote/controller/VoteController.java +++ b/src/main/java/co/kr/jurumarble/vote/controller/VoteController.java @@ -54,6 +54,15 @@ public ResponseEntity> getVotes(@RequestParam(required = false) .body(voteService.sortFindVotes(keyword, sortBy, page, size)); } + @Operation(summary = "일반/전통주 투표 리스트 검색, 조회", description = "파라미터에 keyeword, sortBy, page, size, category 보내주시면 됩니다. 검색이 아니면 keyword = 에 값 없이 기획상 최신순, 인기순 만") + @GetMapping("/v2") + public ResponseEntity> getVotesV2(@RequestAttribute(required = false) Long userId, @RequestParam(required = false) String keyword, @RequestParam SortByType sortBy, @RequestParam int page, @RequestParam int size) { + log.info("******************" + String.valueOf(userId)); + return ResponseEntity.status(HttpStatus.OK) + .body(voteService.sortFindVotesV2(keyword, sortBy, page, size, userId)); + } + + @Operation(summary = "마이페이지- 내가 참여한 투표 리스트 조회", description = "파라미터에 keyeword, sortBy, page, size, category 보내주시면 됩니다. 검색이 아니면 keyword = 에 값 없이 ") @GetMapping("/participated") public ResponseEntity> getParticipatedVotes(@RequestAttribute Long userId,@RequestParam int page, @RequestParam int size) { diff --git a/src/main/java/co/kr/jurumarble/vote/repository/VoteEntityRepository.java b/src/main/java/co/kr/jurumarble/vote/repository/VoteEntityRepository.java index 90ae6b2..c04bfbe 100644 --- a/src/main/java/co/kr/jurumarble/vote/repository/VoteEntityRepository.java +++ b/src/main/java/co/kr/jurumarble/vote/repository/VoteEntityRepository.java @@ -12,6 +12,7 @@ import co.kr.jurumarble.vote.repository.dto.MyVotesCntData; import co.kr.jurumarble.vote.repository.dto.VoteCommonData; import co.kr.jurumarble.vote.repository.dto.VoteWithPostedUserCommonData; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Repository; @@ -53,4 +54,5 @@ public interface VoteEntityRepository { Long findMyParticipatedVoteCnt(Long userId); Long findMyWrittenVoteCnt(Long userId); Long findMyBookmarkedVoteCnt(Long userId); + List findVoteCommonDataByTimeAndUserId(Long userId, String keyword, PageRequest pageable); } diff --git a/src/main/java/co/kr/jurumarble/vote/repository/VoteEntityRepositoryImpl.java b/src/main/java/co/kr/jurumarble/vote/repository/VoteEntityRepositoryImpl.java index eb6553f..ab79716 100644 --- a/src/main/java/co/kr/jurumarble/vote/repository/VoteEntityRepositoryImpl.java +++ b/src/main/java/co/kr/jurumarble/vote/repository/VoteEntityRepositoryImpl.java @@ -13,12 +13,18 @@ import co.kr.jurumarble.vote.repository.dto.VoteCommonData; import co.kr.jurumarble.vote.repository.dto.VoteWithPostedUserCommonData; import com.querydsl.core.BooleanBuilder; +import com.querydsl.core.types.Expression; +import com.querydsl.core.types.Order; +import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.Projections; import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.core.types.dsl.CaseBuilder; +import com.querydsl.core.types.dsl.NumberExpression; +import com.querydsl.core.types.dsl.NumberPath; import com.querydsl.jpa.impl.JPAQueryFactory; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Repository; - import javax.persistence.EntityManager; import java.time.LocalDate; import java.time.LocalDateTime; @@ -33,7 +39,6 @@ import static co.kr.jurumarble.vote.domain.QVoteDrinkContent.voteDrinkContent; import static co.kr.jurumarble.vote.domain.QVoteResult.voteResult; - @Repository public class VoteEntityRepositoryImpl implements VoteEntityRepository { @@ -127,6 +132,45 @@ public List findVoteCommonDataByTime(String keyword, Pageable pa } + @Override + public List findVoteCommonDataByTimeAndUserId(Long userId, String keyword, PageRequest pageable) { + int pageNo = pageable.getPageNumber(); + int pageSize = pageable.getPageSize(); + + BooleanExpression keywordExpression = getKeywordExpression(keyword); + + NumberExpression voteOrder = new CaseBuilder() + .when(voteResult.votedUserId.eq(userId)).then(1) + .otherwise(0); + + List result = jpaQueryFactory + .select(Projections.bean(VoteCommonData.class, + vote.id.as("voteId"), + vote.postedUserId, + vote.title, + vote.detail, + vote.filteredGender, + vote.filteredAge, + vote.filteredMbti, + voteResult.count().as("voteCount"), + vote.voteType, + vote.createdDate.as("createdAt") + )) + .from(vote) + .leftJoin(voteResult).on(vote.id.eq(voteResult.voteId)) + .where(keywordExpression) + .groupBy(vote.id) + .orderBy( + voteOrder.asc(), + vote.createdDate.desc() + ) + .offset(pageNo * pageSize) + .limit(pageSize + 1) + .fetch(); + + return result; + } + @Override public List findDrinkVotesByTime(String keyword, String region, int pageNum, int pageSize) { diff --git a/src/main/java/co/kr/jurumarble/vote/service/VoteService.java b/src/main/java/co/kr/jurumarble/vote/service/VoteService.java index 22341e1..e006477 100644 --- a/src/main/java/co/kr/jurumarble/vote/service/VoteService.java +++ b/src/main/java/co/kr/jurumarble/vote/service/VoteService.java @@ -208,4 +208,30 @@ public MyVotesCntData getMyVotes(Long userId) { Long myBookmarkedVoteCnt = voteRepository.findMyBookmarkedVoteCnt(userId); return new MyVotesCntData(myWrittenVoteCnt, myParticipatedVoteCnt, myBookmarkedVoteCnt); } + + public Slice sortFindVotesV2(String keyword, SortByType sortBy, Integer page, Integer size, Long userId) { + + if (SortByType.ByTime == sortBy) { + PageRequest pageRequest = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, sortBy.getValue())); + return findVotesByTimeV2(userId, keyword, pageRequest); + } + + if (SortByType.ByPopularity == sortBy) { + PageRequest pageRequest = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, sortBy.getValue())); + return findVotesByPopularity(keyword, pageRequest); + } + + throw new SortByNotFountException(); + } + + public Slice findVotesByTimeV2(Long userId, String keyword, PageRequest pageable) { + List voteCommonDataByTime; + if(userId == null) { + voteCommonDataByTime = voteRepository.findVoteCommonDataByTime(keyword, pageable); + } else { + voteCommonDataByTime = voteRepository.findVoteCommonDataByTimeAndUserId(userId, keyword, pageable); + } + return voteFinder.getVoteData(pageable, voteCommonDataByTime); + } + } \ No newline at end of file