Skip to content

Commit

Permalink
Merge pull request #82 from jisung-in/feature/80-comment-image
Browse files Browse the repository at this point in the history
[Feature] 의견 등록 시 이미지 업로드 기능 추가
  • Loading branch information
AHNYUNKI authored Apr 18, 2024
2 parents e1a4e60 + dbb5828 commit d287c54
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.jisungin.application.comment.request.CommentCreateServiceRequest;
import jakarta.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -13,14 +15,18 @@ public class CommentCreateRequest {
@NotBlank(message = "내용은 필수 입니다.")
private String content;

private List<String> imageUrls = new ArrayList<>();

@Builder
private CommentCreateRequest(String content) {
private CommentCreateRequest(String content, List<String> imageUrls) {
this.content = content;
this.imageUrls = imageUrls;
}

public CommentCreateServiceRequest toService() {
return CommentCreateServiceRequest.builder()
.content(content)
.imageUrls(imageUrls)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.jisungin.api.comment.request;

import com.jisungin.application.comment.request.CommentEditServiceRequest;
import java.util.ArrayList;
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -11,9 +13,15 @@ public class CommentEditRequest {

private String content;

private List<String> newImage = new ArrayList<>();

private List<String> removeImage = new ArrayList<>();

@Builder
private CommentEditRequest(String content) {
private CommentEditRequest(String content, List<String> newImage, List<String> removeImage) {
this.content = content;
this.newImage = newImage;
this.removeImage = removeImage;
}

public CommentEditServiceRequest toService() {
Expand Down
33 changes: 30 additions & 3 deletions src/main/java/com/jisungin/application/comment/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import com.jisungin.domain.ReadingStatus;
import com.jisungin.domain.comment.Comment;
import com.jisungin.domain.comment.repository.CommentRepository;
import com.jisungin.domain.commentimage.CommentImage;
import com.jisungin.domain.commentimage.repository.CommentImageRepository;
import com.jisungin.domain.commentlike.repository.CommentLikeRepository;
import com.jisungin.domain.userlibrary.repository.UserLibraryRepository;
import com.jisungin.domain.talkroom.TalkRoom;
import com.jisungin.domain.talkroom.repository.TalkRoomRepository;
import com.jisungin.domain.talkroom.repository.TalkRoomRoleRepository;
import com.jisungin.domain.user.User;
import com.jisungin.domain.user.repository.UserRepository;
import com.jisungin.domain.userlibrary.repository.UserLibraryRepository;
import com.jisungin.exception.BusinessException;
import com.jisungin.exception.ErrorCode;
import java.util.Collections;
Expand All @@ -35,6 +37,7 @@ public class CommentService {
private final CommentLikeRepository commentLikeRepository;
private final UserLibraryRepository userLibraryRepository;
private final TalkRoomRoleRepository talkRoomRoleRepository;
private final CommentImageRepository commentImageRepository;

@Transactional
public CommentResponse writeComment(CommentCreateServiceRequest request, Long talkRoomId, Long userId) {
Expand All @@ -56,7 +59,15 @@ public CommentResponse writeComment(CommentCreateServiceRequest request, Long ta

commentRepository.save(comment);

return CommentResponse.of(comment.getContent(), user.getName());
if (request.getImageUrls() != null && !request.getImageUrls().isEmpty()) {
request.getImageUrls().stream()
.map(url -> CommentImage.createImages(comment, url))
.forEach(commentImageRepository::save);
}

List<String> imageUrls = commentImageRepository.findByCommentIdWithImageUrl(comment.getId());

return CommentResponse.of(comment.getContent(), user.getName(), imageUrls);
}

public CommentPageResponse findAllComments(Long talkRoomId, Long userId) {
Expand Down Expand Up @@ -88,7 +99,19 @@ public CommentResponse editComment(Long commentId, CommentEditServiceRequest req

comment.edit(request.getContent());

return CommentResponse.of(comment.getContent(), user.getName());
if (request.getNewImage() != null && !request.getNewImage().isEmpty()) {
request.getNewImage().stream().map(url -> CommentImage.createImages(comment, url))
.forEach(commentImageRepository::save);
}

if (request.getRemoveImage() != null && !request.getRemoveImage().isEmpty()) {
request.getRemoveImage().stream().map(url -> commentImageRepository.findByCommentAndImageUrl(comment, url))
.forEach(commentImageRepository::deleteAll);
}

List<String> imageUrls = commentImageRepository.findByCommentIdWithImageUrl(comment.getId());

return CommentResponse.of(comment.getContent(), user.getName(), imageUrls);
}

@Transactional
Expand All @@ -103,6 +126,10 @@ public void deleteComment(Long commentId, Long userId) {
throw new BusinessException(ErrorCode.UNAUTHORIZED_REQUEST);
}

List<CommentImage> images = commentImageRepository.findByComment(comment);
if (images != null && !images.isEmpty()) {
commentImageRepository.deleteAll(images);
}
commentRepository.delete(comment);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jisungin.application.comment.request;

import java.util.ArrayList;
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -10,9 +12,12 @@ public class CommentCreateServiceRequest {

private String content;

private List<String> imageUrls = new ArrayList<>();

@Builder
private CommentCreateServiceRequest(String content) {
private CommentCreateServiceRequest(String content, List<String> imageUrls) {
this.content = content;
this.imageUrls = imageUrls;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jisungin.application.comment.request;

import java.util.ArrayList;
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -10,9 +12,15 @@ public class CommentEditServiceRequest {

private String content;

private List<String> newImage = new ArrayList<>();

private List<String> removeImage = new ArrayList<>();

@Builder
private CommentEditServiceRequest(String content) {
private CommentEditServiceRequest(String content, List<String> newImage, List<String> removeImage) {
this.content = content;
this.newImage = newImage;
this.removeImage = removeImage;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jisungin.application.comment.response;

import java.util.ArrayList;
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -12,16 +14,20 @@ public class CommentResponse {

private String userName;

private List<String> imageUrls = new ArrayList<>();

@Builder
private CommentResponse(String content, String userName) {
private CommentResponse(String content, String userName, List<String> imageUrls) {
this.content = content;
this.userName = userName;
this.imageUrls = imageUrls;
}

public static CommentResponse of(String content, String name) {
public static CommentResponse of(String content, String name, List<String> imageUrls) {
return CommentResponse.builder()
.content(content)
.userName(name)
.imageUrls(imageUrls)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ public void editTalkRoom(TalkRoomEditServiceRequest request, Long userId) {
}

if (request.getRemoveImage() != null && !request.getRemoveImage().isEmpty()) {
request.getRemoveImage().stream().map(s -> talkRoomImageRepository.findByTalkRoomAndImageUrl(talkRoom, s))
request.getRemoveImage().stream()
.map(url -> talkRoomImageRepository.findByTalkRoomAndImageUrl(talkRoom, url))
.forEach(talkRoomImageRepository::deleteAll);
}
}
Expand All @@ -158,7 +159,7 @@ public void deleteTalkRoom(Long talkRoomId, Long userId) {
commentRepository.findByTalkRoom(talkRoom).ifPresent(commentRepository::delete);

List<TalkRoomImage> images = talkRoomImageRepository.findByTalkRoom(talkRoom);
if (images != null) {
if (images != null && !images.isEmpty()) {
talkRoomImageRepository.deleteAll(images);
}
talkRoomRoleRepository.deleteAllByTalkRoom(talkRoom);
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/jisungin/domain/commentimage/CommentImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.jisungin.domain.commentimage;

import com.jisungin.domain.comment.Comment;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class CommentImage {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "image_url")
private String imageUrl;

@ManyToOne
@JoinColumn(name = "comment_id")
private Comment comment;

@Builder
private CommentImage(String imageUrl, Comment comment) {
this.imageUrl = imageUrl;
this.comment = comment;
}

public static CommentImage createImages(Comment comment, String imageUrl) {
return CommentImage.builder()
.comment(comment)
.imageUrl(imageUrl)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.jisungin.domain.commentimage.repository;

import com.jisungin.domain.comment.Comment;
import com.jisungin.domain.commentimage.CommentImage;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface CommentImageRepository extends JpaRepository<CommentImage, Long> {

@Query(
"select ci.imageUrl from CommentImage ci where ci.comment.id = :commentId"
)
List<String> findByCommentIdWithImageUrl(@Param("commentId") Long commentId);

List<CommentImage> findByCommentAndImageUrl(Comment comment, String url);

List<CommentImage> findByComment(Comment comment);
}
Loading

0 comments on commit d287c54

Please sign in to comment.