Skip to content

Commit

Permalink
Merge pull request #95 from OurMenu/fix/article
Browse files Browse the repository at this point in the history
Fix/article
  • Loading branch information
david-parkk authored Aug 15, 2024
2 parents 32f3759 + 541755d commit 4798f4e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
* 게시글을 저장한다.
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -75,7 +79,7 @@ public void visit() {
}

public void deleteAllArticleMenus() {
this.articleMenuList = new ArrayList<>();
articleMenuList.clear();
menuCount = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 4798f4e

Please sign in to comment.