Skip to content

Commit

Permalink
โ™ป๏ธ remove unnecessary fields
Browse files Browse the repository at this point in the history
  • Loading branch information
hyxrxn committed Sep 20, 2024
1 parent 3b27fe3 commit 6d51f41
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import lombok.RequiredArgsConstructor;
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.authentication.resolver.LoginUser;
import net.pengcook.recipe.dto.MainRecipeResponse;
import net.pengcook.recipe.dto.PageRecipeRequest;
import net.pengcook.recipe.dto.RecipeDescriptionResponse;
import net.pengcook.recipe.dto.RecipeHomeWithMineResponse;
import net.pengcook.recipe.dto.RecipeRequest;
import net.pengcook.recipe.dto.RecipeResponse;
import net.pengcook.recipe.dto.RecipeStepResponse;
Expand All @@ -33,15 +33,15 @@ public class RecipeController {
private final RecipeStepService recipeStepService;

@GetMapping
public List<MainRecipeResponse> readRecipes(
public List<RecipeHomeWithMineResponse> readRecipes(
@LoginUser UserInfo userInfo,
@ModelAttribute @Valid PageRecipeRequest pageRecipeRequest
) {
return recipeService.readRecipes(userInfo, pageRecipeRequest);
}

@GetMapping("/likes")
public List<MainRecipeResponse> readLikeRecipes(@LoginUser UserInfo userInfo) {
public List<RecipeHomeWithMineResponse> readLikeRecipes(@LoginUser UserInfo userInfo) {
return recipeService.readLikeRecipes(userInfo);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.pengcook.recipe.dto;

import java.time.LocalDateTime;
import net.pengcook.user.domain.AuthorAble;

public record RecipeHomeResponse(
long recipeId,
String title,
long authorId,
String authorName,
String authorImage,
String thumbnail,
int likeCount,
int commentCount,
LocalDateTime createdAt
) implements AuthorAble {

@Override
public long getAuthorId() {
return authorId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.time.LocalDateTime;
import net.pengcook.authentication.domain.UserInfo;

public record MainRecipeResponse(
public record RecipeHomeWithMineResponse(
long recipeId,
String title,
AuthorResponse author,
Expand All @@ -14,9 +14,9 @@ public record MainRecipeResponse(
boolean mine
) {

public MainRecipeResponse(
public RecipeHomeWithMineResponse(
UserInfo userInfo,
RecipeDataResponse firstResponse
RecipeHomeResponse firstResponse
) {
this(
firstResponse.recipeId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import net.pengcook.recipe.domain.Recipe;
import net.pengcook.recipe.dto.RecipeDataResponse;
import net.pengcook.recipe.dto.RecipeHomeResponse;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -29,33 +30,21 @@ List<Long> findRecipeIdsByCategoryAndKeyword(
);

@Query("""
SELECT new net.pengcook.recipe.dto.RecipeDataResponse(
SELECT new net.pengcook.recipe.dto.RecipeHomeResponse(
r.id,
r.title,
r.author.id,
r.author.username,
r.author.image,
r.cookingTime,
r.thumbnail,
r.difficulty,
r.likeCount,
r.commentCount,
r.description,
r.createdAt,
c.id,
c.name,
i.id,
i.name,
ir.requirement
r.createdAt
)
FROM Recipe r
JOIN FETCH CategoryRecipe cr ON cr.recipe = r
JOIN FETCH Category c ON cr.category = c
JOIN FETCH IngredientRecipe ir ON ir.recipe = r
JOIN FETCH Ingredient i ON ir.ingredient = i
WHERE r.id IN :recipeIds
""")
List<RecipeDataResponse> findRecipeData(List<Long> recipeIds);
List<RecipeHomeResponse> findRecipeData(List<Long> recipeIds);

@Query("""
SELECT new net.pengcook.recipe.dto.RecipeDataResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import net.pengcook.recipe.domain.Recipe;
import net.pengcook.recipe.dto.CategoryResponse;
import net.pengcook.recipe.dto.IngredientResponse;
import net.pengcook.recipe.dto.MainRecipeResponse;
import net.pengcook.recipe.dto.RecipeHomeResponse;
import net.pengcook.recipe.dto.RecipeHomeWithMineResponse;
import net.pengcook.recipe.dto.PageRecipeRequest;
import net.pengcook.recipe.dto.RecipeDataResponse;
import net.pengcook.recipe.dto.RecipeDescriptionResponse;
Expand Down Expand Up @@ -51,7 +52,7 @@ public class RecipeService {
private final CommentService commentService;
private final RecipeLikeService recipeLikeService;

public List<MainRecipeResponse> readRecipes(UserInfo userInfo, PageRecipeRequest pageRecipeRequest) {
public List<RecipeHomeWithMineResponse> readRecipes(UserInfo userInfo, PageRecipeRequest pageRecipeRequest) {
Pageable pageable = getValidatedPageable(pageRecipeRequest.pageNumber(), pageRecipeRequest.pageSize());
List<Long> recipeIds = recipeRepository.findRecipeIdsByCategoryAndKeyword(
pageable,
Expand All @@ -60,14 +61,14 @@ public List<MainRecipeResponse> readRecipes(UserInfo userInfo, PageRecipeRequest
pageRecipeRequest.userId()
);

List<RecipeDataResponse> recipeDataResponses = recipeRepository.findRecipeData(recipeIds);
return convertToMainRecipeResponses(userInfo, recipeDataResponses);
List<RecipeHomeResponse> recipeHomeResponses = recipeRepository.findRecipeData(recipeIds);
return convertToRecipeHomeResponses(userInfo, recipeHomeResponses);
}

public List<MainRecipeResponse> readLikeRecipes(UserInfo userInfo) {
public List<RecipeHomeWithMineResponse> readLikeRecipes(UserInfo userInfo) {
List<Long> likeRecipeIds = likeRepository.findRecipeIdsByUserId(userInfo.getId());
List<RecipeDataResponse> recipeDataResponses = recipeRepository.findRecipeData(likeRecipeIds);
return convertToMainRecipeResponses(userInfo, recipeDataResponses);
List<RecipeHomeResponse> recipeHomeResponses = recipeRepository.findRecipeData(likeRecipeIds);
return convertToRecipeHomeResponses(userInfo, recipeHomeResponses);
}

public RecipeResponse createRecipe(UserInfo userInfo, RecipeRequest recipeRequest) {
Expand Down Expand Up @@ -115,24 +116,24 @@ public void deleteRecipe(UserInfo userInfo, long recipeId) {
});
}

private List<MainRecipeResponse> convertToMainRecipeResponses(
private List<RecipeHomeWithMineResponse> convertToRecipeHomeResponses(
UserInfo userInfo,
List<RecipeDataResponse> recipeDataResponses
List<RecipeHomeResponse> recipeHomeResponses
) {
Collection<List<RecipeDataResponse>> groupedRecipeData = recipeDataResponses.stream()
.collect(Collectors.groupingBy(RecipeDataResponse::recipeId))
Collection<List<RecipeHomeResponse>> groupedRecipeData = recipeHomeResponses.stream()
.collect(Collectors.groupingBy(RecipeHomeResponse::recipeId))
.values();

return groupedRecipeData.stream()
.map(data -> getMainRecipeResponse(userInfo, data))
.sorted(Comparator.comparing(MainRecipeResponse::recipeId).reversed())
.sorted(Comparator.comparing(RecipeHomeWithMineResponse::recipeId).reversed())
.collect(Collectors.toList());
}

private MainRecipeResponse getMainRecipeResponse(UserInfo userInfo, List<RecipeDataResponse> groupedResponses) {
RecipeDataResponse firstResponse = groupedResponses.getFirst();
private RecipeHomeWithMineResponse getMainRecipeResponse(UserInfo userInfo, List<RecipeHomeResponse> groupedResponses) {
RecipeHomeResponse firstResponse = groupedResponses.getFirst();

return new MainRecipeResponse(
return new RecipeHomeWithMineResponse(
userInfo,
firstResponse
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package net.pengcook.recipe.repository;

import static net.pengcook.ingredient.domain.Requirement.REQUIRED;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import net.pengcook.recipe.dto.RecipeDataResponse;
import net.pengcook.recipe.dto.RecipeHomeResponse;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -37,15 +35,13 @@ void findRecipeIdsByCategoryAndKeyword() {
@DisplayName("๋ ˆ์‹œํ”ผ id์— ํ•ด๋‹น๋˜๋Š” ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.")
void findRecipeData() {
List<Long> recipeIds = List.of(4L, 3L);
RecipeDataResponse expectedData = new RecipeDataResponse(4, "ํ† ๋งˆํ† ์ŠคํŒŒ๊ฒŒํ‹ฐ", 1, "loki", "loki.jpg",
LocalTime.of(0, 30), "ํ† ๋งˆํ† ์ŠคํŒŒ๊ฒŒํ‹ฐ์ด๋ฏธ์ง€.jpg", 3, 2, 0, "ํ† ๋งˆํ† ์ŠคํŒŒ๊ฒŒํ‹ฐ ์กฐ๋ฆฌ๋ฒ•", LocalDateTime.of(2024, 7, 2, 13, 0, 0),
2, "์–‘์‹", 2, "์Œ€", REQUIRED
);
RecipeHomeResponse expectedData = new RecipeHomeResponse(4, "ํ† ๋งˆํ† ์ŠคํŒŒ๊ฒŒํ‹ฐ", 1, "loki", "loki.jpg",
"ํ† ๋งˆํ† ์ŠคํŒŒ๊ฒŒํ‹ฐ์ด๋ฏธ์ง€.jpg", 2, 0, LocalDateTime.of(2024, 7, 2, 13, 0, 0));

List<RecipeDataResponse> recipeData = repository.findRecipeData(recipeIds);
List<RecipeHomeResponse> recipeData = repository.findRecipeData(recipeIds);

assertAll(
() -> assertThat(recipeData).hasSize(6 + 3),
() -> assertThat(recipeData).hasSize(2),
() -> assertThat(recipeData).contains(expectedData)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.ingredient.domain.Requirement;
import net.pengcook.ingredient.dto.IngredientCreateRequest;
import net.pengcook.recipe.dto.MainRecipeResponse;
import net.pengcook.recipe.dto.RecipeHomeWithMineResponse;
import net.pengcook.recipe.dto.PageRecipeRequest;
import net.pengcook.recipe.dto.RecipeRequest;
import net.pengcook.recipe.dto.RecipeResponse;
Expand Down Expand Up @@ -41,21 +41,21 @@ class RecipeServiceTest {
void readRecipes(int pageNumber, int pageSize, int expectedFirstRecipeId) {
UserInfo userInfo = new UserInfo(1L, "[email protected]");
PageRecipeRequest pageRecipeRequest = new PageRecipeRequest(pageNumber, pageSize, null, null, null);
List<MainRecipeResponse> mainRecipeResponses = recipeService.readRecipes(userInfo, pageRecipeRequest);
List<RecipeHomeWithMineResponse> recipeHomeWithMineResponses = recipeService.readRecipes(userInfo, pageRecipeRequest);

assertThat(mainRecipeResponses.getFirst().recipeId()).isEqualTo(expectedFirstRecipeId);
assertThat(recipeHomeWithMineResponses.getFirst().recipeId()).isEqualTo(expectedFirstRecipeId);
}

@Test
@DisplayName("๋ ˆ์‹œํ”ผ ๊ฐœ์š” ์กฐํšŒ ์‹œ ์กฐํšŒ์ž๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ•จ๊ป˜ ์‘๋‹ตํ•œ๋‹ค.")
void readRecipesWithUserInfo() {
UserInfo userInfo = new UserInfo(1L, "[email protected]");
PageRecipeRequest pageRecipeRequest = new PageRecipeRequest(0, 2, null, null, null);
List<MainRecipeResponse> mainRecipeResponses = recipeService.readRecipes(userInfo, pageRecipeRequest);
List<RecipeHomeWithMineResponse> recipeHomeWithMineResponses = recipeService.readRecipes(userInfo, pageRecipeRequest);

assertAll(
() -> assertThat(mainRecipeResponses.getFirst().mine()).isFalse(),
() -> assertThat(mainRecipeResponses.getLast().mine()).isTrue()
() -> assertThat(recipeHomeWithMineResponses.getFirst().mine()).isFalse(),
() -> assertThat(recipeHomeWithMineResponses.getLast().mine()).isTrue()
);
}

Expand All @@ -77,7 +77,7 @@ void readRecipesWhenPageOffsetIsGreaterThanIntMaxValue() {
void readLikeRecipes() {
UserInfo userInfo = new UserInfo(1L, "[email protected]");

List<MainRecipeResponse> actual = recipeService.readLikeRecipes(userInfo);
List<RecipeHomeWithMineResponse> actual = recipeService.readLikeRecipes(userInfo);

assertAll(
() -> assertThat(actual.size()).isOne(),
Expand Down

0 comments on commit 6d51f41

Please sign in to comment.