Skip to content

4 Backend chanul#25

Open
chanul0817 wants to merge 1 commit into
Backendfrom
4-Backend-chanul
Open

4 Backend chanul#25
chanul0817 wants to merge 1 commit into
Backendfrom
4-Backend-chanul

Conversation

@chanul0817

Copy link
Copy Markdown

💡 개요

  • 마인드웨이 과제 수행

📃 작업내용

  • CRUD기능을 가진 Board구현

🔀 변경사항

🙋‍♂️ 질문사항

💀 어려웠던 점

import java.util.List;

@Service
@AllArgsConstructor

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AllArgsConstructor는 모든 필드 값을 파라미터로 받는 생성자를 생성 어노테이션이라 서비스 로직에서 사용하기에 적합하지 않아보여요

}

@PutMapping("/{id}")
public Long updateBoard(@PathVariable Long id, @RequestBody BoardRequestDto requestDto) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

board를 수정하는 메서드 인데 반환하는 값이 Long인 이유가 있나요?

public class BoardController {
private final BoardService boardService;

public BoardController(BoardService boardService) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자를 만들어서 BoardService를 사용하는 것이 아닌 @RequiredArgsConstructor를 통한 의존성 주입을 통해 boardService를 사용하는게 더 좋을거 같아요

@Entity(name = "boards") // DB 테이블 이름을 "boards"로 설정
public class Board {
// 글 고유 아이디
@GeneratedValue(strategy = GenerationType.AUTO)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 기본키 생성 전략을 GenerationType.AUTO로 해두어 하이버네이트가 자동으로 전략을 선택하게끔 됬는데 DB 버전에 따라 선택되는 전략이 달라질 수 있으므로 사용하는 DBMS에 맞는 전략을 사용하시면 좋을거 같아요

@Transactional
public Long deleteBoard(Long id) {
boardRepository.deleteById(id);
return id;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

삭제하는 메서드에서 삭제한 board의 id를 다시 반환하는 이유가 있나요?


@Transactional
public Long deleteBoard(Long id) {
boardRepository.deleteById(id);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

받아온 id를 바로 삭제하는게 아니라 이 board의 id가 있는지 확인하는 로직이 있으면 좋을 것 같아요

@@ -0,0 +1,5 @@
spring.datasource.url=jdbc:mysql://localhost:3306/boardmysql?serverTimezone=Asia/Seoul&useSSL=false&allowPublicKeyRetrieval=true

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

많은 프로젝트에서 application.properties 파일보다 application.yml파일을 더 많이 사용해요
그래서 yml 파일 사용해보는것도 좋을거 같아요

@@ -0,0 +1,5 @@
spring.datasource.url=jdbc:mysql://localhost:3306/boardmysql?serverTimezone=Asia/Seoul&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

username이나 password 같은 민감한 정보는 외부에 노출되면 안되기 때문에 커밋을 할때 환경변수 처리해서 올려주세요

}

@PostMapping("/")
public BoardResponseDto createBoard(@RequestBody BoardRequestDto requestDto) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

컨트롤러에서 반환할때 ResponseEntity 사용해서 반환값과 상태 코드를 함께 반환하면 좋을거 같아요

ResponseEntity는 적절한 상태 코드와 응답 헤더 및 응답 본문을 생성하여 클라이언트에 전달하기 위해 사용합니다

return boardService.findOneBoard(id);
}

@PutMapping("/{id}")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PutMapping보다 @PatchMapping이 나아보입니다.
기본적인 http 메서드에 대해 알아보시는걸 추천드려요


@Repository
public interface BoardRepository extends JpaRepository<Board, Long> {
List<BoardListResponseDto> findAllByOrderByModifiedAtDesc();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 메서드 사용하지 않고 있는데 넣으신 이유가 있나요?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

로컬에서 실행했을 때 ModifiedAt이라는 필드가 없지만 여기서 ModifiedAt이라는 필드를 기준으로 정렬하려해서 오류가 터져 실행이 되지 않습니다

@ta2ye0n ta2ye0n left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

글 작성과 글 수정할 때 같은 dto를 사용하고 있는데 분리해주세요

ex) 글작성 -> BoardWriteRequestDto 글수정 -> BoardUpdateRequestDto (이건 예시입니다 참고해서 수정해주세요)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants