Skip to content

Commit

Permalink
Merge pull request #83 from minseok1015/dev
Browse files Browse the repository at this point in the history
Keyword 검색 api 완료(뱃지도 같이 response)
  • Loading branch information
minseok1015 authored Aug 13, 2024
2 parents 9bf5100 + 59d52c7 commit e75d60a
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package store.itpick.backend.common.exception;


import lombok.Getter;
import store.itpick.backend.common.response.status.ResponseStatus;

@Getter
public class KeywordException extends RuntimeException{
private final ResponseStatus exceptionStatus;

public KeywordException(ResponseStatus exceptionStatus) {
super(exceptionStatus.getMessage());
this.exceptionStatus = exceptionStatus;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package store.itpick.backend.common.exception_handler;


import jakarta.annotation.Priority;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import store.itpick.backend.common.exception.KeywordException;
import store.itpick.backend.common.exception.ReferenceException;
import store.itpick.backend.common.response.BaseErrorResponse;

@Slf4j
@Priority(0)
@RestControllerAdvice
public class KeywordExceptionControllerAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(KeywordException.class)
public BaseErrorResponse handle_UserException(KeywordException e) {
log.error("[handle_KeywordException]", e);
return new BaseErrorResponse(e.getExceptionStatus(), e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ReferenceExceptionControllerAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ReferenceException.class)
public BaseErrorResponse handle_UserException(ReferenceException e) {
log.error("[handle_UserException]", e);
log.error("[handle_ReferenceException]", e);
return new BaseErrorResponse(e.getExceptionStatus(), e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ public enum BaseExceptionResponseStatus implements ResponseStatus {
INVALID_REFERENCE(8000,HttpStatus.BAD_REQUEST.value(),"잘못된 관련자료 요청입니다"),
NO_Search_REFERENCE(8001,HttpStatus.BAD_REQUEST.value(),"관련자료를 뉴스에서 검색하지 못하였습니다"),

EMPTY_REFERENCE(8002,HttpStatus.BAD_REQUEST.value(),"해당 키워드의 관련자료를 찾지 못하였습니다");
EMPTY_REFERENCE(8002,HttpStatus.BAD_REQUEST.value(),"해당 키워드의 관련자료를 찾지 못하였습니다"),

NO_SEARCH_KEYWORD(8004,HttpStatus.BAD_REQUEST.value(),"관련된 키워드를 찾지 못했습니다");



Expand Down
2 changes: 1 addition & 1 deletion src/main/java/store/itpick/backend/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(jwtAuthenticationInterceptor)
.order(1)
.addPathPatterns("/**")
.excludePathPatterns("/auth/login", "/auth/signup", "/auth/refresh","/auth/emails/**", "/rank/**","/auth/email/check","/auth/nickname/check","/favicon.ico");
.excludePathPatterns("/auth/login", "/auth/signup", "/auth/refresh","/auth/emails/**", "/rank/**","/auth/email/check","/auth/nickname/check","/favicon.ico","/keyword/**");
//인터셉터 적용 범위 수정
registry.addInterceptor(getJwtInterceptor)
.addPathPatterns("/user/email");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package store.itpick.backend.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import store.itpick.backend.common.exception.KeywordException;
import store.itpick.backend.common.response.BaseResponse;
import store.itpick.backend.dto.keyword.SearchDTO;
import store.itpick.backend.model.Keyword;
import store.itpick.backend.service.KeywordService;

import java.util.List;

import static store.itpick.backend.common.response.status.BaseExceptionResponseStatus.EMPTY_REFERENCE;
import static store.itpick.backend.common.response.status.BaseExceptionResponseStatus.NO_SEARCH_KEYWORD;

@RestController
@RequiredArgsConstructor
@RequestMapping("/keyword")
public class KeywordController {

private final KeywordService keywordService;

@GetMapping("/search/nobadge")
public BaseResponse<List<String>> searchKeywords(@RequestParam String query) {
List<String> keywords = keywordService.searchKeywords(query);
if(keywords.isEmpty()){
throw new KeywordException(NO_SEARCH_KEYWORD);
}
return new BaseResponse<>(keywords);
}
@GetMapping("/search")
public BaseResponse<List<SearchDTO>> searchKeywordsWithBadge(@RequestParam String query) {
List<SearchDTO> keywords = keywordService.searchKeywordsWithBadge(query);
if(keywords.isEmpty()){
throw new KeywordException(NO_SEARCH_KEYWORD);
}
return new BaseResponse<>(keywords);
}
}
14 changes: 14 additions & 0 deletions src/main/java/store/itpick/backend/dto/keyword/SearchDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package store.itpick.backend.dto.keyword;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class SearchDTO {
private String keyword;
private long nateRank;
private long naverRank;
private long zumRank;
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ Optional<Keyword> findTop1ByKeywordAndCommunityOrderByUpdatedAtDesc(@Param("keyw
@Query("SELECT k FROM Keyword k JOIN k.communityPeriods cp WHERE cp.community = 'zum' ORDER BY k.updateAt DESC")
List<Keyword> findTop10ByCommunityZum(Pageable pageable);


/** 검색할때 사용하는 JPA **/
List<Keyword> findByKeywordStartingWithIgnoreCase(String substring);


}
38 changes: 35 additions & 3 deletions src/main/java/store/itpick/backend/service/KeywordService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import store.itpick.backend.dto.keyword.SearchDTO;
import store.itpick.backend.dto.redis.RankDTO;
import store.itpick.backend.dto.redis.RankListForKeyword;
import store.itpick.backend.model.CommunityPeriod;
import store.itpick.backend.model.Keyword;
import store.itpick.backend.model.rank.PeriodType;
import store.itpick.backend.repository.CommunityPeriodRepository;
import store.itpick.backend.repository.KeywordRepository;
import store.itpick.backend.util.Redis;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand All @@ -27,6 +30,7 @@ public class KeywordService {

private final KeywordRepository keywordRepository;
private final CommunityPeriodRepository communityPeriodRepository;
private final Redis redis;



Expand Down Expand Up @@ -176,4 +180,32 @@ public void performDailyTasksZum() {
keywordRepository.save(keyword); // 연관관계 업데이트 후 저장
}
}

public List<String> searchKeywords(String keyword) {
List<Keyword> keywordList= keywordRepository.findByKeywordStartingWithIgnoreCase(keyword);
List<String> keywords= new ArrayList<>();
for (Keyword keyword1 : keywordList) {
keywords.add(keyword1.getKeyword());
}
return keywords;
}

public List<SearchDTO> searchKeywordsWithBadge(String keyword) {
List<Keyword> keywordList= keywordRepository.findByKeywordStartingWithIgnoreCase(keyword);
List<String> keywords= new ArrayList<>();
for (Keyword keyword1 : keywordList) {
keywords.add(keyword1.getKeyword());
}

List<SearchDTO> rankingList = new ArrayList<>();
for (String keywordBadge : keywords) {
RankListForKeyword rankingBadgeResponse = redis.getRankingBadgeResponse(keywordBadge, PeriodType.BY_REAL_TIME, "");
rankingList.add(new SearchDTO(keywordBadge, rankingBadgeResponse.getNateRank(), rankingBadgeResponse.getNaverRank(), rankingBadgeResponse.getZumRank()));
}
return rankingList;
}




}

0 comments on commit e75d60a

Please sign in to comment.