-
Notifications
You must be signed in to change notification settings - Fork 0
[feat] 다솜소식 API 구현 #34
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
Conversation
| @Setter | ||
| @Entity | ||
| @Schema(description = "뉴스 엔티티") | ||
| public class NewsEntity { |
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.
BaseEntity 상속받아서 생성일, 수정일, 상태 필드 구성하면 될 것 같습니다
|
|
||
| public List<NewsResponseDto> getAllNews() { | ||
| return newsRepository.findAll().stream() | ||
| .map(news -> new NewsResponseDto(news.getId(), news.getTitle(), news.getContent(), news.getCreatedAt(), news.getImageUrl())) |
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.
News 엔티티 클래스 내부에 Dto 변환 메소드를 구현하면 더 깔끔하게 구현 가능합니다
Applicant 메소드 참고해보세용
| } | ||
|
|
||
| public NewsResponseDto createNews(NewsRequestDto requestDto) { | ||
| NewsEntity news = new NewsEntity(); |
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.
setter를 이용해 객체 필드를 초기화하는 것보단 생성자나 Builder 패턴을 이용해서 인스턴스 생성과 동시에 초기화 하는 것이 더 적절합니다.
setter를 쓰게 되면 인스턴스 생성 후 객체 내부 상태를 변경하는 행위가 되는 것인데, 인스턴스 생성 이외의 상태 변경은 별도의 update 메소드 같이 도메인 로직 내에서 명시적으로 허용된 경우에만 이루어져야합니다.
| news.setCreatedAt(LocalDateTime.now()); | ||
|
|
||
| NewsEntity savedNews = newsRepository.save(news); | ||
| return new NewsResponseDto(savedNews.getId(), savedNews.getTitle(), savedNews.getContent(), savedNews.getCreatedAt(), savedNews.getImageUrl()); |
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.
마찬가지로 Entity -> Dto 변환 메소드를 엔티티 내부에 구현하면 깔끔합니다
| private LocalDateTime createdAt; | ||
|
|
||
| @Schema(description = "뉴스 이미지 URL", example = "http://example.com/image.jpg") | ||
| private String imageUrl; |
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.
이미지는 외부 DB에 저장하는 건가요?
|
|
||
| @Tag(name = "NEWS API", description = "다솜소식 API") | ||
| @RestController | ||
| @RequestMapping("/news") |
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 로 시작하는 걸로 통일할게용
(ex. /api/news)
| @Operation(summary = "소식 등록", description = "새로운 소식을 등록") | ||
| @ApiResponses(value = { | ||
| @ApiResponse(responseCode = "201", description = "생성 완료"), | ||
| @ApiResponse(responseCode = "400", description = "유효하지 않은 요청 데이터", |
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.
Request Body -> NewsRequestDto 역직렬화 과정에서 필드 값 Validation을 하기 위해서는 NewsRequestDto 선언부 앞에 @Valid 어노테이션을 추가하고 NewsRequestDto의 각 필드에 제약 조건을 설정하면 됩니다. ApplicantCreateRequestDto를 참고해보세요
|
|
||
| @Schema(description = "소식 ID", example = "1") | ||
| private Long id; | ||
| private final Long id; |
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.
DTO를 불변 객체로 만들 필요는 없으므로 필드에 final 키워드는 빼는 것이 좋아보입니다
| import lombok.*; | ||
|
|
||
| @Getter | ||
| @Setter |
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.
Setter를 사용하는 곳이 없다면 어노테이션은 제거해주세요
|
GOOD 👍 |
[feat] 다솜소식 API 구현
Issue
변경 내용
구현 사항
테스트 (필요 시)