Skip to content

Commit 55b3e64

Browse files
committed
🔀 merge conflict
2 parents 8639c32 + c09d671 commit 55b3e64

File tree

5 files changed

+31
-34
lines changed

5 files changed

+31
-34
lines changed

backend/src/main/java/net/pengcook/recipe/repository/RecipeRepository.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,34 @@
1111
public interface RecipeRepository extends JpaRepository<Recipe, Long> {
1212

1313
@Query("""
14-
SELECT DISTINCT recipe.id
14+
SELECT recipe.id
1515
FROM CategoryRecipe
1616
WHERE category.name = :category
17-
ORDER BY recipe.id DESC
17+
ORDER BY recipe.createdAt DESC
1818
""")
1919
List<Long> findRecipeIdsByCategory(Pageable pageable, String category);
2020

2121
@Query("""
22-
SELECT DISTINCT id
22+
SELECT id
2323
FROM Recipe
2424
WHERE title LIKE CONCAT('%', :keyword, '%')
2525
OR description LIKE CONCAT('%', :keyword, '%')
26-
ORDER BY id DESC
26+
ORDER BY createdAt DESC
2727
""")
2828
List<Long> findRecipeIdsByKeyword(Pageable pageable, String keyword);
2929

30-
List<Recipe> findRecipeByAuthorIdOrderByIdDesc(Pageable pageable, long authorId);
30+
List<Recipe> findRecipeByAuthorIdOrderByCreatedAtDesc(Pageable pageable, long authorId);
3131

3232
@Query("""
33-
SELECT DISTINCT r.id
33+
SELECT r.id
3434
FROM Recipe r
3535
LEFT JOIN CategoryRecipe cr ON cr.recipe = r
3636
LEFT JOIN Category c ON cr.category = c
3737
WHERE (:category IS NULL OR c.name = :category)
3838
AND (:keyword IS NULL OR CONCAT(r.title, r.description) LIKE CONCAT('%', :keyword, '%'))
3939
AND (:userId IS NULL OR r.author.id = :userId)
40-
ORDER BY r.id DESC
40+
GROUP BY r.id, r.createdAt
41+
ORDER BY r.createdAt DESC
4142
""")
4243
List<Long> findRecipeIdsByCategoryAndKeyword(
4344
Pageable pageable,
@@ -46,7 +47,7 @@ List<Long> findRecipeIdsByCategoryAndKeyword(
4647
@Param("userId") @Nullable Long userId
4748
);
4849

49-
List<Recipe> findAllByIdIn(List<Long> recipeIds);
50+
List<Recipe> findAllByIdInOrderByCreatedAtDesc(List<Long> recipeIds);
5051

5152
List<Recipe> findAllByAuthorId(long authorId);
5253

backend/src/main/java/net/pengcook/recipe/service/RecipeService.java

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.pengcook.recipe.service;
22

33
import java.time.LocalTime;
4-
import java.util.Comparator;
54
import java.util.List;
65
import java.util.Optional;
76
import lombok.RequiredArgsConstructor;
@@ -41,7 +40,7 @@
4140
@RequiredArgsConstructor
4241
public class RecipeService {
4342

44-
private static final String KEY_RECIPE = "id";
43+
private static final String CREATION_DATE = "createdAt";
4544

4645
private final RecipeRepository recipeRepository;
4746
private final UserRepository userRepository;
@@ -73,11 +72,10 @@ public List<RecipeHomeWithMineResponse> readRecipes(UserInfo userInfo, PageRecip
7372
@Transactional(readOnly = true)
7473
public List<RecipeHomeWithMineResponseV1> readRecipesV1(UserInfo userInfo, PageRecipeRequest pageRecipeRequest) {
7574
List<Long> recipeIds = findRecipeIdsByMultipleCondition(pageRecipeRequest);
76-
List<Recipe> recipes = recipeRepository.findAllByIdIn(recipeIds);
75+
List<Recipe> recipes = recipeRepository.findAllByIdInOrderByCreatedAtDesc(recipeIds);
7776

7877
return recipes.stream()
7978
.map(recipe -> new RecipeHomeWithMineResponseV1(userInfo, recipe))
80-
.sorted(Comparator.comparing(RecipeHomeWithMineResponseV1::recipeId).reversed())
8179
.toList();
8280
}
8381

@@ -89,7 +87,7 @@ private List<Long> findRecipeIdsByMultipleCondition(PageRecipeRequest pageRecipe
8987
Pageable descPageable = PageRequest.of(
9088
pageable.getPageNumber(),
9189
pageable.getPageSize(),
92-
Sort.by(KEY_RECIPE).descending()
90+
Sort.by(CREATION_DATE).descending()
9391
);
9492
return recipeRepository.findAll(descPageable).stream()
9593
.map(Recipe::getId)
@@ -120,7 +118,7 @@ private List<Long> findRecipeIdsBySingleCondition(PageRecipeRequest pageRecipeRe
120118
return recipeRepository.findRecipeIdsByKeyword(pageable, keyword);
121119
}
122120
if (userId != null) {
123-
return recipeRepository.findRecipeByAuthorIdOrderByIdDesc(pageable, userId).stream()
121+
return recipeRepository.findRecipeByAuthorIdOrderByCreatedAtDesc(pageable, userId).stream()
124122
.map(Recipe::getId)
125123
.toList();
126124
}
@@ -138,11 +136,10 @@ public List<RecipeHomeWithMineResponse> readLikeRecipes(UserInfo userInfo) {
138136
@Transactional(readOnly = true)
139137
public List<RecipeHomeWithMineResponseV1> readLikeRecipesV1(UserInfo userInfo) {
140138
List<Long> likeRecipeIds = likeRepository.findRecipeIdsByUserId(userInfo.getId());
141-
List<Recipe> recipes = recipeRepository.findAllByIdIn(likeRecipeIds);
139+
List<Recipe> recipes = recipeRepository.findAllByIdInOrderByCreatedAtDesc(likeRecipeIds);
142140

143141
return recipes.stream()
144142
.map(recipe -> new RecipeHomeWithMineResponseV1(userInfo, recipe))
145-
.sorted(Comparator.comparing(RecipeHomeWithMineResponseV1::recipeId).reversed())
146143
.toList();
147144
}
148145

@@ -236,14 +233,13 @@ public void deleteRecipe(UserInfo userInfo, Recipe recipe) {
236233
}
237234

238235
private List<RecipeHomeWithMineResponse> getRecipeHomeWithMineResponses(UserInfo userInfo, List<Long> recipeIds) {
239-
List<Recipe> recipes = recipeRepository.findAllByIdIn(recipeIds);
236+
List<Recipe> recipes = recipeRepository.findAllByIdInOrderByCreatedAtDesc(recipeIds);
240237
return recipes.stream()
241238
.map(recipe -> {
242239
List<CategoryResponse> categories = categoryService.findCategoryByRecipe(recipe);
243240
List<IngredientResponse> ingredients = ingredientService.findIngredientByRecipe(recipe);
244241
return new RecipeHomeWithMineResponse(userInfo, recipe, categories, ingredients);
245242
})
246-
.sorted(Comparator.comparing(RecipeHomeWithMineResponse::recipeId).reversed())
247243
.toList();
248244
}
249245

backend/src/test/java/net/pengcook/recipe/controller/RecipeControllerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ void readRecipesOfUser() {
529529
void readRecipeDescription() {
530530
RecipeDescriptionResponse expected = new RecipeDescriptionResponse(2L, "김밥",
531531
new AuthorResponse(1L, "loki", "loki.jpg"), LocalTime.of(1, 0, 0),
532-
"김밥이미지.jpg", 8, 1, 0, "김밥 조리법", LocalDateTime.of(2024, 7, 2, 13, 0, 0),
532+
"김밥이미지.jpg", 8, 1, 0, "김밥 조리법", LocalDateTime.of(2024, 7, 2, 11, 0, 0),
533533
List.of(new CategoryResponse(1, "한식"), new CategoryResponse(3, "채식")),
534534
List.of(new IngredientResponse(2, "쌀", Requirement.REQUIRED),
535535
new IngredientResponse(3, "계란", Requirement.OPTIONAL),

backend/src/test/java/net/pengcook/recipe/repository/RecipeRepositoryTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void findRecipeIdsByCategoryAndKeyword() {
6262
void findRecipeIdsByUserId() {
6363
Pageable pageable = PageRequest.of(0, 3);
6464

65-
List<Long> recipeIds = repository.findRecipeByAuthorIdOrderByIdDesc(pageable, 1L).stream()
65+
List<Long> recipeIds = repository.findRecipeByAuthorIdOrderByCreatedAtDesc(pageable, 1L).stream()
6666
.map(Recipe::getId)
6767
.toList();
6868

backend/src/test/resources/data/recipe.sql

+14-14
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,21 @@ VALUES ('한식'),
5858
('스프');
5959

6060
INSERT INTO recipe (title, author_id, cooking_time, thumbnail, difficulty, like_count, comment_count, description, created_at)
61-
VALUES ('김치볶음밥', 1, '00:30:00', '김치볶음밥이미지.jpg', 3, 2, 0, '김치볶음밥 조리법', '2024-07-02 13:00:00'),
62-
('김밥', 1, '01:00:00', '김밥이미지.jpg', 8, 1, 0, '김밥 조리법', '2024-07-02 13:00:00'),
63-
('김치찌개', 1, '00:30:00', '김치찌개이미지.jpg', 3, 2, 0, '김치찌개 조리법', '2024-07-02 13:00:00'),
61+
VALUES ('김치볶음밥', 1, '00:30:00', '김치볶음밥이미지.jpg', 3, 2, 0, '김치볶음밥 조리법', '2024-07-01 13:00:00'),
62+
('김밥', 1, '01:00:00', '김밥이미지.jpg', 8, 1, 0, '김밥 조리법', '2024-07-02 11:00:00'),
63+
('김치찌개', 1, '00:30:00', '김치찌개이미지.jpg', 3, 2, 0, '김치찌개 조리법', '2024-07-02 12:00:00'),
6464
('토마토스파게티', 1, '00:30:00', '토마토스파게티이미지.jpg', 3, 2, 0, '토마토스파게티 조리법', '2024-07-02 13:00:00'),
65-
('간장계란밥', 1, '00:10:00', '간장계란밥이미지.jpg', 1, 3, 0, '간장계란밥 조리법', '2024-07-02 13:00:00'),
66-
('피자', 1, '00:30:00', '피자이미지.jpg', 3, 2, 0, '피자 조리법', '2024-07-02 13:00:00'),
67-
('된장찌개', 1, '00:30:00', '된장찌개이미지.jpg', 3, 2, 0, '된장찌개 조리법', '2024-07-02 13:00:00'),
68-
('햄버거', 1, '00:30:00', '햄버거이미지.jpg', 3, 2, 0, '햄버거 조리법', '2024-07-02 13:00:00'),
69-
('흰쌀밥', 1, '00:40:00', '흰쌀밥이미지.jpg', 2, 4, 0, '흰쌀밥 조리법', '2024-07-02 13:00:00'),
70-
('샐러드', 1, '00:15:00', '샐러드이미지.jpg', 1, 5, 0, '샐러드 조리법', '2024-07-02 13:00:00'),
71-
('연어스테이크', 1, '00:45:00', '연어스테이크이미지.jpg', 4, 3, 0, '연어스테이크 조리법', '2024-07-02 13:00:00'),
72-
('초콜릿 케이크', 1, '01:20:00', '초콜릿케이크이미지.jpg', 5, 6, 0, '초콜릿 케이크 조리법', '2024-07-02 13:00:00'),
73-
('베지터블 스프', 1, '00:50:00', '베지터블스프이미지.jpg', 3, 2, 0, '베지터블 스프 조리법', '2024-07-02 13:00:00'),
74-
('카레라이스', 1, '00:30:00', '카레라이스이미지.jpg', 3, 2, 0, '카레라이스 조리법', '2024-07-02 13:00:00'),
75-
('새우볶음밥', 2, '00:25:00', '새우볶음밥이미지.jpg', 2, 3, 0, '새우볶음밥 조리법', '2024-07-02 13:00:00');
65+
('간장계란밥', 1, '00:10:00', '간장계란밥이미지.jpg', 1, 3, 0, '간장계란밥 조리법', '2024-07-02 16:00:00'),
66+
('피자', 1, '00:30:00', '피자이미지.jpg', 3, 2, 0, '피자 조리법', '2024-07-02 17:00:00'),
67+
('된장찌개', 1, '00:30:00', '된장찌개이미지.jpg', 3, 2, 0, '된장찌개 조리법', '2024-07-02 18:00:00'),
68+
('햄버거', 1, '00:30:00', '햄버거이미지.jpg', 3, 2, 0, '햄버거 조리법', '2024-07-02 19:00:00'),
69+
('흰쌀밥', 1, '00:40:00', '흰쌀밥이미지.jpg', 2, 4, 0, '흰쌀밥 조리법', '2024-07-02 20:00:00'),
70+
('샐러드', 1, '00:15:00', '샐러드이미지.jpg', 1, 5, 0, '샐러드 조리법', '2024-07-02 21:00:00'),
71+
('연어스테이크', 1, '00:45:00', '연어스테이크이미지.jpg', 4, 3, 0, '연어스테이크 조리법', '2024-07-02 21:00:01'),
72+
('초콜릿 케이크', 1, '01:20:00', '초콜릿케이크이미지.jpg', 5, 6, 0, '초콜릿 케이크 조리법', '2024-07-02 21:00:02'),
73+
('베지터블 스프', 1, '00:50:00', '베지터블스프이미지.jpg', 3, 2, 0, '베지터블 스프 조리법', '2024-07-02 21:00:03'),
74+
('카레라이스', 1, '00:30:00', '카레라이스이미지.jpg', 3, 2, 0, '카레라이스 조리법', '2024-07-02 21:00:04'),
75+
('새우볶음밥', 2, '00:25:00', '새우볶음밥이미지.jpg', 2, 3, 0, '새우볶음밥 조리법', '2024-07-02 21:00:05');
7676

7777
INSERT INTO category_recipe (category_id, recipe_id)
7878
VALUES (1, 2), -- 김밥은 한식

0 commit comments

Comments
 (0)