Skip to content

Commit

Permalink
[feat] 검색어 저장 및 추천검색어 제공
Browse files Browse the repository at this point in the history
  • Loading branch information
jainefer committed Dec 4, 2024
1 parent fdf778a commit 021da9f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
import site.billbill.apiserver.common.response.BaseResponse;
import site.billbill.apiserver.common.utils.jwt.JWTUtil;

import java.util.List;

@Slf4j
@RestController
@Tag(name = "borrowPosts", description = "대여 게시물 관련")
@RequestMapping("/api/v1/posts/borrowPosts")
@RequiredArgsConstructor
public class PostsController {
private final PostsService postsService;
@Operation(summary = "게시물 생성", description = "게시물 생성 API")
@PostMapping("")
public BaseResponse<PostsResponse.UploadResponse> uploadPostsController(@RequestBody @Valid PostsRequest.UploadRequest request){

Expand Down Expand Up @@ -54,6 +57,7 @@ public BaseResponse<PostsResponse.ViewAllResultResponse> getPostsController(
Sort.Direction direction = "asc".equalsIgnoreCase(order) ? Sort.Direction.ASC : Sort.Direction.DESC;
return new BaseResponse<>(postsService.ViewAllPostService(category,page,direction,sortBy));
}
@Operation(summary = "게시물 검색", description = "게시물 검색 API")
@GetMapping("/search")
public BaseResponse<PostsResponse.ViewAllResultResponse> getSearchPostsController(
@Parameter(name = "category", description = "카테고리 필터 (예: entire, camp, sports,tools )", example = "entire", in = ParameterIn.QUERY, required = false)
Expand All @@ -76,11 +80,13 @@ public BaseResponse<PostsResponse.ViewAllResultResponse> getSearchPostsControlle
Sort.Direction direction = "asc".equalsIgnoreCase(order) ? Sort.Direction.ASC : Sort.Direction.DESC;
return new BaseResponse<>(postsService.ViewSearchPostService(userId,category, page, direction, sortBy,keyword,state));
}
@Operation(summary = "게시물 조회", description = "게시물 상세 조회")
@GetMapping("/{postId}")
public BaseResponse<PostsResponse.ViewPostResponse> getPostController(@PathVariable(value = "postId",required = true)String postId){

return new BaseResponse<>(postsService.ViewPostService(postId));
}
@Operation(summary = "게시물 삭제", description = "게시물 삭제")
@DeleteMapping("/{postId}")
public BaseResponse<String> deletePostController(@PathVariable(value = "postId",required = true)String postId){

Expand All @@ -90,6 +96,21 @@ public BaseResponse<String> deletePostController(@PathVariable(value = "postId",
}
return new BaseResponse<>(postsService.deletePostService(postId,userId));
}
@Operation(summary = "저장한 검색어 불러오기", description = "저장한 검색어 불러오기")
@GetMapping("/searchHist")
public BaseResponse<List<String>> getSearchHistController(){
String userId = "";
if(MDC.get(JWTUtil.MDC_USER_ID) != null) {
userId= MDC.get(JWTUtil.MDC_USER_ID).toString();
}
return new BaseResponse<>(postsService.findSearchService(userId));
}
@Operation(summary = "추천 검색어 불러오기", description = "추천 검색어 주기")
@GetMapping("/recommend")
public BaseResponse<List<String>> getRecommendController(){
return new BaseResponse<>(postsService.findRecommandService());
}
@Operation(summary = "게시물 수정", description = "게시물 수정")
@PatchMapping("/{postId}")
public BaseResponse<String> updatePostController(@PathVariable(value="postId",required = true)String postId,
@RequestBody @Valid PostsRequest.UploadRequest request){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import site.billbill.apiserver.model.post.*;
import site.billbill.apiserver.model.user.UserJpaEntity;
import site.billbill.apiserver.model.user.UserSearchHistJpaEntity;
import site.billbill.apiserver.repository.borrowPosts.SearchKeywordStatRepository;

import java.time.format.DateTimeFormatter;
import java.util.List;
Expand Down Expand Up @@ -96,5 +97,12 @@ public static SearchKeywordStatsJpaEntity toSearchKeywordStats(String keyword){
.keyword(keyword)
.searchCount(1).build();
}
public static String toUserSearchHist(UserSearchHistJpaEntity userSearchHist){
return userSearchHist.getKeyword();
}
public static String toRecommandSearch(SearchKeywordStatsJpaEntity searchKeywordStats){
return searchKeywordStats.getKeyword();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import site.billbill.apiserver.api.borrowPosts.dto.request.PostsRequest;
import site.billbill.apiserver.api.borrowPosts.dto.response.PostsResponse;

import java.util.List;

public interface PostsService {
PostsResponse.UploadResponse uploadPostService(PostsRequest.UploadRequest request,String userId);

Expand All @@ -12,7 +14,13 @@ public interface PostsService {
PostsResponse.ViewPostResponse ViewPostService(String postId);

String deletePostService(String postId,String userId);

String UpdatePostService(String postId,String userId,PostsRequest.UploadRequest request);

PostsResponse.ViewAllResultResponse ViewSearchPostService(String userId,String category, int page, Sort.Direction direction, String orderType,String keyword,boolean state);

List<String> findSearchService(String userId);

List<String> findRecommandService();

}
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,19 @@ public PostsResponse.ViewAllResultResponse ViewSearchPostService(String userId,
return PostsConverter.toViewAllList(items);
}

public List<String> findSearchService(String userId){
UserJpaEntity user = userRepository.findById(userId).orElse(null);
List<UserSearchHistJpaEntity> searchHists=userSearchHistRepository.findByUserAndDelYnOrderByCreatedAtDesc(user,false);
List<String> result= searchHists.stream().map(searchHist-> PostsConverter.toUserSearchHist(searchHist)).toList();
return result;

}

public List<String> findRecommandService(){
List<SearchKeywordStatsJpaEntity> searchKeywordStats=searchKeywordStatRepository.findAllByOrderBySearchCountDesc();
List<String> result =searchKeywordStats.stream().map(searchKeywordStat-> PostsConverter.toRecommandSearch(searchKeywordStat)).toList();
return result;
}
//모듈화 코드

private Pageable createPageable(int page, Sort.Direction direction, String orderType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import org.springframework.data.jpa.repository.JpaRepository;
import site.billbill.apiserver.model.post.SearchKeywordStatsJpaEntity;

import java.util.List;

public interface SearchKeywordStatRepository extends JpaRepository<SearchKeywordStatsJpaEntity, Long> {
SearchKeywordStatsJpaEntity findByKeyword(String keyword);
List<SearchKeywordStatsJpaEntity> findAllByOrderBySearchCountDesc();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package site.billbill.apiserver.repository.user;

import org.springframework.data.jpa.repository.JpaRepository;
import site.billbill.apiserver.model.user.UserJpaEntity;
import site.billbill.apiserver.model.user.UserSearchHistJpaEntity;

import java.util.List;


public interface UserSearchHistRepository extends JpaRepository<UserSearchHistJpaEntity, String> {
List<UserSearchHistJpaEntity> findByUserAndDelYnOrderByCreatedAtDesc(UserJpaEntity user,boolean delYn);
}

0 comments on commit 021da9f

Please sign in to comment.