Skip to content

Commit

Permalink
♻️ relocate methods by responsibility
Browse files Browse the repository at this point in the history
  • Loading branch information
hyxrxn committed Sep 23, 2024
1 parent 63394e9 commit 21216d6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import jakarta.annotation.Nullable;
import jakarta.validation.constraints.Min;
import net.pengcook.recipe.exception.InvalidParameterException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

public record PageRecipeRequest(
@Min(0) int pageNumber,
Expand All @@ -10,4 +13,15 @@ public record PageRecipeRequest(
@Nullable String keyword,
@Nullable Long userId
) {

public PageRecipeRequest {
long offset = (long) pageNumber * pageSize;
if (offset > Integer.MAX_VALUE) {
throw new InvalidParameterException("μ μ ˆν•˜μ§€ μ•Šμ€ νŽ˜μ΄μ§€ μ •λ³΄μž…λ‹ˆλ‹€.");
}
}

public Pageable getPageable() {
return PageRequest.of(pageNumber, pageSize);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.pengcook.recipe.service;

import java.time.LocalTime;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
Expand All @@ -18,19 +17,17 @@
import net.pengcook.recipe.domain.Recipe;
import net.pengcook.recipe.dto.CategoryResponse;
import net.pengcook.recipe.dto.IngredientResponse;
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;
import net.pengcook.recipe.dto.RecipeHomeResponse;
import net.pengcook.recipe.dto.RecipeHomeWithMineResponse;
import net.pengcook.recipe.dto.RecipeRequest;
import net.pengcook.recipe.dto.RecipeResponse;
import net.pengcook.recipe.exception.InvalidParameterException;
import net.pengcook.recipe.exception.UnauthorizedException;
import net.pengcook.recipe.repository.RecipeRepository;
import net.pengcook.user.domain.User;
import net.pengcook.user.repository.UserRepository;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -53,7 +50,7 @@ public class RecipeService {
private final RecipeLikeService recipeLikeService;

public List<RecipeHomeWithMineResponse> readRecipes(UserInfo userInfo, PageRecipeRequest pageRecipeRequest) {
Pageable pageable = getValidatedPageable(pageRecipeRequest.pageNumber(), pageRecipeRequest.pageSize());
Pageable pageable = pageRecipeRequest.getPageable();
List<Long> recipeIds = recipeRepository.findRecipeIdsByCategoryAndKeyword(
pageable,
pageRecipeRequest.category(),
Expand Down Expand Up @@ -138,14 +135,6 @@ private List<CategoryResponse> getCategoryResponses(List<RecipeDataResponse> gro
.collect(Collectors.toList());
}

private Pageable getValidatedPageable(int pageNumber, int pageSize) {
long offset = (long) pageNumber * pageSize;
if (offset > Integer.MAX_VALUE) {
throw new InvalidParameterException("μ μ ˆν•˜μ§€ μ•Šμ€ νŽ˜μ΄μ§€ μ •λ³΄μž…λ‹ˆλ‹€.");
}
return PageRequest.of(pageNumber, pageSize);
}

private void verifyUserCanDeleteRecipe(UserInfo userInfo, Recipe recipe) {
if (recipe.getAuthor().getId() != userInfo.getId()) {
throw new UnauthorizedException("λ ˆμ‹œν”Όλ₯Ό μ‚­μ œν•  수 μ—†μŠ΅λ‹ˆλ‹€.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.pengcook.recipe.dto;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import net.pengcook.recipe.exception.InvalidParameterException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class PageRecipeRequestTest {

@Test
@DisplayName("μš”μ²­λ°›μ€ νŽ˜μ΄μ§€ offset 값이 int νƒ€μž…μ˜ μ΅œλŒ“κ°’μ„ μ΄ˆκ³Όν•˜λ©΄ μ˜ˆμ™Έκ°€ λ°œμƒν•œλ‹€.")
void readRecipesWhenPageOffsetIsGreaterThanIntMaxValue() {
int pageNumber = 1073741824;
int pageSize = 2;

assertThatThrownBy(() -> new PageRecipeRequest(pageNumber, pageSize, null, null, null))
.isInstanceOf(InvalidParameterException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.ingredient.domain.Requirement;
import net.pengcook.ingredient.dto.IngredientCreateRequest;
import net.pengcook.recipe.dto.RecipeHomeWithMineResponse;
import net.pengcook.recipe.dto.PageRecipeRequest;
import net.pengcook.recipe.dto.RecipeHomeWithMineResponse;
import net.pengcook.recipe.dto.RecipeRequest;
import net.pengcook.recipe.dto.RecipeResponse;
import net.pengcook.recipe.dto.RecipeStepRequest;
import net.pengcook.recipe.exception.InvalidParameterException;
import net.pengcook.recipe.exception.UnauthorizedException;
import net.pengcook.recipe.repository.RecipeRepository;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -41,7 +40,8 @@ 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<RecipeHomeWithMineResponse> recipeHomeWithMineResponses = recipeService.readRecipes(userInfo, pageRecipeRequest);
List<RecipeHomeWithMineResponse> recipeHomeWithMineResponses = recipeService.readRecipes(userInfo,
pageRecipeRequest);

assertThat(recipeHomeWithMineResponses.getFirst().recipeId()).isEqualTo(expectedFirstRecipeId);
}
Expand All @@ -51,26 +51,15 @@ void readRecipes(int pageNumber, int pageSize, int expectedFirstRecipeId) {
void readRecipesWithUserInfo() {
UserInfo userInfo = new UserInfo(1L, "[email protected]");
PageRecipeRequest pageRecipeRequest = new PageRecipeRequest(0, 2, null, null, null);
List<RecipeHomeWithMineResponse> recipeHomeWithMineResponses = recipeService.readRecipes(userInfo, pageRecipeRequest);
List<RecipeHomeWithMineResponse> recipeHomeWithMineResponses = recipeService.readRecipes(userInfo,
pageRecipeRequest);

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

@Test
@DisplayName("μš”μ²­λ°›μ€ νŽ˜μ΄μ§€ offset 값이 int νƒ€μž…μ˜ μ΅œλŒ“κ°’μ„ μ΄ˆκ³Όν•˜λ©΄ μ˜ˆμ™Έκ°€ λ°œμƒν•œλ‹€.")
void readRecipesWhenPageOffsetIsGreaterThanIntMaxValue() {
int pageNumber = 1073741824;
int pageSize = 2;
UserInfo userInfo = new UserInfo(1L, "[email protected]");
PageRecipeRequest pageRecipeRequest = new PageRecipeRequest(pageNumber, pageSize, null, null, null);

assertThatThrownBy(() -> recipeService.readRecipes(userInfo, pageRecipeRequest))
.isInstanceOf(InvalidParameterException.class);
}

@Test
@Sql({"/data/recipe.sql", "/data/like.sql"})
@DisplayName("λ‚΄κ°€ μ’‹μ•„ν•˜λŠ” λͺ¨λ“  κ²Œμ‹œκΈ€ κ°œμš”λ₯Ό μ‘°νšŒν•œλ‹€.")
Expand Down

0 comments on commit 21216d6

Please sign in to comment.