-
Notifications
You must be signed in to change notification settings - Fork 1
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
[2주차] 지선의 : 게시글 생성 api #6
base: Seoneui
Are you sure you want to change the base?
The head ref may contain hidden characters: "feat/2\uC8FC\uCC28"
Conversation
@Transactional | ||
public PostDto createPost(PostDto postDto) { | ||
Post postEntity = PostDto.toEntity(postDto); | ||
postEntity.setCreatedAt(LocalDateTime.now()); |
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.
CreatedAt과 setCreatedAt이 서비스 계층에서 setting 되고 있는데 entity 단에서 설정을 하지 않고 서비스 계층에서 설정한 이유는 무엇일까요? 저는 개인적으로 entity 객체 자체가 책임 지면 좋을 것 같아요!
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.
게시글 생성할때 CreatedAt과 setCreatedAt을 전달해주는 방법 중 하나로 단순하게 서비스 계층에서 전달해주면 된다 생각해서 짰던 것 같습니다.
@PrePersist
protected void onCreate() {
this.createdAt = LocalDateTime.now();
this.updatedAt = LocalDateTime.now();
}
조언을 참고하여 다음을 entity인 Post 클래스에 추가하여 엔티티에서 추가할 수 있도록 수정하였습니다.
@Operation( | ||
summary = "단일 게시글 search", | ||
responses = { | ||
@ApiResponse(responseCode = "200", description = "ok"), | ||
@ApiResponse(responseCode = "500", description = "INTERNAL_SERVER_ERROR") | ||
} | ||
) | ||
@GetMapping("/{id}") | ||
public Response<GetPostResponse> getPosts(@PathVariable("id") Long id) { | ||
PostDto postDto = postService.getPostById(id); | ||
return Response.data(GetPostResponse.from(postDto)); | ||
} |
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.
@Operation
안은 indent가 8갠데 함수 안은 4개네요. 4자로 일정하면 좋을 것 같아요. 인텔리제이 Actions on save 설정하면 설정한 indent 수로 자동 포맷팅을 해줍니다:)
public class PostCreateController { | ||
private final PostService postService; |
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.
게시글 컨트롤러가 2개라 유지보수하기 힘들거 같아서 합쳐주시면 좋을 것 같아요
public static CreatePostResponse from(PostDto postDto) { | ||
return CreatePostResponse.builder() | ||
.id(postDto.getId()) | ||
.title(postDto.getTitle()) | ||
.content(postDto.getContent()) | ||
.createdAt(postDto.getCreatedAt()) | ||
.updatedAt(postDto.getUpdatedAt()) | ||
.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.
여기도 indent!
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.*; |
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.
*
로 Import는 아시겠지만 지양하는게 좋습니다. 이것도 인텔리제이 import 설정에서 무조건 필요한애만 가져오게 바꿀 수 있어요
public static Post toEntity(PostDto postDto) { | ||
return Post.builder() | ||
.title(postDto.getTitle()) | ||
.content(postDto.getContent()) | ||
.createdAt(postDto.getCreatedAt()) | ||
.updatedAt(postDto.getUpdatedAt()) | ||
.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.
엔티티로 만드는 친구니까 엔티티 클래스로 옮기면 더 좋을듯!
Description
기존 게시글 조회 코드에서 게시글 생성 코드 추가하였습니다.