Skip to content
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

Open
wants to merge 6 commits into
base: Seoneui
Choose a base branch
from

Conversation

sunnny619
Copy link

@sunnny619 sunnny619 commented May 26, 2024

Description

기존 게시글 조회 코드에서 게시글 생성 코드 추가하였습니다.

@sunnny619 sunnny619 changed the title [2주차] : 지선의 게시글 생성 추가 [2주차] 지선의 : 게시글 생성 api May 26, 2024
@sunnny619 sunnny619 self-assigned this May 26, 2024
@sunnny619 sunnny619 requested a review from Gusionling May 27, 2024 00:39
@Transactional
public PostDto createPost(PostDto postDto) {
Post postEntity = PostDto.toEntity(postDto);
postEntity.setCreatedAt(LocalDateTime.now());

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 객체 자체가 책임 지면 좋을 것 같아요!

Copy link
Author

@sunnny619 sunnny619 May 27, 2024

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 클래스에 추가하여 엔티티에서 추가할 수 있도록 수정하였습니다.

Comment on lines +23 to +34
@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));
}
Copy link
Collaborator

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 수로 자동 포맷팅을 해줍니다:)

Comment on lines +20 to +21
public class PostCreateController {
private final PostService postService;
Copy link
Collaborator

Choose a reason for hiding this comment

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

게시글 컨트롤러가 2개라 유지보수하기 힘들거 같아서 합쳐주시면 좋을 것 같아요

Comment on lines +20 to +28
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();
}
Copy link
Collaborator

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.*;
Copy link
Collaborator

Choose a reason for hiding this comment

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

* 로 Import는 아시겠지만 지양하는게 좋습니다. 이것도 인텔리제이 import 설정에서 무조건 필요한애만 가져오게 바꿀 수 있어요

Comment on lines +31 to +38
public static Post toEntity(PostDto postDto) {
return Post.builder()
.title(postDto.getTitle())
.content(postDto.getContent())
.createdAt(postDto.getCreatedAt())
.updatedAt(postDto.getUpdatedAt())
.build();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

엔티티로 만드는 친구니까 엔티티 클래스로 옮기면 더 좋을듯!

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