Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
khee2 committed Jul 31, 2024
2 parents 4977b88 + 496f54b commit 84ad4a2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,16 @@ public ResponseEntity<?> getAllPosts(Authentication authentication) {
* @return 성공 시 200 OK와 함께 게시글 상세 정보를 반환하며, 실패 시 404 Not Found 또는 500 Internal Server Error를 반환합니다.
*/
@GetMapping("/{postId}")
public ResponseEntity<?> getPost(@PathVariable Long postId) {
public ResponseEntity<?> getPost(@PathVariable Long postId, Authentication authentication) {
try {
PostDetailDto postDetailDto = postService.getPostById(postId);
Member member = null;

if (!(authentication == null || !authentication.isAuthenticated())) {
String memberEmail = authentication.getName();
member = memberService.getMemberByEmail(memberEmail);
}

PostDetailDto postDetailDto = postService.getPostById(postId, member);
return ResponseEntity.ok(postDetailDto);
} catch (EntityNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("해당 게시글이 존재하지 않습니다.");
Expand Down Expand Up @@ -98,7 +105,7 @@ public ResponseEntity<?> changeLikes(@PathVariable Long postId, Authentication a
.body("해당 서비스를 이용하기 위해서는 로그인이 필요합니다.");
}

postService.getPostById(postId); // 게시글 존재 여부 확인
postService.isPostExist(postId); // 게시글 존재 여부 확인
String memberEmail = authentication.getName();
Member member = memberService.getMemberByEmail(memberEmail);

Expand Down Expand Up @@ -226,7 +233,7 @@ public ResponseEntity<?> deletePost(@PathVariable Long postId, Authentication au
.body("해당 서비스를 이용하기 위해서는 로그인이 필요합니다.");
}

postService.getPostById(postId); // 게시글 존재 여부 확인
postService.isPostExist(postId); // 게시글 존재 여부 확인

String memberEmail = authentication.getName();
Member member = memberService.getMemberByEmail(memberEmail);
Expand Down Expand Up @@ -304,7 +311,7 @@ public ResponseEntity<?> updatePost(@PathVariable Long postId,
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("사용자를 찾을 수 없습니다.");
}

int nowImageCnt = postService.getPostById(postId).getImages().size();
int nowImageCnt = postService.getPostById(postId, member).getImages().size();
postUpdateDto.validate(nowImageCnt); // 제목, 본문, 해시태그, 삭제할 이미지 검증

if (postImages != null && !postImages.isEmpty()) { // 이미지 변경이 있는 경우
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public class PostDetailDto {
private List<Long> postHashtags;
private List<String> images;
private int likeCnt;
private Boolean likes = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class PostService {
private final PostHashtagRepository postHashtagsRepository;

@Transactional(readOnly = true)
public PostDetailDto getPostById(Long postId) {
public PostDetailDto getPostById(Long postId, Member member) {
Post post = postRepository.findById(postId).orElseThrow(EntityNotFoundException::new);
String memberEmail = post.getMember().getEmail();

Expand All @@ -59,10 +59,16 @@ public PostDetailDto getPostById(Long postId) {
post.getCreateDate(),
hashtags,
images,
likeCnt
likeCnt,
likesRepository.isMemberLikesPostByEmail(post.getId(), member.getEmail())
);
}

@Transactional(readOnly = true)
public Boolean isPostExist(Long postId){
return postRepository.existsById(postId);
}

@Transactional(readOnly = true)
public List<PostResponseDto> getAllPosts(Member member) {
List<Post> posts = postRepository.findAllPostWithImagesAndPostHashtags();
Expand All @@ -73,6 +79,7 @@ public List<PostResponseDto> getAllPosts(Member member) {
if (member != null) {
postDto.setLikes(likesRepository.isMemberLikesPostByEmail(post.getId(), member.getEmail()));
}

return postDto;
})
.collect(Collectors.toList());
Expand Down

0 comments on commit 84ad4a2

Please sign in to comment.