Skip to content

Commit

Permalink
feat: commnet 작성 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
YongGoose committed Oct 30, 2023
1 parent ec14384 commit 929295c
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package WithYou.domain.comment.controller;

import WithYou.domain.comment.dto.request.CommentRegistDto;
import WithYou.domain.comment.service.CommentService;
import WithYou.global.jwt.MemberPrincipal;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class CommentController {
private final CommentService commentService;

@PostMapping("/comment/regist/{id}")
public ResponseEntity<?> registComment(@AuthenticationPrincipal MemberPrincipal memberPrincipal,
@RequestBody CommentRegistDto commentRegistDto,
@PathVariable Long id) {
commentService.registComment(id, commentRegistDto, memberPrincipal.getMember());
return ResponseEntity.ok()
.body(commentRegistDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package WithYou.domain.comment.dto.request;

import WithYou.domain.comment.entity.Comment;
import WithYou.domain.member.entity.Member;
import WithYou.domain.post.entity.Post;
import javax.validation.constraints.NotNull;

public class CommentRegistDto {
@NotNull
private String content;

public Comment of(Member member, Post post) {
return Comment.builder()
.content(content)
.userNickName(member.getNickName())
.userMajor(member.getMajor())
.userGrade(member.getGrade())
.post(post)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package WithYou.domain.comment.dto.response;

import WithYou.domain.comment.entity.Comment;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
public class CommentResponseDto {
private String content;
private String userNickName;
private String userMajor;
private int userGrade;

public static CommentResponseDto of(Comment comment) {
return new CommentResponseDto(comment.getContent(), comment.getUserNickName(), comment.getUserMajor(),
comment.getUserGrade());
}
}
8 changes: 8 additions & 0 deletions src/main/java/WithYou/domain/comment/entity/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Getter
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package WithYou.domain.comment.exception;

public class CommentNotFoundException extends RuntimeException {
public CommentNotFoundException(final String message) {
super(message);
}

public CommentNotFoundException() {
this("댓글을 찾을 수 없습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package WithYou.domain.comment.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class CommentQueryRepository {
private final JPAQueryFactory jpaQueryFactory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package WithYou.domain.comment.repository;

import WithYou.domain.comment.entity.Comment;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CommentRepository extends JpaRepository<Comment, Long> {
}
41 changes: 41 additions & 0 deletions src/main/java/WithYou/domain/comment/service/CommentService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package WithYou.domain.comment.service;

import WithYou.domain.comment.dto.request.CommentRegistDto;
import WithYou.domain.comment.dto.response.CommentResponseDto;
import WithYou.domain.comment.entity.Comment;
import WithYou.domain.comment.repository.CommentQueryRepository;
import WithYou.domain.comment.repository.CommentRepository;
import WithYou.domain.member.entity.Member;
import WithYou.domain.post.entity.Post;
import WithYou.domain.post.exception.PostNotFoundException;
import WithYou.domain.post.repository.PostReporitoy;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CommentService {
private final CommentRepository commentRepository;
private final CommentQueryRepository commentQueryRepository;
private final PostReporitoy postReporitoy;

public void registComment(Long id, CommentRegistDto commentRegistDto, Member member) {
Post post = postReporitoy.findPostById(id).orElseThrow(() -> new PostNotFoundException());

Comment comment = commentRegistDto.of(member, post);
commentRepository.save(comment);
}

public List<CommentResponseDto> changeCommentListToDtoList(List<Comment> commentList) {
return commentList.stream()
.map(CommentResponseDto::of)
.collect(Collectors.toList());
}

public List<Comment> findCommentByPostId(Long id) {
Post post = postReporitoy.findPostById(id).orElseThrow(() -> new PostNotFoundException());
return post.getCommentList();
}
}
11 changes: 10 additions & 1 deletion src/main/java/WithYou/domain/post/controller/PostController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package WithYou.domain.post.controller;

import WithYou.domain.comment.dto.response.CommentResponseDto;
import WithYou.domain.comment.entity.Comment;
import WithYou.domain.comment.service.CommentService;
import WithYou.domain.post.dto.request.PostRegistDto;
import WithYou.domain.post.dto.response.PostLookupDto;
import WithYou.domain.post.entity.Post;
import WithYou.domain.post.service.PostService;
import WithYou.domain.post.vo.CommentPostValueObject;
import WithYou.global.jwt.MemberPrincipal;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -23,6 +27,7 @@
@RequiredArgsConstructor
public class PostController {
private final PostService postService;
private final CommentService commentService;

@PostMapping("/post/regist")
public ResponseEntity<PostRegistDto> registPost(@AuthenticationPrincipal MemberPrincipal memberPrincipal,
Expand All @@ -46,8 +51,12 @@ public ResponseEntity<?> lookupPost(@AuthenticationPrincipal MemberPrincipal mem
public ResponseEntity<?> findPostById(@AuthenticationPrincipal MemberPrincipal memberPrincipal,
@RequestParam("id") Long id) {
Post post = postService.findPostAndVerifyMember(id, memberPrincipal.getMember());
List<Comment> comment = commentService.findCommentByPostId(id);
List<CommentResponseDto> commentResponseDtoList = commentService.changeCommentListToDtoList(comment);
PostLookupDto postLookupDto = postService.changePostToDto(post);
CommentPostValueObject commentPostValueObject = new CommentPostValueObject(commentResponseDtoList,
postLookupDto);
return ResponseEntity.ok()
.body(postLookupDto);
.body(commentPostValueObject);
}
}
5 changes: 3 additions & 2 deletions src/main/java/WithYou/domain/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public class Post extends BaseEntity {
private String title;
@Column(length = 5000)
private String content;
@OneToMany(mappedBy = "post", cascade = CascadeType.REMOVE)
private List<Comment> commentList = new ArrayList<>();
private String userNickName;
private String userMajor;
private int userGrade;
private int commentCount;
@OneToMany(mappedBy = "post", cascade = CascadeType.REMOVE)
private List<Comment> commentList = new ArrayList<>();
}
1 change: 0 additions & 1 deletion src/main/java/WithYou/domain/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public void savePost(PostRegistDto postRegistDto, Member member) {

public Page<Post> lookupDtoList(Member member, Pageable pageable) {
return postQueryRepository.findPostByMemberGrade(pageable, member);

}

public List<PostLookupDto> changeToPostLookupDtoList(Page<Post> posts) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/WithYou/domain/post/vo/CommentPostValueObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package WithYou.domain.post.vo;

import WithYou.domain.comment.dto.response.CommentResponseDto;
import WithYou.domain.post.dto.response.PostLookupDto;
import java.util.List;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public class CommentPostValueObject {
private List<CommentResponseDto> commentResponseDto;
private PostLookupDto postLookupDto;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import WithYou.domain.member.exception.MemberNickNameDulicatedException;
import WithYou.domain.member.exception.MemberNotFoundException;
import WithYou.domain.member.exception.MemberPasswordNotFoundException;
import WithYou.domain.post.exception.DepartmentNotMatchException;
import WithYou.domain.post.exception.PostNotFoundException;
import WithYou.domain.scrap.exception.ContentNotFoundException;
import WithYou.global.auth.exception.TokenDecodeException;
import WithYou.global.auth.exception.TokenException;
Expand All @@ -30,7 +32,8 @@ public ResponseEntity<ErrorResponse> handleInvalidAuthorization(final RuntimeExc
TokenUnsupportedException.class,
TokenDecodeException.class,
MemberIdDuplicatedException.class,
MemberNickNameDulicatedException.class
MemberNickNameDulicatedException.class,
DepartmentNotMatchException.class
})
public ResponseEntity<ErrorResponse> handleBadRequest(final RuntimeException e) {
ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
Expand All @@ -50,7 +53,8 @@ public ResponseEntity<ErrorResponse> handleInternationalServerError(final Runtim
MemberIdNotFoundException.class,
MemberPasswordNotFoundException.class,
ResultNotFoundException.class,
ContentNotFoundException.class
ContentNotFoundException.class,
PostNotFoundException.class
})
public ResponseEntity<ErrorResponse> handleNotFound(final RuntimeException e) {
ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
Expand Down

0 comments on commit 929295c

Please sign in to comment.