-
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 제출입니다. #6
base: main
Are you sure you want to change the base?
Changes from all commits
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,3 @@ | ||
<img width="769" alt="스크린샷 2024-01-03 오후 4 01 30" src="https://github.com/Erichong7/jpa-practice/assets/97429550/5cfd774e-2d59-419e-92e1-647043f22e1c"> | ||
|
||
api 문서 : https://documenter.getpostman.com/view/29836884/2s9YsFEEBq | ||
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.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.*; | ||
|
||
@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); | ||
} | ||
|
||
@PatchMapping("/{id}") | ||
public ResponseEntity<Void> update(@PathVariable("id") Long id, @RequestBody BoardUpdateRequest boardUpdateRequest) { | ||
|
||
boardService.update(id, boardUpdateRequest); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> delete(@PathVariable("id") Long id) { | ||
|
||
boardService.delete(id); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
} | ||
Comment on lines
+1
to
+49
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. 게시글 전체 조회 api가 없는 것 같아요 !! ㅠ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.example.jpa.board.domain; | ||
|
||
import com.example.jpa.comment.domain.Comment; | ||
import com.example.jpa.member.domain.Member; | ||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Board { | ||
@Id | ||
@GeneratedValue | ||
@Column(name = "board_id") | ||
private Long id; | ||
|
||
private String title; | ||
|
||
private String body; | ||
|
||
@OneToMany(mappedBy = "board", cascade = CascadeType.REMOVE) | ||
private List<Comment> comments = new ArrayList<>(); | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id") | ||
private Member member; | ||
|
||
@Builder | ||
public Board(Member member, String title, String body) { | ||
this.member = member; | ||
this.title = title; | ||
this.body = body; | ||
} | ||
|
||
public void update(String title, String body) { | ||
this.title = title; | ||
this.body = body; | ||
} | ||
} | ||
Comment on lines
+40
to
+45
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. 변경 감지 굿입니다 |
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 Long member; | ||
private String title; | ||
private String body; | ||
Comment on lines
+7
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. 제가 말씀드리진 않았지만, java 17을 사용하시니 record에 대해서 알아보시고 적용해 보는 것도 좋을 것 같아요! |
||
|
||
public Board toEntity(Member member) { | ||
return Board.builder() | ||
.member(member) | ||
.title(this.title) | ||
.body(this.body) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.example.jpa.board.dto.request; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class BoardUpdateRequest { | ||
private String title; | ||
private String body; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.jpa.board.dto.response; | ||
|
||
import com.example.jpa.board.domain.Board; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
public class BoardResponse { | ||
private Long id; | ||
private String title; | ||
private String name; | ||
private String body; | ||
private List<String> comments; | ||
|
||
public BoardResponse(Long id, String title, String name, String body, List<String> comments) { | ||
this.id = id; | ||
this.title = title; | ||
this.name = name; | ||
this.body = body; | ||
this.comments = comments; | ||
} | ||
|
||
public static BoardResponse from(Board board, String name, List<String> comments) { | ||
return new BoardResponse(board.getId(), board.getTitle(), name, board.getBody(), comments); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.example.jpa.board.repository; | ||
|
||
import com.example.jpa.board.domain.Board; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface BoardRepository extends JpaRepository<Board, Long> { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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.board.repository.BoardRepository; | ||
import com.example.jpa.comment.domain.Comment; | ||
import com.example.jpa.comment.dto.request.CommentCreateRequest; | ||
import com.example.jpa.comment.repository.CommentRepository; | ||
import com.example.jpa.member.domain.Member; | ||
import com.example.jpa.member.repository.MemberRepository; | ||
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; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class BoardService { | ||
|
||
private final BoardRepository boardRepository; | ||
private final MemberRepository memberRepository; | ||
private final CommentRepository commentRepository; | ||
|
||
@Transactional | ||
public void create(BoardCreateRequest boardCreateRequest) { | ||
Member member = memberRepository.findById(boardCreateRequest.getMember()).get(); | ||
boardRepository.save(boardCreateRequest.toEntity(member)); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public BoardResponse findOne(Long id) { | ||
Board board = boardRepository.findById(id) | ||
.orElseThrow(() -> new NoSuchElementException("게시글이 존재하지 않습니다.")); | ||
List<String> comments = commentRepository.findByBoardId(id) | ||
.stream() | ||
.map(Comment::getBody) | ||
.collect(Collectors.toList()); | ||
|
||
return BoardResponse.from(board, board.getMember().getName(), comments); | ||
} | ||
|
||
@Transactional | ||
public void update(Long id, BoardUpdateRequest boardUpdateRequest) { | ||
Board board = boardRepository.findById(id) | ||
.orElseThrow(() -> new NoSuchElementException("게시글이 존재하지 않습니다.")); | ||
board.update(boardUpdateRequest.getTitle(), boardUpdateRequest.getBody()); | ||
} | ||
|
||
@Transactional | ||
public void delete(Long id) { | ||
boardRepository.deleteById(id); | ||
} | ||
} | ||
Comment on lines
+54
to
+58
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. 삭제에 대한 예외처리도 필요할 것 같아요 ! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.example.jpa.comment.controller; | ||
|
||
import com.example.jpa.comment.dto.request.CommentCreateRequest; | ||
import com.example.jpa.comment.dto.request.CommentUpdateRequest; | ||
import com.example.jpa.comment.dto.response.CommentResponse; | ||
import com.example.jpa.comment.service.CommentService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/comment") | ||
public class CommentController { | ||
|
||
private final CommentService commentService; | ||
|
||
@PostMapping("/create") | ||
public ResponseEntity<Void> create(@RequestBody CommentCreateRequest commentCreateRequest) { | ||
Long saveResult = commentService.create(commentCreateRequest); | ||
if (saveResult != null) { | ||
return new ResponseEntity<>(HttpStatus.OK); | ||
} else { | ||
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | ||
} | ||
} | ||
Comment on lines
+21
to
+29
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. 여기서 에러 대신에 널 체킹으로 빼는 이유는 무엇인가요>?? |
||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<CommentResponse> findOne(@PathVariable("id") Long id) { | ||
|
||
CommentResponse commentResponse = commentService.findOne(id); | ||
|
||
return ResponseEntity.ok(commentResponse); | ||
} | ||
|
||
@PatchMapping("/{id}") | ||
public ResponseEntity<Void> update(@PathVariable("id") Long id, @RequestBody CommentUpdateRequest commentUpdateRequest) { | ||
|
||
commentService.update(id, commentUpdateRequest); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> delete(@PathVariable("id") Long id) { | ||
|
||
commentService.delete(id); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<List> findAll() { | ||
|
||
List<CommentResponse> commentResponses = commentService.findAll(); | ||
|
||
return ResponseEntity.ok(commentResponses); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.example.jpa.comment.domain; | ||
|
||
import com.example.jpa.board.domain.Board; | ||
import com.example.jpa.member.domain.Member; | ||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Comment { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id") | ||
private Member member; | ||
|
||
@Column | ||
private String body; | ||
|
||
/* Board:Comment = 1:N */ | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "board_id") | ||
private Board board; | ||
|
||
@Builder | ||
public Comment(Member member, String body, Board board) { | ||
this.member = member; | ||
this.body = body; | ||
this.board = board; | ||
} | ||
|
||
public void update(String body) { | ||
this.body = body; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.example.jpa.comment.dto.request; | ||
|
||
import com.example.jpa.board.domain.Board; | ||
import com.example.jpa.comment.domain.Comment; | ||
import com.example.jpa.member.domain.Member; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CommentCreateRequest { | ||
private Long member; | ||
private Long board; | ||
private String body; | ||
|
||
public Comment toEntity(Member member, Board board) { | ||
return Comment.builder() | ||
.member(member) | ||
.board(board) | ||
.body(this.body) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.example.jpa.comment.dto.request; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CommentUpdateRequest { | ||
private String body; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.example.jpa.comment.dto.response; | ||
|
||
import com.example.jpa.comment.domain.Comment; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CommentResponse { | ||
|
||
private Long id; | ||
private String body; | ||
|
||
public CommentResponse(Long id, String body) { | ||
this.id = id; | ||
this.body = body; | ||
} | ||
|
||
public static CommentResponse from(Comment comment) { | ||
return new CommentResponse(comment.getId(), comment.getBody()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.example.jpa.comment.repository; | ||
|
||
import com.example.jpa.comment.domain.Comment; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
List<Comment> findByBoardId(Long boardId); | ||
} |
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.
일반적으로 api에서 도메인을 복수형으로 사용할 것을 권장드립니다!!!