Skip to content

Commit

Permalink
Merge pull request #257 from Wingle-SMWU/feat/커뮤니티-게시글-사진-업로드-#256
Browse files Browse the repository at this point in the history
Feat/커뮤니티 게시글 사진 업로드 #256
  • Loading branch information
Lightieey authored Aug 14, 2023
2 parents 6c7b9a3 + 2a9bb9b commit c2e9b97
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
9 changes: 7 additions & 2 deletions wingle/src/main/java/kr/co/wingle/common/util/S3Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ public String profileImageUpload(MultipartFile file) {
}
}

public String articleImageUpload(MultipartFile file) throws IOException {
return upload(file, "article");
public String articleImageUpload(MultipartFile file) {
try {
return upload(file, "article");
} catch (IOException e) {
log.warn(e.getMessage());
throw new InternalServerErrorException(ErrorCode.FILE_UPLOAD_FAIL);
}
}

public void delete(String url) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package kr.co.wingle.community.article;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;

import org.springframework.util.Assert;

import kr.co.wingle.common.entity.BaseEntity;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ArticleImage extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
private Article article;

@Column(nullable = false)
private String imageUrl;

@Column(nullable = false)
private int orderNumber;

@Builder
ArticleImage(Article article, String imageUrl, int orderNumber) {
Assert.notNull(article, "article must not be null");
Assert.notNull(imageUrl, "imageUrl must not be null");

this.article = article;
this.imageUrl = imageUrl;
this.orderNumber = orderNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kr.co.wingle.community.article;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ArticleImageRepository extends JpaRepository<ArticleImage, Long> {

List<ArticleImage> getArticleImagesByArticleIdAndIsDeletedOrderByOrderNumber(Long articleId, boolean isDeleted);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import kr.co.wingle.common.constants.ErrorCode;
import kr.co.wingle.common.exception.ForbiddenException;
import kr.co.wingle.common.exception.NotFoundException;
import kr.co.wingle.common.util.S3Util;
import kr.co.wingle.community.forum.Forum;
import kr.co.wingle.community.forum.ForumCode;
import kr.co.wingle.community.forum.ForumService;
Expand All @@ -25,9 +26,11 @@
@Service
public class ArticleService extends WritingService {
private final ArticleRepository articleRepository;
private final ArticleImageRepository articleImageRepository;
private final ForumService forumService;
private final AuthService authService;
private final ArticleMapper articleMapper;
private final S3Util s3Util;

@Transactional
public ArticleResponseDto create(ArticleRequestDto request) {
Expand All @@ -47,18 +50,28 @@ public ArticleResponseDto create(ArticleRequestDto request) {

articleRepository.save(article);

ArrayList<String> imageUrlList = new ArrayList<>();
for (int i = 0; i < request.getImages().size(); i++) {
String imageUrl = s3Util.articleImageUpload(request.getImages().get(i));
imageUrlList.add(imageUrl);
articleImageRepository.save(new ArticleImage(article, imageUrl, i + 1));
}

// TODO: Redis 최신목록에 등록

// TODO: new ArrayList<String> 부분을 s3에서 받은 이미지 경로로 변경
return articleMapper.toResponseDto(article, new ArrayList<String>());
return articleMapper.toResponseDto(article, imageUrlList);
}

@Transactional(readOnly = true)
public ArticleResponseDto getOne(Long forumId, Long articleId) {
Article article = getArticleById(articleId);
isValidForum(article, forumId);
// TODO: new ArrayList<String> 부분을 s3에서 받은 이미지 경로로 변경
return articleMapper.toResponseDto(article, new ArrayList<String>());

List<String> imageUrlList = articleImageRepository.getArticleImagesByArticleIdAndIsDeletedOrderByOrderNumber(
article.getId(), false)
.stream().map(articleImage -> articleImage.getImageUrl()).collect(Collectors.toList());

return articleMapper.toResponseDto(article, imageUrlList);
}

@Transactional(readOnly = true)
Expand All @@ -71,9 +84,11 @@ public List<ArticleResponseDto> getList(Long forumId, int page, int size, boolea
} else {
pages = articleRepository.findByForumIdAndIsDeleted(forumId, false, pageable);
}
// TODO: new ArrayList<String> 부분을 s3에서 받은 이미지 경로로 변경

return pages.stream()
.map(x -> articleMapper.toResponseDto(x, new ArrayList<String>()))
.map(x -> articleMapper.toResponseDto(x,
articleImageRepository.getArticleImagesByArticleIdAndIsDeletedOrderByOrderNumber(x.getId(), false)
.stream().map(articleImage -> articleImage.getImageUrl()).collect(Collectors.toList())))
.collect(
Collectors.toList());
}
Expand All @@ -84,6 +99,8 @@ public Long delete(Long forumId, Long articleId) {
Article article = getArticleById(articleId);

if (isValidMember(article, member) && isExist(article) && isValidForum(article, forumId)) {
articleImageRepository.getArticleImagesByArticleIdAndIsDeletedOrderByOrderNumber(articleId, false)
.forEach(articleImage -> articleImage.softDelete());
article.softDelete();
}
return article.getId();
Expand Down

0 comments on commit c2e9b97

Please sign in to comment.