From 69071485bc07ec1e143e2b1bb8de0bc9f08a6fed Mon Sep 17 00:00:00 2001 From: hyxrxn Date: Tue, 23 Jul 2024 18:58:50 +0900 Subject: [PATCH 1/9] :sparkles: add constructor to domain --- .../main/java/net/pengcook/category/domain/Category.java | 8 ++++++++ .../java/net/pengcook/category/domain/CategoryRecipe.java | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/backend/src/main/java/net/pengcook/category/domain/Category.java b/backend/src/main/java/net/pengcook/category/domain/Category.java index 0593be75..ff0ae106 100644 --- a/backend/src/main/java/net/pengcook/category/domain/Category.java +++ b/backend/src/main/java/net/pengcook/category/domain/Category.java @@ -4,8 +4,12 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; @Entity +@NoArgsConstructor +@AllArgsConstructor public class Category { @Id @@ -13,4 +17,8 @@ public class Category { private long id; private String name; + + public Category(String name) { + this(0, name); + } } diff --git a/backend/src/main/java/net/pengcook/category/domain/CategoryRecipe.java b/backend/src/main/java/net/pengcook/category/domain/CategoryRecipe.java index 65f49805..2f763389 100644 --- a/backend/src/main/java/net/pengcook/category/domain/CategoryRecipe.java +++ b/backend/src/main/java/net/pengcook/category/domain/CategoryRecipe.java @@ -6,9 +6,13 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; import net.pengcook.recipe.domain.Recipe; @Entity +@NoArgsConstructor +@AllArgsConstructor public class CategoryRecipe { @Id @@ -22,4 +26,8 @@ public class CategoryRecipe { @ManyToOne @JoinColumn(name = "recipe_id") private Recipe recipe; + + public CategoryRecipe(Category category, Recipe recipe) { + this(0, category, recipe); + } } From 09f5dae20f4dda875bdf4ee4ad3192aa30026188 Mon Sep 17 00:00:00 2001 From: hyxrxn Date: Tue, 23 Jul 2024 19:00:27 +0900 Subject: [PATCH 2/9] :sparkles: add category recipe jpa repository --- .../category/repository/CategoryRecipeRepository.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 backend/src/main/java/net/pengcook/category/repository/CategoryRecipeRepository.java diff --git a/backend/src/main/java/net/pengcook/category/repository/CategoryRecipeRepository.java b/backend/src/main/java/net/pengcook/category/repository/CategoryRecipeRepository.java new file mode 100644 index 00000000..2c2b2044 --- /dev/null +++ b/backend/src/main/java/net/pengcook/category/repository/CategoryRecipeRepository.java @@ -0,0 +1,9 @@ +package net.pengcook.category.repository; + +import net.pengcook.category.domain.CategoryRecipe; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CategoryRecipeRepository extends JpaRepository { +} From 52d7269c8d2596bd2d1d1e701361da552552f5e7 Mon Sep 17 00:00:00 2001 From: hyxrxn Date: Tue, 23 Jul 2024 19:01:08 +0900 Subject: [PATCH 3/9] :sparkles: add category jpa repository - add method to find category by name --- .../category/repository/CategoryRepository.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 backend/src/main/java/net/pengcook/category/repository/CategoryRepository.java diff --git a/backend/src/main/java/net/pengcook/category/repository/CategoryRepository.java b/backend/src/main/java/net/pengcook/category/repository/CategoryRepository.java new file mode 100644 index 00000000..14aa0183 --- /dev/null +++ b/backend/src/main/java/net/pengcook/category/repository/CategoryRepository.java @@ -0,0 +1,12 @@ +package net.pengcook.category.repository; + +import java.util.Optional; +import net.pengcook.category.domain.Category; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CategoryRepository extends JpaRepository { + + Optional findByName(String name); +} From 3791180c0f5de315e168a0a19321bcf860a5b7f9 Mon Sep 17 00:00:00 2001 From: hyxrxn Date: Tue, 23 Jul 2024 19:01:46 +0900 Subject: [PATCH 4/9] :sparkles: save categories of recipe --- .../category/service/CategoryService.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 backend/src/main/java/net/pengcook/category/service/CategoryService.java diff --git a/backend/src/main/java/net/pengcook/category/service/CategoryService.java b/backend/src/main/java/net/pengcook/category/service/CategoryService.java new file mode 100644 index 00000000..01e9f994 --- /dev/null +++ b/backend/src/main/java/net/pengcook/category/service/CategoryService.java @@ -0,0 +1,29 @@ +package net.pengcook.category.service; + +import java.util.List; +import lombok.RequiredArgsConstructor; +import net.pengcook.category.domain.Category; +import net.pengcook.category.domain.CategoryRecipe; +import net.pengcook.category.repository.CategoryRecipeRepository; +import net.pengcook.category.repository.CategoryRepository; +import net.pengcook.recipe.domain.Recipe; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class CategoryService { + + private final CategoryRepository categoryRepository; + private final CategoryRecipeRepository categoryRecipeRepository; + + public void saveCategories(Recipe recipe, List categories) { + categories.forEach(category -> saveCategoryRecipe(recipe, category)); + } + + private void saveCategoryRecipe(Recipe recipe, String name) { + Category category = categoryRepository.findByName(name) + .orElseGet(() -> categoryRepository.save(new Category(name))); + + categoryRecipeRepository.save(new CategoryRecipe(category, recipe)); + } +} From 28e1afde2a399df35c23aff7c5de039dc29a804b Mon Sep 17 00:00:00 2001 From: hyxrxn Date: Tue, 23 Jul 2024 19:02:07 +0900 Subject: [PATCH 5/9] :white_check_mark: save categories of recipe --- .../category/service/CategoryServiceTest.java | 50 +++++++++++++++++++ backend/src/test/resources/data/category.sql | 34 +++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 backend/src/test/java/net/pengcook/category/service/CategoryServiceTest.java create mode 100644 backend/src/test/resources/data/category.sql diff --git a/backend/src/test/java/net/pengcook/category/service/CategoryServiceTest.java b/backend/src/test/java/net/pengcook/category/service/CategoryServiceTest.java new file mode 100644 index 00000000..ea9d56b5 --- /dev/null +++ b/backend/src/test/java/net/pengcook/category/service/CategoryServiceTest.java @@ -0,0 +1,50 @@ +package net.pengcook.category.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +import java.time.LocalDate; +import java.time.LocalTime; +import java.util.List; +import net.pengcook.category.repository.CategoryRecipeRepository; +import net.pengcook.category.repository.CategoryRepository; +import net.pengcook.recipe.domain.Recipe; +import net.pengcook.user.domain.User; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.jdbc.Sql; + +@DataJpaTest +@Import(CategoryService.class) +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +@Sql(scripts = "/data/category.sql") +class CategoryServiceTest { + + private final long INITIAL_CATEGORY_COUNT = 5; + private final long INITIAL_CATEGORY_RECIPE_COUNT = 0; + + @Autowired + private CategoryService categoryService; + @Autowired + private CategoryRepository categoryRepository; + @Autowired + private CategoryRecipeRepository categoryRecipeRepository; + + @Test + @DisplayName("레시피의 카테고리를 저장할 수 있다.") + void saveCategories() { + User author = new User("ela@pengcook.net", "ela", "엘라", "ela.jpg", LocalDate.of(2024, 7, 22), "KOREA"); + Recipe recipe = new Recipe(1L, "김치볶음밥", author, LocalTime.of(0, 30, 0), "김치볶음밥이미지.jpg", 3, 2, "김치볶음밥 조리법"); + + categoryService.saveCategories(recipe, List.of("한식", "매운음식")); + + assertAll( + () -> assertThat(categoryRepository.count()).isEqualTo(INITIAL_CATEGORY_COUNT + 1), + () -> assertThat(categoryRecipeRepository.count()).isEqualTo(INITIAL_CATEGORY_RECIPE_COUNT + 2) + ); + } +} diff --git a/backend/src/test/resources/data/category.sql b/backend/src/test/resources/data/category.sql new file mode 100644 index 00000000..beb15140 --- /dev/null +++ b/backend/src/test/resources/data/category.sql @@ -0,0 +1,34 @@ +SET REFERENTIAL_INTEGRITY FALSE; + +TRUNCATE TABLE users; +ALTER TABLE users ALTER COLUMN id RESTART; + +TRUNCATE TABLE category; +ALTER TABLE category ALTER COLUMN id RESTART; + +TRUNCATE TABLE ingredient; +ALTER TABLE ingredient ALTER COLUMN id RESTART; + +TRUNCATE TABLE recipe; +ALTER TABLE recipe ALTER COLUMN id RESTART; + +TRUNCATE TABLE category_recipe; +ALTER TABLE category_recipe ALTER COLUMN id RESTART; + +TRUNCATE TABLE ingredient_recipe; +ALTER TABLE ingredient_recipe ALTER COLUMN id RESTART; + +SET REFERENTIAL_INTEGRITY TRUE; + +INSERT INTO users (email, username, nickname, image, birth, region) +VALUES ('ela@pengcook.net', 'ela', '엘라', 'ela.jpg', '2024-07-22', 'KOREA'); + +INSERT INTO category (name) +VALUES ('한식'), + ('양식'), + ('채식'), + ('건강식'), + ('간편식'); + +INSERT INTO recipe (title, author_id, cooking_time, thumbnail, difficulty, like_count, description) +VALUES ('김치볶음밥', 1, '00:30:00', '김치볶음밥이미지.jpg', 3, 2, '김치볶음밥 조리법'); From 18fc44b28e5681bed9b5c408a82577bc966f859c Mon Sep 17 00:00:00 2001 From: hyxrxn Date: Wed, 24 Jul 2024 10:20:58 +0900 Subject: [PATCH 6/9] :recycle: specify id as long in secondary constructor --- .../src/main/java/net/pengcook/category/domain/Category.java | 2 +- .../main/java/net/pengcook/category/domain/CategoryRecipe.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/net/pengcook/category/domain/Category.java b/backend/src/main/java/net/pengcook/category/domain/Category.java index ff0ae106..203fff36 100644 --- a/backend/src/main/java/net/pengcook/category/domain/Category.java +++ b/backend/src/main/java/net/pengcook/category/domain/Category.java @@ -19,6 +19,6 @@ public class Category { private String name; public Category(String name) { - this(0, name); + this(0L, name); } } diff --git a/backend/src/main/java/net/pengcook/category/domain/CategoryRecipe.java b/backend/src/main/java/net/pengcook/category/domain/CategoryRecipe.java index 2f763389..b1ca74a5 100644 --- a/backend/src/main/java/net/pengcook/category/domain/CategoryRecipe.java +++ b/backend/src/main/java/net/pengcook/category/domain/CategoryRecipe.java @@ -28,6 +28,6 @@ public class CategoryRecipe { private Recipe recipe; public CategoryRecipe(Category category, Recipe recipe) { - this(0, category, recipe); + this(0L, category, recipe); } } From 7cb01fdcb9e20f4fba9c4d58b0b53a8586b1ca6f Mon Sep 17 00:00:00 2001 From: hyxrxn Date: Wed, 24 Jul 2024 11:11:25 +0900 Subject: [PATCH 7/9] :recycle: add access modifier to Lombok constructor --- .../src/main/java/net/pengcook/category/domain/Category.java | 5 +++-- .../java/net/pengcook/category/domain/CategoryRecipe.java | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/net/pengcook/category/domain/Category.java b/backend/src/main/java/net/pengcook/category/domain/Category.java index 203fff36..e9752e35 100644 --- a/backend/src/main/java/net/pengcook/category/domain/Category.java +++ b/backend/src/main/java/net/pengcook/category/domain/Category.java @@ -4,12 +4,13 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; @Entity -@NoArgsConstructor -@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor(access = AccessLevel.PRIVATE) public class Category { @Id diff --git a/backend/src/main/java/net/pengcook/category/domain/CategoryRecipe.java b/backend/src/main/java/net/pengcook/category/domain/CategoryRecipe.java index b1ca74a5..dfe914b9 100644 --- a/backend/src/main/java/net/pengcook/category/domain/CategoryRecipe.java +++ b/backend/src/main/java/net/pengcook/category/domain/CategoryRecipe.java @@ -6,13 +6,14 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; +import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; import net.pengcook.recipe.domain.Recipe; @Entity -@NoArgsConstructor -@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor(access = AccessLevel.PRIVATE) public class CategoryRecipe { @Id From 7bab457078c9639062c4b97179fe56bd655c6403 Mon Sep 17 00:00:00 2001 From: hyxrxn Date: Wed, 24 Jul 2024 11:19:38 +0900 Subject: [PATCH 8/9] :recycle: remove unnecessary annotation --- .../java/net/pengcook/category/service/CategoryServiceTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/src/test/java/net/pengcook/category/service/CategoryServiceTest.java b/backend/src/test/java/net/pengcook/category/service/CategoryServiceTest.java index ea9d56b5..116db5ca 100644 --- a/backend/src/test/java/net/pengcook/category/service/CategoryServiceTest.java +++ b/backend/src/test/java/net/pengcook/category/service/CategoryServiceTest.java @@ -20,7 +20,6 @@ @DataJpaTest @Import(CategoryService.class) -@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) @Sql(scripts = "/data/category.sql") class CategoryServiceTest { From 7db971a49ac47ea3664bfd8cd33a6190b12e07c5 Mon Sep 17 00:00:00 2001 From: hyxrxn Date: Wed, 24 Jul 2024 11:31:52 +0900 Subject: [PATCH 9/9] :recycle: remove unnecessary annotation --- .../pengcook/category/repository/CategoryRecipeRepository.java | 2 -- .../net/pengcook/category/repository/CategoryRepository.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/backend/src/main/java/net/pengcook/category/repository/CategoryRecipeRepository.java b/backend/src/main/java/net/pengcook/category/repository/CategoryRecipeRepository.java index 2c2b2044..e13df319 100644 --- a/backend/src/main/java/net/pengcook/category/repository/CategoryRecipeRepository.java +++ b/backend/src/main/java/net/pengcook/category/repository/CategoryRecipeRepository.java @@ -2,8 +2,6 @@ import net.pengcook.category.domain.CategoryRecipe; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; -@Repository public interface CategoryRecipeRepository extends JpaRepository { } diff --git a/backend/src/main/java/net/pengcook/category/repository/CategoryRepository.java b/backend/src/main/java/net/pengcook/category/repository/CategoryRepository.java index 14aa0183..110ac866 100644 --- a/backend/src/main/java/net/pengcook/category/repository/CategoryRepository.java +++ b/backend/src/main/java/net/pengcook/category/repository/CategoryRepository.java @@ -3,9 +3,7 @@ import java.util.Optional; import net.pengcook.category.domain.Category; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; -@Repository public interface CategoryRepository extends JpaRepository { Optional findByName(String name);