Skip to content

Commit

Permalink
feat: Edit write, getSimeple post logic with hasPostFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Minuooooo committed Jul 13, 2024
1 parent f0050ea commit 2909111
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ public class GetSimplePostInfosResponseDto {
private String createdAt;
private String writerEmail;
private String title;
private Boolean existsFile;
private Boolean hasPostFile;

public static GetSimplePostInfosResponseDto from(Post post){
return GetSimplePostInfosResponseDto.builder()
.id(post.getId())
.writerEmail(post.getWriter().getEmail())
.createdAt(post.getCreatedAt())
.title(post.getTitle())
.existsFile(post.checkFileExist())
.hasPostFile(post.getHasPostFile())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public class WritePostRequestDto {
@Schema(description = "글 내용", defaultValue = "빨리 가을이 왔으면 좋겠어요.")
private String content;

public Post toEntity(Member writer){
public Post toEntity(Member writer, Boolean hasPostFile){
return Post.builder()
.writer(writer)
.title(this.title)
.content(this.content)
.hits(0)
.hasPostFile(hasPostFile)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,11 @@ public class Post extends AuditEntity {
private String content;
@Column(nullable = false)
private Integer hits;
private boolean hasPostFile;
@Column(nullable = false)
private Boolean hasPostFile;
@Formula("(SELECT COUNT(*) FROM post_like pl WHERE pl.post_id = post_id)") // 테이블에는 만들어지지 않는 가상 속성. 게시글을 좋아요 순으로 정렬할 때 사용 (api 요청할때 sort=likes,desc)
private Integer likes;

public Boolean checkFileExist() {
if(this.postFiles.isEmpty()) return false;
return true;
}

public List<String> getFileUrls(){
List<String> fileUrls = new ArrayList<>();
for(PostFile postFile : postFiles){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ public class PostService {
public void writePost(WritePostRequestDto writePostRequestDto, List<MultipartFile> multipartFiles, Member writer) {
// s3에 파일을 업로드 한 뒤 예외가 발생하면 db는 롤백이 되지만,
// 이미 s3에 저장된 이미지는 삭제되지 않는 문제가 있음.

// post를 먼저 저장
Post post = postRepository.save(writePostRequestDto.toEntity(writer));
//파일을 첨부한 경우
if(multipartFiles != null){
// s3에 첨부파일을 저장하고, db에도 post_file을 저장
uploadPostFileList(multipartFiles, post);
if(checkEmptyPostFiles(multipartFiles)){
save(writePostRequestDto.toEntity(writer, false));
return;
}
uploadPostFileList(multipartFiles, save(writePostRequestDto.toEntity(writer, true)));
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -123,7 +120,6 @@ public void editPostInfo(Long postId, WritePostRequestDto editPostInfoRequestDto
for(String fileUrl : fileUrls) s3Service.deleteFile(fileUrl);
}

@Transactional
public void uploadPostFileList(List<MultipartFile> multipartFiles, Post post) {
// s3에 파일을 업로드 한 뒤 예외가 발생하면 db는 롤백이 되지만,
// 이미 s3에 저장된 이미지는 삭제되지 않는 문제가 있음.
Expand All @@ -142,4 +138,12 @@ public Page<GetSimplePostInfosResponseDto> getMySimplePostInfos(Pageable pageabl
return postRepository.findAllByWriterId(currentMember.getId(), pageable)
.map(GetSimplePostInfosResponseDto::from);
}

private boolean checkEmptyPostFiles(List<MultipartFile> postFiles) {
return postFiles.isEmpty();
}

private Post save(Post post) {
return postRepository.save(post);
}
}

0 comments on commit 2909111

Please sign in to comment.