-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feature: 카테고리 검색 구현, userId resolver 해결, querydsl 추가
- Loading branch information
Showing
17 changed files
with
244 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
17 changes: 17 additions & 0 deletions
17
linkmind/src/main/java/com/app/toaster/config/JpaQueryFactoryConfig.java
This file contains 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,17 @@ | ||
package com.app.toaster.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
import jakarta.persistence.EntityManager; | ||
|
||
@Configuration | ||
public class JpaQueryFactoryConfig { | ||
|
||
@Bean | ||
JPAQueryFactory jpaQueryFactory(EntityManager em) { | ||
return new JPAQueryFactory(em); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
linkmind/src/main/java/com/app/toaster/config/WebConfig.java
This file contains 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,21 @@ | ||
package com.app.toaster.config; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
private final UserIdResolver userIdResolver; | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { | ||
resolvers.add(userIdResolver); | ||
} | ||
} |
This file contains 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
29 changes: 29 additions & 0 deletions
29
linkmind/src/main/java/com/app/toaster/controller/CategoryController.java
This file contains 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,29 @@ | ||
package com.app.toaster.controller; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.app.toaster.common.dto.ApiResponse; | ||
import com.app.toaster.domain.Category; | ||
import com.app.toaster.infrastructure.CategoryRepository; | ||
import com.app.toaster.service.search.SearchService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/category") | ||
public class CategoryController { | ||
private final SearchService searchService; | ||
|
||
@GetMapping("/search") | ||
public ApiResponse searchProducts(@RequestHeader Long userId ,@RequestParam("query") String query){ | ||
return searchService.searchCategoryTitle(userId,query); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
linkmind/src/main/java/com/app/toaster/controller/MainController.java
This file contains 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,26 @@ | ||
package com.app.toaster.controller; | ||
|
||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.app.toaster.common.dto.ApiResponse; | ||
import com.app.toaster.service.search.SearchService; | ||
import com.app.toaster.service.toast.ToastService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/main") | ||
public class MainController { | ||
private final SearchService searchService; | ||
|
||
@GetMapping("/search") | ||
public ApiResponse searchProducts(@RequestHeader Long userId ,@RequestParam("query") String query){ | ||
return searchService.searchMain(userId,query); | ||
} | ||
} |
This file contains 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
7 changes: 7 additions & 0 deletions
7
linkmind/src/main/java/com/app/toaster/controller/response/search/CategoryResult.java
This file contains 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,7 @@ | ||
package com.app.toaster.controller.response.search; | ||
|
||
public record CategoryResult(Long categoryId, String title) { | ||
public static CategoryResult of(Long categoryId, String title){ | ||
return new CategoryResult(categoryId, title); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
linkmind/src/main/java/com/app/toaster/controller/response/search/SearchCategoryResult.java
This file contains 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,11 @@ | ||
package com.app.toaster.controller.response.search; | ||
|
||
import java.util.List; | ||
|
||
import com.app.toaster.domain.Category; | ||
|
||
public record SearchCategoryResult(List<CategoryResult> categories) { | ||
public static SearchCategoryResult of(List<CategoryResult> categories){ | ||
return new SearchCategoryResult(categories); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
linkmind/src/main/java/com/app/toaster/controller/response/search/SearchMainResult.java
This file contains 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,9 @@ | ||
package com.app.toaster.controller.response.search; | ||
|
||
import java.util.List; | ||
|
||
public record SearchMainResult(List<ToastResult> toasts, List<CategoryResult> categories) { | ||
public static SearchMainResult of(List<ToastResult> toasts, List<CategoryResult> categories){ | ||
return new SearchMainResult(toasts,categories); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
linkmind/src/main/java/com/app/toaster/controller/response/search/ToastResult.java
This file contains 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,7 @@ | ||
package com.app.toaster.controller.response.search; | ||
|
||
public record ToastResult(Long toastId, String title) { | ||
public static ToastResult of(Long toastId, String title){ | ||
return new ToastResult(toastId, title); | ||
} | ||
} |
This file contains 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 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 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
linkmind/src/main/java/com/app/toaster/infrastructure/ToastRepository.java
This file contains 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 |
---|---|---|
@@ -1,8 +1,18 @@ | ||
package com.app.toaster.infrastructure; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import com.app.toaster.domain.Category; | ||
import com.app.toaster.domain.Toast; | ||
|
||
public interface ToastRepository extends JpaRepository<Toast, Long> { | ||
|
||
@Query("SELECT t FROM Toast t WHERE " + | ||
"t.user.userId = :userId and " + | ||
"t.title LIKE CONCAT('%',:query, '%')" | ||
) | ||
List<Toast> searchToastsByQuery(Long userId, String query); | ||
} |
This file contains 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
70 changes: 70 additions & 0 deletions
70
linkmind/src/main/java/com/app/toaster/service/search/SearchService.java
This file contains 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,70 @@ | ||
package com.app.toaster.service.search; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.app.toaster.common.dto.ApiResponse; | ||
import com.app.toaster.controller.response.search.CategoryResult; | ||
import com.app.toaster.controller.response.search.SearchCategoryResult; | ||
import com.app.toaster.controller.response.search.SearchMainResult; | ||
import com.app.toaster.controller.response.search.ToastResult; | ||
import com.app.toaster.domain.Category; | ||
import com.app.toaster.domain.Toast; | ||
import com.app.toaster.domain.User; | ||
import com.app.toaster.exception.Error; | ||
import com.app.toaster.exception.Success; | ||
import com.app.toaster.exception.model.CustomException; | ||
import com.app.toaster.exception.model.NotFoundException; | ||
import com.app.toaster.infrastructure.CategoryRepository; | ||
import com.app.toaster.infrastructure.ToastRepository; | ||
import com.app.toaster.infrastructure.UserRepository; | ||
import com.google.protobuf.Api; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class SearchService { | ||
private final ToastRepository toastRepository; | ||
private final UserRepository userRepository; | ||
private final CategoryRepository categoryRepository; | ||
|
||
public ApiResponse<SearchCategoryResult> searchCategoryTitle(Long userId, String searchParam){ | ||
User presentUser = userRepository.findByUserId(userId).orElseThrow( | ||
()-> new NotFoundException(Error.NOT_FOUND_USER_EXCEPTION, Error.NOT_FOUND_USER_EXCEPTION.getMessage()) | ||
); | ||
List<Category> searchCategoryList = categoryRepository.searchCategoriesByQuery(userId, searchParam); | ||
|
||
if (searchCategoryList.isEmpty()){ | ||
return ApiResponse.success(Success.SEARCH_SUCCESS_BUT_IS_EMPTY,null); | ||
} | ||
|
||
return ApiResponse.success(Success.SEARCH_SUCCESS, SearchCategoryResult.of( | ||
searchCategoryList.stream().map( | ||
category -> CategoryResult.of(category.getCategoryId(), category.getTitle())) | ||
.collect(Collectors.toList()))); | ||
} | ||
|
||
public ApiResponse<SearchMainResult> searchMain(Long userId, String searchParam){ | ||
User presentUser = userRepository.findByUserId(userId).orElseThrow( | ||
()-> new NotFoundException(Error.NOT_FOUND_USER_EXCEPTION, Error.NOT_FOUND_USER_EXCEPTION.getMessage()) | ||
); | ||
List<Toast> searchToastList = toastRepository.searchToastsByQuery(userId, searchParam); | ||
List<Category> searchCategoryList = categoryRepository.searchCategoriesByQuery(userId, searchParam); | ||
|
||
if (searchToastList.isEmpty() && searchCategoryList.isEmpty()){ | ||
return ApiResponse.success(Success.SEARCH_SUCCESS_BUT_IS_EMPTY,null); | ||
} | ||
return ApiResponse.success(Success.SEARCH_SUCCESS, SearchMainResult.of( | ||
searchToastList.stream().map( | ||
toast -> ToastResult.of(toast.getId(), toast.getTitle())) | ||
.collect(Collectors.toList()), | ||
searchCategoryList.stream().map( | ||
category -> CategoryResult.of(category.getCategoryId(), category.getTitle())) | ||
.collect(Collectors.toList()) | ||
)); | ||
} | ||
|
||
} |