-
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 제출합니다. #1
base: main
Are you sure you want to change the base?
Changes from all commits
6a04d86
e8b908f
eccbffe
ea9e59a
117d199
79d1cda
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,49 @@ | ||||||
package com.example.jpa.board.controller; | ||||||
|
||||||
import com.example.jpa.board.dto.request.BoardUpdateRequest; | ||||||
import com.example.jpa.board.dto.request.BoardCreateRequest; | ||||||
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/boards") | ||||||
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
|
||||||
@RequiredArgsConstructor | ||||||
public class BoardController { | ||||||
|
||||||
private final BoardService boardService; | ||||||
|
||||||
@PostMapping | ||||||
public ResponseEntity<Void> createBoard(@RequestBody BoardCreateRequest boardCreateRequest) { | ||||||
boardService.createBoard(boardCreateRequest); | ||||||
return ResponseEntity.ok().build(); | ||||||
} | ||||||
|
||||||
@GetMapping("/{id}") | ||||||
public ResponseEntity<BoardResponse> retrieveBoardById(@PathVariable("id") Long id) { | ||||||
BoardResponse boardresponse = boardService.retrieveBoardById(id); | ||||||
return ResponseEntity.ok(boardresponse); | ||||||
} | ||||||
|
||||||
@GetMapping | ||||||
public ResponseEntity<List<BoardResponse>> retrieveBoards() { | ||||||
return ResponseEntity.ok(boardService.retrieveBoards()); | ||||||
} | ||||||
|
||||||
@DeleteMapping("/{id}") | ||||||
public ResponseEntity<Void> deleteBoard(@PathVariable("id") Long id) { | ||||||
boardService.deleteBoard(id); | ||||||
return ResponseEntity.ok().build(); | ||||||
} | ||||||
|
||||||
@PutMapping | ||||||
public ResponseEntity<Void> updateBoard(@RequestBody BoardUpdateRequest boardUpdateRequest) { | ||||||
boardService.updateBoard(boardUpdateRequest); | ||||||
return ResponseEntity.ok().build(); | ||||||
} | ||||||
gyuchangShim marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
gyuchangShim marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,49 @@ | ||||||
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.time.LocalDate; | ||||||
import java.util.ArrayList; | ||||||
import java.util.List; | ||||||
|
||||||
@Entity | ||||||
@Getter | ||||||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||||||
public class Board { | ||||||
gyuchangShim marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
// @Builder - 생성자를 통해 반환하기 때문에 필요 X | ||||||
// @Setter - update 메서드를 통해 수정하기 때문에 필요 X | ||||||
|
||||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||||||
@Column(name = "board_id") | ||||||
private Long id; | ||||||
|
||||||
@ManyToOne(fetch = FetchType.LAZY) | ||||||
@JoinColumn(name = "member_id") | ||||||
private Member writer; | ||||||
|
||||||
@OneToMany(mappedBy = "board", orphanRemoval = true) | ||||||
private List<Reply> replyList = new ArrayList<>(); | ||||||
Comment on lines
+29
to
+30
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.
|
||||||
|
||||||
private String title; | ||||||
private String content; | ||||||
private LocalDate date; | ||||||
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. createdDate, UpdatedDate같은 명확한 네이밍 가져가기
Suggested change
|
||||||
|
||||||
@Builder | ||||||
public Board(Member writer, String title, String content, LocalDate date) { | ||||||
this.writer = writer; | ||||||
this.title = title; | ||||||
this.content = content; | ||||||
this.date = date; | ||||||
} | ||||||
|
||||||
public void update(BoardUpdateRequest boardUpdateRequest) { | ||||||
this.title = boardUpdateRequest.getTitle(); | ||||||
this.content = boardUpdateRequest.getContent(); | ||||||
this.date = LocalDate.now(); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.jpa.board.dto.request; | ||
|
||
import com.example.jpa.board.domain.Board; | ||
import com.example.jpa.member.domain.Member; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Getter; | ||
|
||
import java.time.LocalDate; | ||
|
||
@Getter | ||
public class BoardCreateRequest { | ||
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.
|
||
|
||
@NotBlank | ||
private Long id; | ||
|
||
@NotBlank | ||
private String title; | ||
|
||
@NotBlank | ||
private String content; | ||
|
||
public Board toEntity(Member writer) { | ||
return Board.builder() | ||
.writer(writer) | ||
.title(this.title) | ||
.content(this.content) | ||
.date(LocalDate.now()) | ||
.build(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.jpa.board.dto.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class BoardUpdateRequest { | ||
|
||
@NotBlank | ||
private Long id; | ||
|
||
@NotBlank | ||
private String title; | ||
|
||
@NotBlank | ||
private String content; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.example.jpa.board.dto.response; | ||
|
||
import com.example.jpa.board.domain.Board; | ||
import lombok.Getter; | ||
|
||
import java.time.LocalDate; | ||
|
||
@Getter | ||
public class BoardResponse { | ||
|
||
private Long id; | ||
private String memberName; | ||
|
||
private String title; | ||
private String content; | ||
private LocalDate date; | ||
|
||
public BoardResponse(Long id, String memberName, String title, String content, LocalDate date) { | ||
this.id = id; | ||
this.memberName = memberName; | ||
this.title = title; | ||
this.content = content; | ||
this.date = date; | ||
} | ||
|
||
public static BoardResponse from(Board board) { | ||
return new BoardResponse(board.getId(), board.getWriter().getName(), board.getTitle(), board.getContent(), board.getDate()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.example.jpa.board.repository; | ||
|
||
import com.example.jpa.board.domain.Board; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface BoardRepository extends JpaRepository<Board, Long> { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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.member.domain.Member; | ||
import com.example.jpa.member.service.MemberService; | ||
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 | ||
@Transactional(readOnly = true) | ||
public class BoardService { | ||
|
||
private final BoardRepository boardRepository; | ||
private final MemberService memberService; | ||
|
||
@Transactional | ||
gyuchangShim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public void createBoard(BoardCreateRequest boardCreateRequest) { | ||
Member targetMember = memberService.checkExist(boardCreateRequest.getId()); | ||
boardRepository.save(boardCreateRequest.toEntity(targetMember)); | ||
} | ||
|
||
public BoardResponse retrieveBoardById(Long id) { | ||
return BoardResponse.from(checkExist(id)); | ||
} | ||
|
||
public List<BoardResponse> retrieveBoards() { | ||
return boardRepository.findAll().stream().map(BoardResponse::from).collect(Collectors.toList()); | ||
} | ||
|
||
@Transactional | ||
public void deleteBoard(Long id) { | ||
boardRepository.delete(checkExist(id)); | ||
} | ||
|
||
@Transactional | ||
public void updateBoard(BoardUpdateRequest boardUpdateRequest) { | ||
Board targetBoard = checkExist(boardUpdateRequest.getId()); | ||
targetBoard.update(boardUpdateRequest); | ||
} | ||
|
||
public Board checkExist(Long id) { | ||
Board targetBoard = boardRepository.findById(id) | ||
.orElseThrow(() -> new NoSuchElementException("게시판이 존재하지 않습니다.")); | ||
return targetBoard; | ||
} | ||
Comment on lines
+51
to
+55
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. chekcExist라면 boolean 응답을 기대할 것 같음. get이나 fetch로 쓰는게 적절할듯 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,9 @@ | ||
package com.example.jpa.exception; | ||
|
||
public class DuplicateMemberException extends Exception{ | ||
|
||
public DuplicateMemberException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
package com.example.jpa.member.controller; | ||
|
||
import com.example.jpa.member.dto.request.MemberCreateRequest; | ||
import com.example.jpa.member.dto.request.MemberUpdateRequest; | ||
import com.example.jpa.member.dto.response.MemberResponse; | ||
import com.example.jpa.member.service.MemberService; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
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.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.*; | ||
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. 전체 import 지양하기 |
||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("api/members") | ||
|
@@ -22,9 +20,7 @@ public class MemberController { | |
|
||
@PostMapping | ||
public ResponseEntity<Void> create(@RequestBody MemberCreateRequest memberCreateRequest) { | ||
|
||
memberService.create(memberCreateRequest); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
|
@@ -35,4 +31,21 @@ public ResponseEntity<MemberResponse> findOne(@PathVariable("id") Long id) { | |
|
||
return ResponseEntity.ok(memberResponse); | ||
} | ||
|
||
@GetMapping() | ||
public ResponseEntity<List<MemberResponse>> retrieveMembers() { | ||
return ResponseEntity.ok(memberService.retrieveMembers()); | ||
} | ||
|
||
@PutMapping() | ||
public ResponseEntity<Void> updateMember(@RequestBody MemberUpdateRequest memberUpdateRequest) { | ||
memberService.updateMember(memberUpdateRequest); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deleteMember(@PathVariable("id") Long id) { | ||
memberService.deleteMember(id); | ||
return ResponseEntity.ok().build(); | ||
} | ||
} |
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.
@RestContoller
와@Controller
알아보기!