Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
wellbeing-dough committed Oct 18, 2023
2 parents 66e26ca + b88fe4e commit 9b980cc
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/main/java/co/kr/jurumarble/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/co/kr/jurumarble/interceptor/TokenInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
Expand All @@ -20,21 +21,35 @@ public class TokenInterceptor implements HandlerInterceptor {

private final JwtTokenProvider jwtTokenProvider;

private static final List<String> 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;
}

private void parseTokenAndTransferUserId(HttpServletRequest request, String authorizationHeader) {
HashMap<String, Object> parseJwtTokenMap = jwtTokenProvider.parseJwtToken(request ,authorizationHeader);
Long userId = getUserIdFromToken(parseJwtTokenMap);
request.setAttribute("userId", userId);
log.info("%%%%%%%%%%%%%%%%%" + request.getAttribute("userId"));
}

private Long getUserIdFromToken(HashMap<String, Object> parseJwtTokenMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class JwtTokenProvider {
@Value("${jwt.tokenPrefix}")
private String tokenPrefix;

private final List<String> notFilteredRoutes = List.of("/api/votes", "/api/drinks");
private final List<String> notFilteredRoutes = List.of("/api/votes/v2", "/api/drinks");

/**
* JwtToken 생성 메서드
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public ResponseEntity<Slice<VoteData>> getVotes(@RequestParam(required = false)
.body(voteService.sortFindVotes(keyword, sortBy, page, size));
}

@Operation(summary = "일반/전통주 투표 리스트 검색, 조회", description = "파라미터에 keyeword, sortBy, page, size, category 보내주시면 됩니다. 검색이 아니면 keyword = 에 값 없이 기획상 최신순, 인기순 만")
@GetMapping("/v2")
public ResponseEntity<Slice<VoteData>> 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<Slice<VoteData>> getParticipatedVotes(@RequestAttribute Long userId,@RequestParam int page, @RequestParam int size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -53,4 +54,5 @@ public interface VoteEntityRepository {
Long findMyParticipatedVoteCnt(Long userId);
Long findMyWrittenVoteCnt(Long userId);
Long findMyBookmarkedVoteCnt(Long userId);
List<VoteCommonData> findVoteCommonDataByTimeAndUserId(Long userId, String keyword, PageRequest pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -127,6 +132,45 @@ public List<VoteCommonData> findVoteCommonDataByTime(String keyword, Pageable pa

}

@Override
public List<VoteCommonData> findVoteCommonDataByTimeAndUserId(Long userId, String keyword, PageRequest pageable) {
int pageNo = pageable.getPageNumber();
int pageSize = pageable.getPageSize();

BooleanExpression keywordExpression = getKeywordExpression(keyword);

NumberExpression<Integer> voteOrder = new CaseBuilder()
.when(voteResult.votedUserId.eq(userId)).then(1)
.otherwise(0);

List<VoteCommonData> 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<VoteData> findDrinkVotesByTime(String keyword, String region, int pageNum, int pageSize) {

Expand Down
26 changes: 26 additions & 0 deletions src/main/java/co/kr/jurumarble/vote/service/VoteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,30 @@ public MyVotesCntData getMyVotes(Long userId) {
Long myBookmarkedVoteCnt = voteRepository.findMyBookmarkedVoteCnt(userId);
return new MyVotesCntData(myWrittenVoteCnt, myParticipatedVoteCnt, myBookmarkedVoteCnt);
}

public Slice<VoteData> 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<VoteData> findVotesByTimeV2(Long userId, String keyword, PageRequest pageable) {
List<VoteCommonData> voteCommonDataByTime;
if(userId == null) {
voteCommonDataByTime = voteRepository.findVoteCommonDataByTime(keyword, pageable);
} else {
voteCommonDataByTime = voteRepository.findVoteCommonDataByTimeAndUserId(userId, keyword, pageable);
}
return voteFinder.getVoteData(pageable, voteCommonDataByTime);
}

}

0 comments on commit 9b980cc

Please sign in to comment.