Skip to content

Commit

Permalink
Feat: 찜한 레시피 카테고리리 필터링 #39
Browse files Browse the repository at this point in the history
  • Loading branch information
7beunseo committed Aug 14, 2024
1 parent 589512d commit 03ea91f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/ohmea/todayrecipe/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ public ResponseEntity<ResponseDTO<List<RecipeResponseDTO>>> getLikedRecipes() {
.body(new ResponseDTO<>(200, "찜한 레시피 조회가 완료되었습니다.", response));
}


// 찜한 레시피 카테고리별 출력
@GetMapping("/like/filter")
public ResponseEntity<ResponseDTO> getLikedRecipesByCategory(@RequestParam("category") String category) {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
List<RecipeResponseDTO> response = userService.getLikedRecipesByCategory(username, category);
return ResponseEntity
.status(HttpStatus.OK.value())
.body(new ResponseDTO<>(200, "찜한 레시피 카테고리별 조회가 완료되었습니다.", response));
}

// (알고리즘) 찜한 레시피
@GetMapping("/recommend/category")
public ResponseEntity<ResponseDTO<List<RecipeResponseDTO>>> getTopCategoryRecipes() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ohmea/todayrecipe/entity/CommentEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public class CommentEntity {
@Column(name = "created_at")
private LocalDateTime createdAt;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "recipe_id") // 외래 키 컬럼 이름을 명확히 지정
private RecipeEntity recipe;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private UserEntity user;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ohmea/todayrecipe/entity/LikeEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class LikeEntity {
@JoinColumn(name = "user_id", nullable = false)
private UserEntity user;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "recipe_id", nullable = false)
private RecipeEntity recipe;
}
4 changes: 2 additions & 2 deletions src/main/java/com/ohmea/todayrecipe/entity/UserEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public class UserEntity {
// admin
private String role;
// refrigerator 식재료들
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<IngredientEntity> ingredients = new ArrayList<>();

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<LikeEntity> likes = new ArrayList<>();

// user 정보 업데이트
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/ohmea/todayrecipe/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,26 @@ public List<RecipeResponseDTO> getLikedRecipes(String username) {
.collect(Collectors.toList());
}

// 찜 레시피 카테고리별 필터링
public List<RecipeResponseDTO> getLikedRecipesByCategory(String username, String category) {
UserEntity user = userRepository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("해당 사용자 이름을 가진 사용자를 찾을 수 없습니다: " + username));

// 사용자의 모든 찜 레시피 가져오기
List<RecipeEntity> recipeEntities = user.getLikes().stream()
.map(LikeEntity::getRecipe).toList();

// 카테고리 필터링
List<RecipeEntity> filteredRecipes = recipeEntities.stream()
.filter(recipe -> recipe.getCategory().equals(category))
.toList();

// DTO로 변환하여 응답
return filteredRecipes.stream()
.map(RecipeResponseDTO::toDto)
.toList();
}

// (알고리즘) 찜한 레시피
public List<RecipeResponseDTO> getTopCategoryRecipes(String username) {
UserEntity user = userRepository.findByUsername(username)
Expand Down Expand Up @@ -178,4 +198,5 @@ public List<RecipeResponseDTO> getTopCategoryRecipes(String username) {
.map(RecipeResponseDTO::toDto)
.collect(Collectors.toList());
}

}

0 comments on commit 03ea91f

Please sign in to comment.