Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/be/dev' into be/feat/430
Browse files Browse the repository at this point in the history
  • Loading branch information
tackyu committed Jan 6, 2025
2 parents 8d4edb0 + 54f4e8b commit 00195da
Show file tree
Hide file tree
Showing 23 changed files with 155 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class Category {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import net.pengcook.recipe.domain.Recipe;

Expand All @@ -22,6 +23,7 @@ public class CategoryRecipe {

@ManyToOne
@JoinColumn(name = "category_id")
@Getter
private Category category;

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

import net.pengcook.category.domain.Category;

public record CategoryResponse(long categoryId, String categoryName) {

public CategoryResponse(Category category) {
this(category.getId(), category.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public interface CategoryRecipeRepository extends JpaRepository<CategoryRecipe,
List<Long> findRecipeIdsByCategoryName(String categoryName, Pageable pageable);

void deleteByRecipe(Recipe recipe);

List<CategoryRecipe> findAllByRecipeId(Long recipeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.pengcook.category.repository.CategoryRecipeRepository;
import net.pengcook.category.repository.CategoryRepository;
import net.pengcook.recipe.domain.Recipe;
import net.pengcook.category.dto.CategoryResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -27,6 +28,15 @@ public void deleteCategoryRecipe(Recipe recipe) {
categoryRecipeRepository.deleteByRecipe(recipe);
}

@Transactional(readOnly = true)
public List<CategoryResponse> findCategoryByRecipe(Recipe recipe) {
List<CategoryRecipe> categoryRecipes = categoryRecipeRepository.findAllByRecipeId(recipe.getId());
return categoryRecipes.stream()
.map(CategoryRecipe::getCategory)
.map(CategoryResponse::new)
.toList();
}

private void saveCategoryRecipe(Recipe recipe, String name) {
Category category = categoryRepository.findByName(name)
.orElseGet(() -> categoryRepository.save(new Category(name)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import net.pengcook.recipe.domain.Recipe;
import net.pengcook.user.domain.AuthorAble;
import net.pengcook.user.domain.Ownable;
import net.pengcook.user.domain.User;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Comment implements AuthorAble {
public class Comment implements Ownable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -42,7 +42,7 @@ public Comment(User user, Recipe recipe, String message, LocalDateTime createdAt
}

@Override
public long getAuthorId() {
public long getOwnerId() {
return user.getId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.pengcook.ingredient.dto;

import net.pengcook.ingredient.domain.IngredientRecipe;
import net.pengcook.ingredient.domain.Requirement;

public record IngredientResponse(long ingredientId, String ingredientName, Requirement requirement) {

public IngredientResponse(IngredientRecipe ingredientRecipe) {
this(
ingredientRecipe.getIngredient().getId(),
ingredientRecipe.getIngredient().getName(),
ingredientRecipe.getRequirement()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import net.pengcook.ingredient.domain.IngredientRecipe;
import net.pengcook.ingredient.domain.Requirement;
import net.pengcook.ingredient.dto.IngredientCreateRequest;
import net.pengcook.ingredient.dto.IngredientResponse;
import net.pengcook.ingredient.exception.InvalidNameException;
import net.pengcook.ingredient.repository.IngredientRecipeRepository;
import net.pengcook.ingredient.repository.IngredientRepository;
import net.pengcook.recipe.domain.Recipe;
import org.springframework.stereotype.Service;
Expand All @@ -21,6 +23,7 @@
public class IngredientService {

private final IngredientRepository ingredientRepository;
private final IngredientRecipeRepository ingredientRecipeRepository;
private final IngredientRecipeService ingredientRecipeService;
private final IngredientSubstitutionService ingredientSubstitutionService;

Expand All @@ -32,6 +35,14 @@ public void register(List<IngredientCreateRequest> requests, Recipe recipe) {
}
}

@Transactional(readOnly = true)
public List<IngredientResponse> findIngredientByRecipe(Recipe recipe) {
return ingredientRecipeRepository.findAllByRecipeId(recipe.getId())
.stream()
.map(IngredientResponse::new)
.toList();
}

private void registerOne(IngredientCreateRequest request, Recipe recipe) {
Ingredient ingredient = registerOrGetIngredient(request.name());
IngredientRecipe ingredientRecipe = registerIngredientRecipe(recipe, request, ingredient);
Expand Down Expand Up @@ -87,5 +98,4 @@ private boolean hasDuplicateName(List<String> names) {
HashSet<String> nonDuplicate = new HashSet<>(names);
return (names.size() != nonDuplicate.size());
}

}
8 changes: 7 additions & 1 deletion backend/src/main/java/net/pengcook/recipe/domain/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import net.pengcook.user.domain.Ownable;
import net.pengcook.user.domain.User;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Getter
public class Recipe {
public class Recipe implements Ownable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down Expand Up @@ -97,6 +98,11 @@ public void decreaseCommentCount() {
commentCount--;
}

@Override
public long getOwnerId() {
return author.getId();
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
package net.pengcook.recipe.dto;

import net.pengcook.user.domain.User;

public record AuthorResponse(long authorId, String authorName, String authorImage) {

public AuthorResponse(User author) {
this(author.getId(), author.getUsername(), author.getImage());
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.time.LocalTime;
import java.util.List;
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.category.dto.CategoryResponse;
import net.pengcook.ingredient.dto.IngredientResponse;
import net.pengcook.recipe.domain.Recipe;

public record RecipeDescriptionResponse(
long recipeId,
Expand All @@ -24,25 +27,25 @@ public record RecipeDescriptionResponse(

public RecipeDescriptionResponse(
UserInfo userInfo,
RecipeDataResponse firstResponse,
Recipe recipe,
List<CategoryResponse> category,
List<IngredientResponse> ingredient,
boolean isLike
) {
this(
firstResponse.recipeId(),
firstResponse.title(),
new AuthorResponse(firstResponse.authorId(), firstResponse.authorName(), firstResponse.authorImage()),
firstResponse.cookingTime(),
firstResponse.thumbnail(),
firstResponse.difficulty(),
firstResponse.likeCount(),
firstResponse.commentCount(),
firstResponse.description(),
firstResponse.createdAt(),
recipe.getId(),
recipe.getTitle(),
new AuthorResponse(recipe.getAuthor()),
recipe.getCookingTime(),
recipe.getThumbnail(),
recipe.getDifficulty(),
recipe.getLikeCount(),
recipe.getCommentCount(),
recipe.getDescription(),
recipe.getCreatedAt(),
category,
ingredient,
userInfo.isSameUser(firstResponse.authorId()),
userInfo.isSameUser(recipe.getAuthor().getId()),
isLike
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.pengcook.recipe.dto;

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

public record RecipeHomeResponse(
long recipeId,
Expand All @@ -13,10 +13,10 @@ public record RecipeHomeResponse(
int likeCount,
int commentCount,
LocalDateTime createdAt
) implements AuthorAble {
) implements Ownable {

@Override
public long getAuthorId() {
public long getOwnerId() {
return authorId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.time.LocalTime;
import java.util.List;
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.category.dto.CategoryResponse;
import net.pengcook.ingredient.dto.IngredientResponse;
import net.pengcook.recipe.domain.Recipe;

public record RecipeHomeWithMineResponse(
long recipeId,
Expand All @@ -23,25 +26,25 @@ public record RecipeHomeWithMineResponse(

public RecipeHomeWithMineResponse(
UserInfo userInfo,
RecipeDataResponse firstResponse,
Recipe recipe,
List<CategoryResponse> category,
List<IngredientResponse> ingredient

) {
this(
firstResponse.recipeId(),
firstResponse.title(),
new AuthorResponse(firstResponse.authorId(), firstResponse.authorName(), firstResponse.authorImage()),
firstResponse.cookingTime(),
firstResponse.thumbnail(),
firstResponse.difficulty(),
firstResponse.likeCount(),
firstResponse.commentCount(),
firstResponse.description(),
firstResponse.createdAt(),
recipe.getId(),
recipe.getTitle(),
new AuthorResponse(recipe.getAuthor()),
recipe.getCookingTime(),
recipe.getThumbnail(),
recipe.getDifficulty(),
recipe.getLikeCount(),
recipe.getCommentCount(),
recipe.getDescription(),
recipe.getCreatedAt(),
category,
ingredient,
userInfo.isSameUser(firstResponse.authorId())
userInfo.isSameUser(recipe.getAuthor().getId())
);
}
}
Loading

0 comments on commit 00195da

Please sign in to comment.