Skip to content

Commit

Permalink
Merge pull request #69 from feedback-forest/feature/categoryFilter
Browse files Browse the repository at this point in the history
[Feat] 카테고리 필터링 구현
  • Loading branch information
Bumnote authored Nov 1, 2024
2 parents 303ba88 + fc18916 commit 08d21b0
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 26 deletions.
38 changes: 25 additions & 13 deletions src/main/java/zerobase/sijak/controller/SearchController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package zerobase.sijak.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.web.bind.annotation.*;
import zerobase.sijak.dto.CategoryRequest;
import zerobase.sijak.dto.CommonResponse;
import zerobase.sijak.dto.LectureHomeResponse;
import zerobase.sijak.dto.SearchRequest;
Expand All @@ -15,25 +17,21 @@

@Slf4j
@RestController
@RequestMapping("/api")
@RequestMapping("/api/search")
@RequiredArgsConstructor
public class SearchController {


private final SearchService searchService;

public SearchController(SearchService searchService) {
this.searchService = searchService;
}

@GetMapping("/search")
public CommonResponse<?> search(@RequestHeader("Authorization") String token,
@RequestBody SearchRequest searchRequest,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "9") int size
) {
@GetMapping("/keyword")
public CommonResponse<?> searchKeyword(@RequestHeader("Authorization") String token,
@RequestBody SearchRequest searchRequest,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "9") int size
) {

Pageable pageable = PageRequest.of(page, size);
Slice<LectureHomeResponse> lectures = searchService.search(token, pageable, searchRequest);
Slice<LectureHomeResponse> lectures = searchService.searchKeyword(token, pageable, searchRequest);

Map<String, Object> totalList = new HashMap<>();
totalList.put("data", lectures.getContent());
Expand All @@ -42,5 +40,19 @@ public CommonResponse<?> search(@RequestHeader("Authorization") String token,
return CommonResponse.of(totalList);
}

@GetMapping("/category")
public CommonResponse<?> searchCategory(@RequestHeader("Authorization") String token,
@RequestBody CategoryRequest categoryRequest,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "9") int size
) {

Pageable pageable = PageRequest.of(page, size);
Slice<LectureHomeResponse> lectures = searchService.searchCategory(token, pageable, categoryRequest);

Map<String, Object> totalList = new HashMap<>();
totalList.put("data", lectures.getContent());
totalList.put("hasNext", lectures.hasNext());
return CommonResponse.of(totalList);
}
}
4 changes: 2 additions & 2 deletions src/main/java/zerobase/sijak/controller/TokenController.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public CommonResponse<?> login(@RequestBody UserLoginRequestDTO userLoginRequest

@PostMapping("/reissue")
public CommonResponse<?> reissue(@RequestBody TokenRequest tokenRequest,
HttpServletResponse response) {
log.info("response: {}", response);
HttpServletResponse response) {

log.info("response: {}", response.getStatus());
TokenDTO tokenDTO = kakaoUserService.reissue(tokenRequest, response);

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/zerobase/sijak/dto/CategoryRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package zerobase.sijak.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CategoryRequest {

private String category;

private String location;

}
6 changes: 4 additions & 2 deletions src/main/java/zerobase/sijak/dto/LectureHomeResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@AllArgsConstructor
@NoArgsConstructor
Expand Down Expand Up @@ -54,4 +52,8 @@ public class LectureHomeResponse {

private boolean heart;

private String category;

private String price;

}
3 changes: 2 additions & 1 deletion src/main/java/zerobase/sijak/dto/PickHomeResponse.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package zerobase.sijak.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -48,4 +47,6 @@ public class PickHomeResponse {

private String division;

private String price;

}
3 changes: 1 addition & 2 deletions src/main/java/zerobase/sijak/exception/Code.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public enum Code {
// Success
OK(0, "success"),

// Failed
// Exception
// Failed Exception

// Lecture Exception
CRAWLING_FAILED(4000, "크롤링 작업 도중 실패했습니다."),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/zerobase/sijak/jwt/JwtTokenProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
public class JwtTokenProvider {

private static final String BEARER_TYPE = "Bearer";
private static final long TOKEN_EXPIRE_TIME = 1000 * 15;
private static final long TOKEN_REFRESH_TIME = 1000 * 60 * 60 * 24;
private static final long TOKEN_EXPIRE_TIME = 1000 * 20 ;
private static final long TOKEN_REFRESH_TIME = 1000 * 60 * 60 * 4;
private final Key key;

//생성자를 통하여 KEY 값을 BASE64로 디코딩(해석)하고 해석한 값을
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/zerobase/sijak/jwt/KakaoUserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public TokenDTO reissue(TokenRequest tokenRequest, HttpServletResponse response)

// 4. 저장소에서 Kakao User ID 를 기반으로 Refresh Token 값 가져옴
RefreshToken refreshToken = refreshTokenRepository.findById(authentication.getName())
.orElseThrow(() -> new CustomException(Code.USER_UNAUTHORIZED));
.orElseThrow(() -> new CustomException(Code.EXPIRED_REFRESH_TOKEN));

// 5. Refresh Token 일치 검사
if (!refreshToken.getValue().equals(tokenRequest.getRefreshToken())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
public interface LectureQueryDslRepository {

Slice<Lecture> searchKeywordAndLocation(Pageable pageable, String keyword, String location);

Slice<Lecture> searchCategoryAndLocation(Pageable pageable, String category, String location);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ public Slice<Lecture> searchKeywordAndLocation(Pageable pageable, String keyword
return new SliceImpl<>(lectures, pageable, hasNext);
}

@Override
public Slice<Lecture> searchCategoryAndLocation(Pageable pageable, String category, String location) {
QLecture lecture = QLecture.lecture;

List<Lecture> lectures = jpaQueryFactory.selectFrom(lecture)
.where(eqCategory(category), eqLocation(location), eqStatusTrue())
.orderBy(lecture.deadline.asc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize() + 1)
.fetch();

System.out.println("lecture cnt = " + lectures.size());
boolean hasNext = lectures.size() > pageable.getPageSize();

if (hasNext) {
lectures.remove(lectures.size() - 1); // 다음 페이지 확인용으로 가져온 추가 데이터 제거
}

return new SliceImpl<>(lectures, pageable, hasNext);
}

// 제목과 설명에 해당 keyword 가 포함되어있는 강좌를 모두 찾는다.
private BooleanExpression containsKeyword(String keyword) {
if (StringUtils.isBlank(keyword)) return null;
Expand All @@ -53,6 +74,12 @@ private BooleanExpression eqLocation(String location) {
return QLecture.lecture.address.contains(location);
}

// 해당 카테고리와 일치하는 강좌를 모두 찾는다.
private BooleanExpression eqCategory(String category) {
if (StringUtils.isBlank(category)) return null;
return QLecture.lecture.category.eq(category);
}

// 활성화된 강좌를 모두 찾는다.
private BooleanExpression eqStatusTrue() {
return QLecture.lecture.status.eq(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public RefreshTokenRepository(final RedisTemplate<String, String> redisTemplate)
public void save(final RefreshToken refreshToken) {
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
valueOperations.set(refreshToken.getKakaoUserID(), refreshToken.getRefreshToken());
redisTemplate.expire(refreshToken.getKakaoUserID(), 60, TimeUnit.SECONDS);
redisTemplate.expire(refreshToken.getKakaoUserID(), 30, TimeUnit.SECONDS);
}

public Optional<RefreshToken> findById(final String refreshToken) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/zerobase/sijak/service/LectureService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public Slice<LectureHomeResponse> readHome(String token, Pageable pageable) {
.hostedBy(lecture.getCenterName())
.dayOfWeek(lecture.getDayOfWeek())
.time(lecture.getTime())
.price(lecture.getPrice())
.target(lecture.getTarget())
.status(lecture.isStatus())
.longAddress(lecture.getAddress())
Expand Down Expand Up @@ -100,6 +101,7 @@ public Slice<LectureHomeResponse> readHome(String token, Pageable pageable) {
.latitude(lecture.getLatitude())
.longitude(lecture.getLongitude())
.hostedBy(lecture.getCenterName())
.price(lecture.getPrice())
.dayOfWeek(lecture.getDayOfWeek())
.target(lecture.getTarget())
.status(lecture.isStatus())
Expand Down Expand Up @@ -133,6 +135,7 @@ public Slice<LectureHomeResponse> readLectures(String token, Pageable pageable)
.latitude(lecture.getLatitude())
.longitude(lecture.getLongitude())
.hostedBy(lecture.getCenterName())
.price(lecture.getPrice())
.dayOfWeek(lecture.getDayOfWeek())
.longAddress(lecture.getAddress())
.shortAddress(shortAddress)
Expand Down Expand Up @@ -167,6 +170,7 @@ public Slice<LectureHomeResponse> readLectures(String token, Pageable pageable)
.target(lecture.getTarget())
.longAddress(lecture.getAddress())
.shortAddress(shortAddress)
.price(lecture.getPrice())
.latitude(lecture.getLatitude())
.longitude(lecture.getLongitude())
.hostedBy(lecture.getCenterName())
Expand Down Expand Up @@ -373,6 +377,7 @@ public List<PickHomeResponse> getPickClasses(String token) {
.endDate(lecture.getEndDate())
.dayOfWeek(lecture.getDayOfWeek())
.status(lecture.isStatus())
.price(lecture.getPrice())
.target(lecture.getTarget())
.link(lecture.getLink())
.heart(false)
Expand Down Expand Up @@ -410,6 +415,7 @@ public List<PickHomeResponse> getPickClasses(String token) {
.endDate(lecture.getEndDate())
.dayOfWeek(lecture.getDayOfWeek())
.status(lecture.isStatus())
.price(lecture.getPrice())
.target(lecture.getTarget())
.link(lecture.getLink())
.heart(isHeart)
Expand Down Expand Up @@ -438,6 +444,7 @@ public Slice<LectureHomeResponse> readLectureByLocation(String token, String loc
.endDate(lecture.getEndDate())
.latitude(lecture.getLatitude())
.longitude(lecture.getLongitude())
.price(lecture.getPrice())
.hostedBy(lecture.getCenterName())
.dayOfWeek(lecture.getDayOfWeek())
.longAddress(lecture.getAddress())
Expand Down Expand Up @@ -478,6 +485,7 @@ public Slice<LectureHomeResponse> readLectureByLocation(String token, String loc
.shortAddress(shortAddress)
.latitude(lecture.getLatitude())
.longitude(lecture.getLongitude())
.price(lecture.getPrice())
.hostedBy(lecture.getCenterName())
.status(lecture.isStatus())
.startDate(lecture.getStartDate())
Expand Down
Loading

0 comments on commit 08d21b0

Please sign in to comment.