-
Notifications
You must be signed in to change notification settings - Fork 7
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
[14주차] 고태현 학습 PR 제출합니다 #3
base: main
Are you sure you want to change the base?
Changes from all commits
ddb0250
06c503a
4b59a4e
b394909
cdae34d
25c4c79
f6791fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.example.jpa.board.controller; | ||
|
||
import com.example.jpa.board.domain.Board; | ||
import com.example.jpa.board.dto.request.BoardCreateRequest; | ||
import com.example.jpa.board.dto.request.BoardUpdateRequest; | ||
import com.example.jpa.board.dto.response.BoardResponse; | ||
import com.example.jpa.board.service.BoardService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("api/board") | ||
@RequiredArgsConstructor | ||
public class BoardController { | ||
private final BoardService boardService; | ||
|
||
@PostMapping | ||
public ResponseEntity<Void> create(@RequestBody BoardCreateRequest boardCreateRequest) { | ||
boardService.create(boardCreateRequest); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<BoardResponse> findOne(@PathVariable("id") Long id) { | ||
BoardResponse boardResponse = boardService.findOne(id); | ||
return ResponseEntity.ok(boardResponse); | ||
} | ||
|
||
@GetMapping("/findAll") | ||
public ResponseEntity<BoardResponse> findAll() { | ||
List<BoardResponse> boardResponseList = boardService.findAll(); | ||
return ResponseEntity.ok((BoardResponse) boardResponseList); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public ResponseEntity<BoardResponse> updateBoard(@RequestBody BoardUpdateRequest boardUpdateRequest) { | ||
boardService.updateBoard(boardUpdateRequest); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deleteBoard(@PathVariable("id") Long id) { | ||
boardService.deleteBoard(id); | ||
return ResponseEntity.ok().build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.example.jpa.board.domain; | ||
|
||
import com.example.jpa.board.dto.request.BoardUpdateRequest; | ||
import com.example.jpa.member.domain.Member; | ||
import com.example.jpa.reply.domain.Reply; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
Comment on lines
+12
to
+16
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. 지양해야하는 방식입니다. 저렇게 getter와 setter를 다 열어둔다면 필드를 Private으로 한 이유가 없습니다. |
||
public class Board { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String title; | ||
private String content; | ||
|
||
private Long member_id; | ||
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. 값자기 스네이크 케이스를 쓰신 이유가 있나요? |
||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private Member member; | ||
|
||
@OneToMany | ||
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. mappedBy 옵션이 없습니다! |
||
private List<Reply> replyList; | ||
|
||
|
||
public Board(Long id, String title, String content, Member member, List<Reply> replyList) { | ||
this.id = id; | ||
this.title = title; | ||
this.content = content; | ||
this.member = member; | ||
this.replyList = replyList; | ||
} | ||
|
||
public void update(BoardUpdateRequest boardUpdateRequest) { | ||
this.title = boardUpdateRequest.getTitle(); | ||
this.content = boardUpdateRequest.getContent(); | ||
this.member = boardUpdateRequest.getMember(); | ||
} | ||
Comment on lines
+43
to
+47
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. 도에인에 Request 객체가 있는 형태는 좋지 않은 방식입니다! |
||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,20 @@ | ||||||||||||||
package com.example.jpa.board.dto.request; | ||||||||||||||
|
||||||||||||||
import com.example.jpa.board.domain.Board; | ||||||||||||||
import com.example.jpa.member.domain.Member; | ||||||||||||||
import lombok.Getter; | ||||||||||||||
|
||||||||||||||
@Getter | ||||||||||||||
public class BoardCreateRequest { | ||||||||||||||
private String title; | ||||||||||||||
private String content; | ||||||||||||||
private Member member; | ||||||||||||||
Comment on lines
+9
to
+11
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. title이 null이 들어오면 어떻게 될까요? |
||||||||||||||
|
||||||||||||||
public Board toEntity(Member member) { | ||||||||||||||
return Board.builder() | ||||||||||||||
.title(this.title) | ||||||||||||||
.content(this.content) | ||||||||||||||
.member(member) | ||||||||||||||
Comment on lines
+15
to
+17
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.
Suggested change
|
||||||||||||||
.build(); | ||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.jpa.board.dto.request; | ||
|
||
import com.example.jpa.member.domain.Member; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class BoardUpdateRequest { | ||
private Long id; | ||
private String title; | ||
private String content; | ||
private Member member; | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||||||||||||||||||||||||||
package com.example.jpa.board.dto.response; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
import com.example.jpa.board.domain.Board; | ||||||||||||||||||||||||||||||
import com.example.jpa.member.domain.Member; | ||||||||||||||||||||||||||||||
import com.example.jpa.reply.domain.Reply; | ||||||||||||||||||||||||||||||
import lombok.Getter; | ||||||||||||||||||||||||||||||
import lombok.Setter; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
import java.util.List; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@Getter | ||||||||||||||||||||||||||||||
public class BoardResponse { | ||||||||||||||||||||||||||||||
private Long id; | ||||||||||||||||||||||||||||||
private String title; | ||||||||||||||||||||||||||||||
private String content; | ||||||||||||||||||||||||||||||
private Long memberId; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
private String memberName; | ||||||||||||||||||||||||||||||
private List<Reply> replyList; | ||||||||||||||||||||||||||||||
Comment on lines
+13
to
+20
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.
Suggested change
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. 도메인 전체를 바깥에 노출하는건 좋지 않습니다! Reply를 따로 감싸는걸 권장드려요. |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
public BoardResponse(Long id, String title, String content, Long memberId, String memberName, List<Reply> replyList) { | ||||||||||||||||||||||||||||||
this.id = id; | ||||||||||||||||||||||||||||||
this.title = title; | ||||||||||||||||||||||||||||||
this.content = content; | ||||||||||||||||||||||||||||||
this.memberId = memberId; | ||||||||||||||||||||||||||||||
this.memberName = memberName; | ||||||||||||||||||||||||||||||
this.replyList = replyList; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
public static BoardResponse from(Board board) { | ||||||||||||||||||||||||||||||
return new BoardResponse(board.getId(), board.getTitle(), board.getContent(), board.getMember().getId(), board.getMember().getName(), board.getReplyList()); | ||||||||||||||||||||||||||||||
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. board.getMember().getId() 처럼 할경에 실행하시면 이상한 점을 느끼실 수 있을꺼에요! 직접 확인해보시고 문제점이 무엇인지 파악해보는 것을 추천드려요! 키워드는 LazyLoading 입니다. |
||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.example.jpa.board.service; | ||
|
||
import com.example.jpa.board.domain.Board; | ||
import com.example.jpa.board.dto.request.BoardCreateRequest; | ||
import com.example.jpa.board.dto.request.BoardUpdateRequest; | ||
import com.example.jpa.board.dto.response.BoardResponse; | ||
import com.example.jpa.member.domain.Member; | ||
import com.example.jpa.member.dto.response.MemberResponse; | ||
import com.example.jpa.reply.domain.Reply; | ||
import com.example.jpa.repository.BoardRepository; | ||
import com.example.jpa.repository.MemberRepository; | ||
import com.example.jpa.repository.ReplyRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.Arrays.stream; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class BoardService { | ||
private final BoardRepository boardRepository; | ||
private final ReplyRepository replyRepository; | ||
private final MemberRepository memberRepository; | ||
Comment on lines
+26
to
+28
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. 저는 개인적으로 BoardRepository를 호출할 수 있는 것은 BoardService라고 생각해요. 이 이야기는 뭐냐면 ReplyRepository에 BoardService가 직접 접근해 Reply를 가져와서 찾는 행위가 ReplyService에서 하는 것이 올바른 책임인가 생각이 듭니다. 해당 내용에 대한 논의 글도 있으니 확인해보시면 좋을 것 같아요! |
||
|
||
@Transactional | ||
public void create(BoardCreateRequest boardCreateRequest) { | ||
Member member = memberRepository.findById(boardCreateRequest.getMember().getId()) | ||
.orElseThrow(() -> new NoSuchElementException("멤버가 존재하지 않습니다.")); | ||
|
||
boardRepository.save(boardCreateRequest.toEntity(member)); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public BoardResponse findOne(Long id) { | ||
Board board = checkBoard(id); | ||
|
||
List<Reply> replyList = replyRepository.findByBoardId(id); | ||
board.setReplyList(replyList); | ||
|
||
return BoardResponse.from(board); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public Board checkBoard(Long id) { | ||
Board board = boardRepository.findById(id) | ||
.orElseThrow(() -> new NoSuchElementException("게시판 정보가 존재하지 않습니다.")); | ||
return board; | ||
} | ||
Comment on lines
+48
to
+53
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. check라는것은 일반적으로는 boolean을 반환합니다 검증용 void 메서드일 수 있고요. check 이름의 메서드지만 Board를 반환하는 것은 메서드만 보았을때 정확히 무슨 메서드인지 확인하기 어려워보입니다! |
||
|
||
@Transactional(readOnly = true) | ||
public List<BoardResponse> findAll() { | ||
List<Board> boardListTemp = boardRepository.findAll(); | ||
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. 변수명이 무엇을 뜻하는지 어렵습니다. |
||
List<BoardResponse> boardResponseList = boardListTemp.stream().map(BoardResponse::from).toList(); | ||
return boardResponseList; | ||
} | ||
|
||
@Transactional | ||
public void updateBoard(BoardUpdateRequest boardUpdateRequest) { | ||
Board board = checkBoard(boardUpdateRequest.getId()); | ||
board.update(boardUpdateRequest); | ||
} | ||
|
||
@Transactional | ||
public void deleteBoard(Long id) { | ||
Board board = checkBoard(id); | ||
|
||
List<Reply> replyList = replyRepository.findByBoardId(id); | ||
board.setReplyList(replyList); | ||
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. setter 대신 의미있는 메서드 명을 사용하면 어떨까요? |
||
|
||
boardRepository.delete(board); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.jpa.member.dto.request; | ||
|
||
import com.example.jpa.member.domain.Member; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
public class MemberUpdateRequest { | ||
|
||
private Long id; | ||
private String name; | ||
} |
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.