-
Notifications
You must be signed in to change notification settings - Fork 0
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
Third seminar 과제 제출 #6
base: main
Are you sure you want to change the base?
Changes from 1 commit
df2f546
8c2f0f7
fa60099
57b759d
42be135
43d41c9
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,28 @@ | ||
package sopt.org.ThirdSeminar.controller.post; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
import sopt.org.ThirdSeminar.common.dto.ApiResponseDto; | ||
import sopt.org.ThirdSeminar.controller.post.dto.request.PostCreateDto; | ||
import sopt.org.ThirdSeminar.exception.SuccessStatus; | ||
import sopt.org.ThirdSeminar.service.PostService; | ||
|
||
import javax.validation.Valid; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class PostController { | ||
|
||
private final PostService postService; | ||
|
||
@PostMapping("/post/{userId}") | ||
public ApiResponseDto create(@PathVariable final Long userId, @RequestBody @Valid final PostCreateDto postCreateDto) { | ||
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. P5) 저는 userId를 request dto에 포함시켰는데, path variable로 따로 받아오게 하셨군요! 혹시 이렇게 하신 이유가 무엇인지 궁금해요! 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. 사실 큰 의미는 없고,,보통 유저는 인증 토큰 정보? 를 이용해서 userId를 찾아오는 방식을 썼었는데, 아직 유저 인증을 구현을 안 했다 보니 그냥 @PathVariable로 받아왔어요! 근데 이렇게 보니 post/1 이런식으로 있으면 이게 userId인지 postId인지 혼동이 올 수 있을 것 같긴 하네요.. |
||
return ApiResponseDto.success(SuccessStatus.POST_CREATE_SUCCESS, postService.create(userId, postCreateDto)); | ||
} | ||
|
||
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. 이 코드 게시물 생성 api 인가요? userid를 뒤에 추가해서 생성 하는 방법도 있군요..! 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. 위에 동섭이 코멘트에 답변한 거처럼 사실 큰 의미는 없고,,보통 유저는 인증 토큰 정보? 를 이용해서 userId를 찾아오는 방식을 썼었는데, 아직 유저 인증을 구현을 안 했다 보니 그냥 @PathVariable로 받아왔어요! 근데 이렇게 보니 post/1 이런식으로 있으면 이게 userId인지 postId인지 혼동이 올 수 있을 것 같긴 하네요.. |
||
@GetMapping("/post/{postId}") | ||
public ApiResponseDto getOne(@PathVariable final Long postId) { | ||
return ApiResponseDto.success(SuccessStatus.POST_INQUIRY_SUCCESS, postService.getOne(postId)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package sopt.org.ThirdSeminar.controller.post.dto.request; | ||
|
||
import lombok.Getter; | ||
|
||
import javax.validation.constraints.NotEmpty; | ||
|
||
@Getter | ||
public class PostCreateDto { | ||
|
||
@NotEmpty(message = "제목은 필수입니다.") | ||
private String title; | ||
|
||
@NotEmpty(message = "내용은 필수입니다.") | ||
private String content; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package sopt.org.ThirdSeminar.controller.post.dto.response; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class PostResponseDto { | ||
private String title; | ||
private String content; | ||
private String authorName; | ||
|
||
public static PostResponseDto of(String title, String content, String authorName) { | ||
return new PostResponseDto(title, content, authorName); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package sopt.org.ThirdSeminar.infrastructure; | ||
|
||
import org.springframework.stereotype.Repository; | ||
import sopt.org.ThirdSeminar.domain.Post; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface PostRepository extends org.springframework.data.repository.Repository<Post, Long> { | ||
|
||
void save(Post post); | ||
|
||
Optional<Post> findById(Long postId); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package sopt.org.ThirdSeminar.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import sopt.org.ThirdSeminar.controller.post.dto.request.PostCreateDto; | ||
import sopt.org.ThirdSeminar.controller.post.dto.response.PostResponseDto; | ||
import sopt.org.ThirdSeminar.domain.Post; | ||
import sopt.org.ThirdSeminar.domain.User; | ||
import sopt.org.ThirdSeminar.infrastructure.PostRepository; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PostService { | ||
|
||
private final PostRepository postRepository; | ||
private final UserService userService; | ||
|
||
@Transactional | ||
public PostResponseDto create(Long userId, PostCreateDto postCreateDto) { | ||
User userById = userService.findUserById(userId); | ||
|
||
Post post = Post.builder() | ||
.title(postCreateDto.getTitle()) | ||
.content(postCreateDto.getContent()) | ||
.user(userById) | ||
.build(); | ||
|
||
postRepository.save(post); | ||
|
||
return PostResponseDto.of(post.getTitle(), post.getContent(), userById.getNickname()); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public PostResponseDto getOne(Long postId) { | ||
Post post = postRepository.findById(postId).orElseThrow(); | ||
|
||
return PostResponseDto.of(post.getTitle(), post.getContent(), post.getUser().getNickname()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,8 @@ | |
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import sopt.org.ThirdSeminar.controller.dto.request.UserRequestDto; | ||
import sopt.org.ThirdSeminar.controller.dto.response.UserResponseDto; | ||
import sopt.org.ThirdSeminar.controller.user.dto.request.UserRequestDto; | ||
import sopt.org.ThirdSeminar.controller.user.dto.response.UserResponseDto; | ||
import sopt.org.ThirdSeminar.domain.User; | ||
import sopt.org.ThirdSeminar.infrastructure.UserRepository; | ||
|
||
|
@@ -26,4 +26,8 @@ public UserResponseDto create(UserRequestDto request) { | |
|
||
return UserResponseDto.of(user.getId(), user.getNickname()); | ||
} | ||
|
||
public User findUserById(Long userId) { | ||
return userRepository.findById(userId).orElseThrow(); | ||
} | ||
} | ||
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. orElseThrow 로 바로 날리는 방법도 있는 거 알아갑니다..! |
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.
폴더 구조를 아예 도메인 별로 나눴네용~