-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[7회차] 1:N 관계 #14
Open
tkdgur0906
wants to merge
6
commits into
master
Choose a base branch
from
develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[7회차] 1:N 관계 #14
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f93b58f
feat: 게시글 작성 사용자 같이 저장하도록 기능 변경
tkdgur0906 ddd7e6c
feat: 특정 게시글 수정할 때 사용자 검증 기능 추가
tkdgur0906 07fd3c6
feat: 특정 게시글 삭제할 때 사용자 검증 기능 추가
tkdgur0906 a2e58a8
feat: 특정 게시글 조회할 때 댓글도 조회하도록 변경
tkdgur0906 651a63a
feat: 댓글 작성 기능 구현
tkdgur0906 5299eba
feat: 특정 게시글 조회할 때 댓글 작성자의 이메일도 조회
tkdgur0906 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
22 changes: 22 additions & 0 deletions
22
src/main/java/com/jscode/board/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,22 @@ | ||
package com.jscode.board.controller; | ||
|
||
import com.jscode.board.dto.comment.CommentDto; | ||
import com.jscode.board.service.CommentService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/boards/{boardId}/comments") | ||
public class CommentController { | ||
|
||
private final CommentService commentService; | ||
|
||
@PostMapping | ||
public ResponseEntity<Long> commentAdd(@PathVariable Long boardId, @RequestBody CommentDto commentDto) { | ||
Long id = commentService.saveComment(boardId, commentDto); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(id); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,33 @@ | ||
package com.jscode.board.domain; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
public class Comment { | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
public class Comment extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@Lob | ||
private String content; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "board_id") | ||
private Board board; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id") | ||
private Member member; | ||
|
||
@Builder | ||
public Comment(String content, Board board, Member member) { | ||
this.content = content; | ||
this.board = board; | ||
this.member = member; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/jscode/board/dto/board/BoardAndCommentsDto.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 com.jscode.board.dto.board; | ||
|
||
import com.jscode.board.domain.Board; | ||
import com.jscode.board.domain.Comment; | ||
import com.jscode.board.dto.comment.CommentDto; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class BoardAndCommentsDto { | ||
|
||
private Long id; | ||
private String title; | ||
private String content; | ||
private LocalDateTime createdDate; | ||
private List<CommentDto> comments; | ||
|
||
@Builder | ||
public static BoardAndCommentsDto from(Board board, List<Comment> comments){ | ||
BoardAndCommentsDto boardAndCommentsDto = new BoardAndCommentsDto(); | ||
boardAndCommentsDto.id = board.getId(); | ||
boardAndCommentsDto.title = board.getTitle(); | ||
boardAndCommentsDto.content = board.getContent(); | ||
boardAndCommentsDto.createdDate = board.getCreatedDate(); | ||
|
||
List<CommentDto> commentDtos = comments.stream().map(CommentDto::from) | ||
.collect(Collectors.toList()); | ||
|
||
boardAndCommentsDto.comments = commentDtos; | ||
return boardAndCommentsDto; | ||
} | ||
|
||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/jscode/board/dto/comment/CommentDto.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,43 @@ | ||
package com.jscode.board.dto.comment; | ||
|
||
import com.jscode.board.domain.Board; | ||
import com.jscode.board.domain.Comment; | ||
import com.jscode.board.domain.Member; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class CommentDto { | ||
|
||
private String email; | ||
private String content; | ||
private LocalDateTime createdDate; | ||
|
||
@Builder | ||
public CommentDto(String email, String content, LocalDateTime createdDate) { | ||
this.email = email; | ||
this.content = content; | ||
this.createdDate = createdDate; | ||
} | ||
|
||
public static CommentDto from(Comment comment){ | ||
return CommentDto.builder() | ||
.email(comment.getMember().getEmail()) | ||
.content(comment.getContent()) | ||
.createdDate(comment.getCreatedDate()) | ||
.build(); | ||
} | ||
|
||
public static Comment toEntity(CommentDto commentDto, Board board, Member member){ | ||
return Comment.builder() | ||
.content(commentDto.getContent()) | ||
.board(board) | ||
.member(member) | ||
.build(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/jscode/board/exception/board/NoAuthorityMemberException.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,10 @@ | ||
package com.jscode.board.exception.board; | ||
|
||
import com.jscode.board.exception.code.ErrorCode; | ||
import com.jscode.board.exception.common.BusinessException; | ||
|
||
public class NoAuthorityMemberException extends BusinessException { | ||
public NoAuthorityMemberException() { | ||
super(ErrorCode.NO_AUTHORITY_MEMBER_EXCEPTION); | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
src/main/java/com/jscode/board/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,15 @@ | ||
package com.jscode.board.repository; | ||
|
||
import com.jscode.board.domain.Board; | ||
import com.jscode.board.domain.Comment; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import java.util.List; | ||
|
||
public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
|
||
@Query("SELECT c FROM Comment c JOIN FETCH c.member m JOIN FETCH c.board b WHERE b = :board") | ||
List<Comment> findAllByBoardAndMember(@Param("board") Board board); | ||
} |
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,10 +1,17 @@ | ||
package com.jscode.board.service; | ||
|
||
import com.jscode.board.domain.Board; | ||
import com.jscode.board.domain.Comment; | ||
import com.jscode.board.domain.Member; | ||
import com.jscode.board.dto.board.BoardAndCommentsDto; | ||
import com.jscode.board.dto.board.BoardRequest; | ||
import com.jscode.board.dto.board.BoardResponse; | ||
import com.jscode.board.exception.board.NoAuthorityMemberException; | ||
import com.jscode.board.exception.board.NotFoundBoardException; | ||
import com.jscode.board.repository.BoardRepository; | ||
import com.jscode.board.repository.CommentRepository; | ||
import com.jscode.board.repository.MemberRepository; | ||
import com.jscode.board.util.SecurityUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
@@ -18,13 +25,16 @@ | |
public class BoardService { | ||
|
||
private final BoardRepository boardRepository; | ||
private final CommentRepository commentRepository; | ||
private final MemberRepository memberRepository; | ||
|
||
/** | ||
* 게시글 생성 | ||
*/ | ||
public BoardResponse saveBoard(BoardRequest request) { | ||
Board savedBoard = boardRepository.save(BoardRequest.toEntity(request)); | ||
return BoardResponse.from(savedBoard); | ||
Member member = memberRepository.getOne(SecurityUtil.getCurrentMemberId()); | ||
Board newBoard = new Board(request.getTitle(), request.getContent(), member); | ||
return BoardResponse.from(boardRepository.save(newBoard)); | ||
} | ||
|
||
/** | ||
|
@@ -41,9 +51,11 @@ public List<BoardResponse> findBoards() { | |
/** | ||
* id로 게시글 조회 | ||
*/ | ||
public BoardResponse findBoardById(Long id) { | ||
public BoardAndCommentsDto findBoardById(Long id) { | ||
Board board = boardRepository.findById(id).orElseThrow(NotFoundBoardException::new); | ||
return BoardResponse.from(board); | ||
List<Comment> comments = commentRepository.findAllByBoardAndMember(board); | ||
BoardAndCommentsDto boardAndCommentsDto = BoardAndCommentsDto.from(board, comments); | ||
return boardAndCommentsDto; | ||
} | ||
|
||
/** | ||
|
@@ -61,7 +73,11 @@ public List<BoardResponse> findBoardByKeyword(String keyword) { | |
* 게시글 내용 변경 | ||
*/ | ||
public BoardResponse updateBoard(Long id, BoardRequest request) { | ||
Long currentMemberId = SecurityUtil.getCurrentMemberId(); | ||
Board board = boardRepository.findById(id).orElseThrow(NotFoundBoardException::new); | ||
if(currentMemberId != board.getMember().getId()){ | ||
throw new NoAuthorityMemberException(); | ||
} | ||
board.updateTitle(request.getTitle()); | ||
board.updateContent(request.getContent()); | ||
return BoardResponse.from(board); | ||
|
@@ -71,7 +87,11 @@ public BoardResponse updateBoard(Long id, BoardRequest request) { | |
* 게시글 삭제 | ||
*/ | ||
public Long deleteBoard(Long id){ | ||
Long currentMemberId = SecurityUtil.getCurrentMemberId(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jwt 인증 깔끔하게 잘 하신 것 같습니다! |
||
Board board = boardRepository.findById(id).orElseThrow(NotFoundBoardException::new); | ||
if(currentMemberId != board.getMember().getId()){ | ||
throw new NoAuthorityMemberException(); | ||
} | ||
boardRepository.delete(board); | ||
return board.getId(); | ||
} | ||
|
31 changes: 31 additions & 0 deletions
31
src/main/java/com/jscode/board/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,31 @@ | ||
package com.jscode.board.service; | ||
|
||
import com.jscode.board.domain.Board; | ||
import com.jscode.board.domain.Comment; | ||
import com.jscode.board.domain.Member; | ||
import com.jscode.board.dto.comment.CommentDto; | ||
import com.jscode.board.exception.board.NotFoundBoardException; | ||
import com.jscode.board.repository.BoardRepository; | ||
import com.jscode.board.repository.CommentRepository; | ||
import com.jscode.board.repository.MemberRepository; | ||
import com.jscode.board.util.SecurityUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class CommentService { | ||
|
||
private final BoardRepository boardRepository; | ||
private final CommentRepository commentRepository; | ||
private final MemberRepository memberRepository; | ||
|
||
public Long saveComment(Long boardId, CommentDto commentDto) { | ||
Member member = memberRepository.getOne(SecurityUtil.getCurrentMemberId()); | ||
Board board = boardRepository.findById(boardId).orElseThrow(NotFoundBoardException::new); | ||
Comment comment = commentRepository.save(CommentDto.toEntity(commentDto, board, member)); | ||
return comment.getId(); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
작성하신 Comment 기능 관련 코드들 잘 작성하셨습니다!! 특히 사용자 인증의 경우 제 방법보다 더 깔끔하게 작성하셔서 배워갑니다!!