-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
183 additions
and
18 deletions.
There are no files selected for viewing
41 changes: 39 additions & 2 deletions
41
src/main/java/com/kimgreen/backend/domain/community/controller/CommentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,48 @@ | ||
package com.kimgreen.backend.domain.community.controller; | ||
|
||
import com.kimgreen.backend.domain.community.dto.GetCommentDto; | ||
import com.kimgreen.backend.domain.community.dto.PostCommentDto; | ||
import com.kimgreen.backend.domain.community.entity.Comment; | ||
import com.kimgreen.backend.domain.community.service.CommentService; | ||
import com.kimgreen.backend.response.Response; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
import static com.kimgreen.backend.response.Message.*; | ||
import static com.kimgreen.backend.response.Response.success; | ||
import static org.springframework.http.HttpStatus.OK; | ||
|
||
@Tag(name = "Comment") | ||
@RestController | ||
@RequestMapping(value="/comment") | ||
@AllArgsConstructor | ||
public class CommentController { | ||
private CommentService commentService; | ||
@Operation(summary = "댓글 작성") | ||
@ResponseStatus(OK) | ||
@PostMapping("/write-comment") | ||
public Response postComment(@RequestParam("postId") Long postId, @RequestBody PostCommentDto postCommentDto) { | ||
commentService.postComment(postId, postCommentDto); | ||
return success(POST_COMMENT_SUCCESS); | ||
} | ||
|
||
@Operation(summary = "댓글 삭제") | ||
@ResponseStatus(OK) | ||
@DeleteMapping("/delete-comment") | ||
public Response deleteComment(@RequestParam("commentId") Long commentId){ | ||
commentService.deleteComment(commentId); | ||
return success(DELETE_COMMENT_SUCCESS); | ||
} | ||
|
||
@Operation(summary = "댓글 목록 불러오기") | ||
@ResponseStatus(OK) | ||
@GetMapping("/get-comment") | ||
public Response getComment(@RequestParam("postId") Long postId){ | ||
List <GetCommentDto> commentList = commentService.getComment(postId); | ||
return success(GET_COMMENT_SUCCESS, commentList); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/kimgreen/backend/domain/community/dto/GetCommentDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.kimgreen.backend.domain.community.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.kimgreen.backend.domain.community.entity.Comment; | ||
import lombok.*; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Builder | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Component | ||
public class GetCommentDto { | ||
private Long commentId; | ||
private String writerProfileImg; | ||
private String writerNickname; | ||
private String writerBadge; | ||
private String content; | ||
private boolean isWriter; | ||
private LocalDateTime createAt; | ||
|
||
public static GetCommentDto from(Comment comment, String writerProfileImg, String writerBadge, boolean isWriter) { | ||
|
||
return GetCommentDto.builder() | ||
.commentId(comment.getCommentId()) | ||
.writerProfileImg(writerProfileImg) | ||
.writerNickname(comment.getMember().getNickname()) | ||
.writerBadge(writerBadge) | ||
.content(comment.getContent()) | ||
.isWriter(isWriter) | ||
.createAt(comment.getCreatedAt()) | ||
.build(); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/kimgreen/backend/domain/community/dto/PostCommentDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.kimgreen.backend.domain.community.dto; | ||
|
||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
|
||
public class PostCommentDto { | ||
private String content; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,6 @@ public class Comment extends AuditEntity { | |
|
||
@Column | ||
private String content; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ | |
|
||
@Repository | ||
public interface PostImgRepository extends JpaRepository<PostImg, Long> { | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/com/kimgreen/backend/domain/community/service/CommentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,88 @@ | ||
package com.kimgreen.backend.domain.community.service; | ||
|
||
import com.kimgreen.backend.domain.community.dto.GetCommentDto; | ||
import com.kimgreen.backend.domain.community.dto.PostCommentDto; | ||
import com.kimgreen.backend.domain.community.entity.Comment; | ||
import com.kimgreen.backend.domain.community.entity.Post; | ||
import com.kimgreen.backend.domain.community.repository.CommentRepository; | ||
import com.kimgreen.backend.domain.community.repository.PostRepository; | ||
import com.kimgreen.backend.domain.member.entity.Member; | ||
import com.kimgreen.backend.domain.member.entity.MemberProfileImg; | ||
import com.kimgreen.backend.domain.member.repository.MemberProfileImgRepository; | ||
import com.kimgreen.backend.domain.member.service.MemberService; | ||
import com.kimgreen.backend.domain.profile.entity.RepresentativeBadge; | ||
import com.kimgreen.backend.domain.profile.repository.RepresentativeBadgeRepository; | ||
import com.kimgreen.backend.exception.PostNotFound; | ||
import com.kimgreen.backend.exception.WrongPath; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.*; | ||
|
||
@Service | ||
@AllArgsConstructor | ||
|
||
public class CommentService { | ||
|
||
private final MemberService memberService; | ||
private final CommentRepository commentRepository; | ||
private final PostRepository postRepository; | ||
private final GetCommentDto getCommentDto; | ||
private final S3Service s3Service; | ||
private final RepresentativeBadgeRepository representativeBadgeRepository; | ||
|
||
private final MemberProfileImgRepository memberProfileImgRepository; | ||
|
||
public void postComment(Long postId, PostCommentDto postCommentDto){ | ||
Member member = memberService.getCurrentMember(); | ||
Post post = postRepository.findById(postId).orElseThrow(PostNotFound::new); | ||
|
||
Comment comment = Comment.builder() | ||
.content(postCommentDto.getContent()) | ||
.post(post) | ||
.member(member) | ||
.build(); | ||
// 데이터 베이스에 저장하기 | ||
commentRepository.save(comment); | ||
// 명세서의 오류들 처리하기??? | ||
} | ||
|
||
public void deleteComment(Long commentId){ | ||
Member member = memberService.getCurrentMember(); | ||
Comment comment = commentRepository.findById(commentId).orElseThrow(WrongPath::new); | ||
commentRepository.delete(comment); | ||
|
||
} | ||
|
||
public List<GetCommentDto> getComment(Long postId){ | ||
List <Comment> allCommentList = commentRepository.findAll(); // 모든 댓글 목록 | ||
Post post = postRepository.findById(postId).orElseThrow(PostNotFound::new); // 내가 보고싶은 포스트 | ||
|
||
List <GetCommentDto> commentList = new ArrayList<>(); // 리턴 | ||
|
||
for (Comment comment : allCommentList){ | ||
if (comment.getPost() == post){ | ||
MemberProfileImg memberProfileImg = memberProfileImgRepository.findByMember(comment.getMember()); | ||
RepresentativeBadge representativeBadge = representativeBadgeRepository.findByMember(comment.getMember()); | ||
/* | ||
GetCommentDto.from(comment, | ||
s3Service.getFullUrl(memberProfileImg.getImgUrl()), | ||
representativeBadge.getRepresentativeBadge().name, | ||
comment.getMember().getMemberId().equals(memberService.getCurrentMember().getMemberId())); | ||
*/ | ||
GetCommentDto getCommentDto1 = GetCommentDto.builder() | ||
.commentId(comment.getCommentId()) | ||
.writerProfileImg(s3Service.getFullUrl(memberProfileImg.getImgUrl())) | ||
.writerNickname(comment.getMember().getNickname()) | ||
.writerBadge(representativeBadge.getRepresentativeBadge().name) | ||
.content(comment.getContent()) | ||
.isWriter(comment.getMember().getMemberId().equals(memberService.getCurrentMember().getMemberId())) | ||
.createAt(comment.getCreatedAt()) | ||
.build(); | ||
commentList.add(getCommentDto1); | ||
} | ||
} | ||
return commentList; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.kimgreen.backend.exception; | ||
|
||
public class WrongPath extends RuntimeException{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters