-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 검색 기능 구현 #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: 검색 기능 구현 #130
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/codeit/todo/common/exception/search/SearchException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.codeit.todo.common.exception.search; | ||
|
|
||
| import com.codeit.todo.common.exception.ApplicationException; | ||
| import com.codeit.todo.common.exception.payload.ErrorStatus; | ||
|
|
||
| public class SearchException extends ApplicationException { | ||
| /** | ||
| * @param errorStatus 상태 코드, 메세지, 발생시간을 저장한 객체 | ||
| */ | ||
| public SearchException(ErrorStatus errorStatus) { | ||
| super(errorStatus); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/codeit/todo/domain/enums/SearchField.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.codeit.todo.domain.enums; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public enum SearchField { | ||
| USER_NAME("유저명"), | ||
| GOAL_TITLE("목표명"); | ||
|
|
||
| private final String value; | ||
|
|
||
| SearchField(String value){ | ||
| this.value = value; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/codeit/todo/service/search/SearchService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.codeit.todo.service.search; | ||
|
|
||
| import com.codeit.todo.web.dto.request.search.ReadSearchRequest; | ||
| import com.codeit.todo.web.dto.response.search.ReadSearchResponse; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface SearchService { | ||
| List<ReadSearchResponse> findUserAndGoal(ReadSearchRequest request); | ||
| } |
85 changes: 85 additions & 0 deletions
85
src/main/java/com/codeit/todo/service/search/impl/SearchServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package com.codeit.todo.service.search.impl; | ||
|
|
||
| import com.codeit.todo.common.exception.EntityNotFoundException; | ||
| import com.codeit.todo.common.exception.goal.GoalNotFoundException; | ||
| import com.codeit.todo.common.exception.payload.ErrorStatus; | ||
| import com.codeit.todo.common.exception.search.SearchException; | ||
| import com.codeit.todo.common.exception.user.UserNotFoundException; | ||
| import com.codeit.todo.domain.Goal; | ||
| import com.codeit.todo.domain.User; | ||
| import com.codeit.todo.domain.enums.SearchField; | ||
| import com.codeit.todo.repository.GoalRepository; | ||
| import com.codeit.todo.repository.UserRepository; | ||
| import com.codeit.todo.service.search.SearchService; | ||
| import com.codeit.todo.web.dto.request.search.ReadSearchRequest; | ||
| import com.codeit.todo.web.dto.response.goal.ReadGoalSearchResponse; | ||
| import com.codeit.todo.web.dto.response.search.ReadSearchResponse; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional | ||
| public class SearchServiceImpl implements SearchService { | ||
| private final UserRepository userRepository; | ||
| private final GoalRepository goalRepository; | ||
|
|
||
| private static final int BAD_REQUEST = 400; | ||
|
|
||
| private static final int NOT_FOUND = 404; | ||
|
|
||
| @Override | ||
| public List<ReadSearchResponse> findUserAndGoal(ReadSearchRequest request) { | ||
| String searchField = request.searchField(); | ||
| String keyword = request.keyword(); | ||
|
|
||
| log.info("Starting search for field: {} with keyword: {}", searchField, keyword); | ||
|
|
||
| if( searchField.equals(SearchField.USER_NAME.getValue()) ){ | ||
| List<User> searchedUsers = userRepository.findByNameContains(keyword); | ||
| if(searchedUsers.isEmpty()) throw new SearchException(ErrorStatus.toErrorStatus("유저에 대한 검색 결과가 없습니다.", NOT_FOUND)); | ||
|
|
||
| List<ReadSearchResponse> responses = searchedUsers.stream() | ||
| .map(user -> { | ||
| List<Goal> goals = user.getGoals(); | ||
| List<ReadGoalSearchResponse> goalsResponses = goals.stream() | ||
| .map(ReadGoalSearchResponse::from) | ||
| .toList(); | ||
|
|
||
| return ReadSearchResponse.from(user, goalsResponses); | ||
| }).toList(); | ||
| return responses; | ||
|
|
||
| }else if(searchField.equals(SearchField.GOAL_TITLE.getValue())){ | ||
| List<Goal> searchedGoals = goalRepository.findByGoalTitleContains(keyword); | ||
| if(searchedGoals.isEmpty()) throw new SearchException(ErrorStatus.toErrorStatus("목표에 대한 검색 결과가 없습니다.", NOT_FOUND)); | ||
|
|
||
| Map<User, List<Goal>> userGoalsMap = searchedGoals.stream() | ||
| .collect(Collectors.groupingBy(Goal::getUser)); | ||
|
|
||
| List<ReadSearchResponse> responses = userGoalsMap.entrySet().stream() | ||
| .map(entry -> { | ||
| User user = entry.getKey(); | ||
| List<Goal> goals = entry.getValue(); | ||
|
|
||
| List<ReadGoalSearchResponse> goalsResponses = goals.stream() | ||
| .map(ReadGoalSearchResponse::from) | ||
| .toList(); | ||
|
|
||
| return ReadSearchResponse.from(user, goalsResponses); | ||
|
|
||
| }).toList(); | ||
| return responses; | ||
| }else{ | ||
| throw new SearchException(ErrorStatus.toErrorStatus("해당 필드로는 검색할 수 없습니다", BAD_REQUEST)); | ||
| } | ||
| } | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
src/main/java/com/codeit/todo/web/controller/SearchController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.codeit.todo.web.controller; | ||
|
|
||
| import com.codeit.todo.repository.CustomUserDetails; | ||
|
|
||
| import com.codeit.todo.service.search.SearchService; | ||
| import com.codeit.todo.web.dto.request.search.ReadSearchRequest; | ||
| import com.codeit.todo.web.dto.response.Response; | ||
| import com.codeit.todo.web.dto.response.search.ReadSearchResponse; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/api/v1/searches") | ||
| public class SearchController { | ||
|
|
||
| private final SearchService searchService; | ||
|
|
||
| @Operation(summary = "유저 또는 목표 검색", description = "유저 또는 목표를 검색하고 유저와 목표를 함께 반환합니다") | ||
| @ApiResponses(value = { | ||
| @ApiResponse(responseCode = "200", description = "검색 성공") | ||
| }) | ||
| @GetMapping | ||
| public Response<List<ReadSearchResponse>> getSearch( | ||
| @Valid @RequestBody ReadSearchRequest request | ||
| ){ | ||
| return Response.ok(searchService.findUserAndGoal(request)); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/codeit/todo/web/dto/request/search/ReadSearchRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.codeit.todo.web.dto.request.search; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record ReadSearchRequest( | ||
| @NotNull | ||
| String searchField, | ||
|
|
||
| @NotNull | ||
| String keyword | ||
| ) { | ||
|
|
||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/codeit/todo/web/dto/response/goal/ReadGoalSearchResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.codeit.todo.web.dto.response.goal; | ||
|
|
||
| import com.codeit.todo.domain.Goal; | ||
| import lombok.Builder; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Builder | ||
| public record ReadGoalSearchResponse( | ||
| int goalId, | ||
| String goalTitle, | ||
| String color | ||
|
|
||
| ) { | ||
| public static ReadGoalSearchResponse from(Goal goal){ | ||
| return ReadGoalSearchResponse.builder() | ||
| .goalId(goal.getGoalId()) | ||
| .goalTitle(goal.getGoalTitle()) | ||
| .color(goal.getColor()) | ||
| .build(); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/codeit/todo/web/dto/response/search/ReadSearchResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.codeit.todo.web.dto.response.search; | ||
|
|
||
| import com.codeit.todo.domain.User; | ||
| import com.codeit.todo.web.dto.response.goal.ReadGoalSearchResponse; | ||
| import lombok.Builder; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Builder | ||
| public record ReadSearchResponse ( | ||
| String name, | ||
| String profilePic, | ||
|
|
||
| List<ReadGoalSearchResponse> goals | ||
| ){ | ||
| public static ReadSearchResponse from(User user, List<ReadGoalSearchResponse> responses) { | ||
| return ReadSearchResponse.builder() | ||
| .name(user.getName()) | ||
| .profilePic(user.getProfilePic()) | ||
| .goals(responses) | ||
| .build(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
현재 코드에서는 SearchField 조건이 추가될 경우 else if를 사용해야 해서 코드 복잡도가 증가할거같아요.
검색 조건별로 코드를 별도의 클래스로 분리하여, 조건에 따라 실행할 로직을 동적으로 결정할 수 있는 구조는 어떻게 생각하시나요? 전략 패턴과 유사한 느낌입니다