diff --git a/src/main/java/com/ourMenu/backend/domain/article/application/ArticleService.java b/src/main/java/com/ourMenu/backend/domain/article/application/ArticleService.java index 6e339181..64c3c642 100644 --- a/src/main/java/com/ourMenu/backend/domain/article/application/ArticleService.java +++ b/src/main/java/com/ourMenu/backend/domain/article/application/ArticleService.java @@ -13,6 +13,7 @@ import com.ourMenu.backend.domain.user.application.UserService; import com.ourMenu.backend.domain.user.domain.User; import com.ourMenu.backend.global.common.Status; +import jakarta.persistence.EntityManager; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -30,8 +31,8 @@ public class ArticleService { private final ArticleRepository articleRepository; private final ArticleMenuRepository articleMenuRepository; private final UserService userService; - private final MenuService menuService; private final ArticleMenuService articleMenuService; + private final EntityManager em; /** * 게시글을 저장한다. @@ -113,23 +114,33 @@ public ArticleMenu findArticleMenu(Long id) { public Article saveArticleWithMenu(Article article, Long userId) { User user = userService.getUserById(userId).get(); article.confirmUser(user); - Article saveArticle = save(article); for (ArticleMenu articleMenu : article.getArticleMenuList()) { + articleMenu.confirmArticle(article); articleMenuService.save(articleMenu); } - return saveArticle; + return save(article); } @Transactional public Article updateArticleWithMenu(Long articleId, Article article, Long userId) { - User user = userService.getUserById(userId).get(); Article findArticle = findOne(articleId); + User user = findArticle.getUser(); + if (!user.getId().equals(userId)) { + throw new RuntimeException("권한이 없습니다"); + } + + findArticle.getArticleMenuList().forEach(articleMenu -> { + articleMenu.deleteArticle(); + articleMenuRepository.delete(articleMenu); + }); + findArticle.deleteAllArticleMenus(); + for (ArticleMenu articleMenu : article.getArticleMenuList()) { - ArticleMenu saveArticleMenu = articleMenuService.save(articleMenu); - findArticle.addArticleMenu(saveArticleMenu); - saveArticleMenu.confirmArticle(findArticle); + findArticle.addArticleMenu(articleMenu); + articleMenu.confirmArticle(findArticle); + articleMenuService.save(articleMenu); } findArticle.update(article); return findArticle; diff --git a/src/main/java/com/ourMenu/backend/domain/article/domain/Article.java b/src/main/java/com/ourMenu/backend/domain/article/domain/Article.java index bec0fd59..7578b635 100644 --- a/src/main/java/com/ourMenu/backend/domain/article/domain/Article.java +++ b/src/main/java/com/ourMenu/backend/domain/article/domain/Article.java @@ -56,9 +56,13 @@ public void confirmUser(User user) { public void addArticleMenu(ArticleMenu articleMenu) { this.articleMenuList.add(articleMenu); - menuCount++; } + public void upMenuCount(){ + this.menuCount++; + } + + public void setStatus(Status status) { this.status = status; } @@ -75,7 +79,7 @@ public void visit() { } public void deleteAllArticleMenus() { - this.articleMenuList = new ArrayList<>(); + articleMenuList.clear(); menuCount = 0; } } diff --git a/src/main/java/com/ourMenu/backend/domain/article/domain/ArticleMenu.java b/src/main/java/com/ourMenu/backend/domain/article/domain/ArticleMenu.java index 2f8bca1b..d88b8289 100644 --- a/src/main/java/com/ourMenu/backend/domain/article/domain/ArticleMenu.java +++ b/src/main/java/com/ourMenu/backend/domain/article/domain/ArticleMenu.java @@ -37,6 +37,10 @@ public class ArticleMenu { public void confirmArticle(Article article){ this.article = article; - article.addArticleMenu(this); + article.upMenuCount(); + } + + public void deleteArticle(){ + this.article = null; } }