Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: PUT/article #120

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
Expand Down Expand Up @@ -50,7 +49,7 @@ public ApiResponse<ArticleResponse> getArticle(@PathVariable Long articleId) {
@PutMapping("/article/{articleId}")
public ApiResponse<ArticleResponse> putArticle(@PathVariable Long articleId, @RequestBody PutArticleRequest putArticleRequest, @UserId Long userId) {
Article article = PutArticleRequest.toEntity(putArticleRequest);
Article saveArticle = articleService.updateArticleWithMenu(articleId, article, userId);
Article saveArticle = articleService.updateArticleWithMenu(articleId, article, userId, putArticleRequest.getGroupIds());
if (saveArticle.getUser().getImgUrl() == null) {
return ApiUtils.success(ArticleResponse.toDto(saveArticle));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ public class PutArticleRequest {

private String articleContent;

private List<ArticleMenuRequest> articleMenus;
private List<Long> groupIds;

public static Article toEntity(PutArticleRequest putArticleRequest){
List<ArticleMenu> articleMenuList = putArticleRequest.getArticleMenus().stream().map(ArticleMenuRequest::toEntity).toList();
return Article.builder()
.title(putArticleRequest.articleTitle)
.content(putArticleRequest.articleContent)
.articleMenuList(articleMenuList)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@
import com.ourMenu.backend.domain.menu.domain.Menu;
import com.ourMenu.backend.domain.menu.dto.request.PostMenuRequest;
import com.ourMenu.backend.domain.menu.dto.request.StoreRequestDTO;
import com.ourMenu.backend.domain.menu.dto.response.MenuDetailDto;
import com.ourMenu.backend.domain.menu.dto.response.PostMenuResponse;
import com.ourMenu.backend.domain.menulist.application.MenuListService;
import com.ourMenu.backend.domain.menulist.domain.MenuList;
import com.ourMenu.backend.domain.user.application.UserService;
import com.ourMenu.backend.domain.user.domain.User;
import com.ourMenu.backend.global.argument_resolver.UserId;
import com.ourMenu.backend.global.common.Status;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
Expand All @@ -30,8 +25,6 @@
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;

import java.util.*;
Expand Down Expand Up @@ -151,7 +144,7 @@ public Article saveArticleWithMenu(Article article, Long userId) {
}

@Transactional
public Article updateArticleWithMenu(Long articleId, Article article, Long userId) {
public Article updateArticleWithMenu(Long articleId, Article article, Long userId, List<Long> groupIds) {

Article findArticle = findOne(articleId);
User user = findArticle.getUser();
Expand All @@ -166,10 +159,24 @@ public Article updateArticleWithMenu(Long articleId, Article article, Long userI

findArticle.deleteAllArticleMenus();

for (ArticleMenu articleMenu : article.getArticleMenuList()) {
findArticle.addArticleMenu(articleMenu);
articleMenu.confirmArticle(findArticle);
articleMenuService.save(articleMenu);
List<Menu> menuList = menuService.getMenuByGroupId(groupIds, userId);
for (Menu menu : menuList) {
ArticleMenu articleMenu = ArticleMenu.builder()
.article(findArticle)
.title(menu.getTitle())
.price(menu.getPrice())
.placeTitle(menu.getPlace().getTitle())
.address(menu.getPlace().getAddress())
.imgUrl(menu.getImages().size() == 0 ? null : menu.getImages().get(0).getUrl())
.groupId(menu.getGroupId())
.menuMemoTitle(menu.getMemoTitle())
.menuIconType(menu.getMenuIconType())
.placeMemo(menu.getPlace().getInfo())
.placeLatitude(menu.getPlace().getLatitude())
.placeLongitude(menu.getPlace().getLongitude())
.build();
ArticleMenu saveArticleMenu = articleMenuService.save(articleMenu);
findArticle.addArticleMenu(saveArticleMenu);
}
findArticle.update(article);
return findArticle;
Expand Down Expand Up @@ -277,4 +284,5 @@ public Article getArticleByGroupId(PostArticleRequest postArticleRequest, Long u
return saveArticle;
}


}
Loading