-
Notifications
You must be signed in to change notification settings - Fork 0
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
13 changed files
with
176 additions
and
6 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/WithYou/domain/comment/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 |
---|---|---|
@@ -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); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/WithYou/domain/comment/dto/request/CommentRegistDto.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,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(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/WithYou/domain/comment/dto/response/CommentResponseDto.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,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()); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/main/java/WithYou/domain/comment/exception/CommentNotFoundException.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,11 @@ | ||
package WithYou.domain.comment.exception; | ||
|
||
public class CommentNotFoundException extends RuntimeException { | ||
public CommentNotFoundException(final String message) { | ||
super(message); | ||
} | ||
|
||
public CommentNotFoundException() { | ||
this("댓글을 찾을 수 없습니다."); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/WithYou/domain/comment/repository/CommentQueryRepository.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,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; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/WithYou/domain/comment/repository/CommentRepository.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,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
41
src/main/java/WithYou/domain/comment/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 |
---|---|---|
@@ -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(); | ||
} | ||
} |
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
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
12 changes: 12 additions & 0 deletions
12
src/main/java/WithYou/domain/post/vo/CommentPostValueObject.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,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; | ||
} |
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