diff --git a/src/main/java/com/example/yeogiserver/base/presentation/advice/GlobalControllerAdvice.java b/src/main/java/com/example/yeogiserver/base/presentation/advice/GlobalControllerAdvice.java index ed4f594..543b616 100644 --- a/src/main/java/com/example/yeogiserver/base/presentation/advice/GlobalControllerAdvice.java +++ b/src/main/java/com/example/yeogiserver/base/presentation/advice/GlobalControllerAdvice.java @@ -1,18 +1,29 @@ package com.example.yeogiserver.base.presentation.advice; import com.example.yeogiserver.common.exception.CustomException; +import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; + @RestControllerAdvice +@Slf4j public class GlobalControllerAdvice { @ExceptionHandler(RuntimeException.class) public ResponseEntity handleException(RuntimeException e) { if(e instanceof CustomException) { + + ZonedDateTime dateTime = ZonedDateTime.now(ZoneId.of("Asia/Seoul")); + + log.warn("[ERROR] TIME = {} , MESSAGE = {}" , dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) , ((CustomException) e).getErrorCode().getMessage()); + return ResponseEntity .status(((CustomException) e).getErrorCode().getStatus()) .body(new ErrorResponse(((CustomException) e).getErrorCode().getMessage() , HttpStatus.valueOf(((CustomException) e).getErrorCode().getStatus()))); diff --git a/src/main/java/com/example/yeogiserver/common/application/CommonService.java b/src/main/java/com/example/yeogiserver/common/application/CommonService.java new file mode 100644 index 0000000..33d87c0 --- /dev/null +++ b/src/main/java/com/example/yeogiserver/common/application/CommonService.java @@ -0,0 +1,35 @@ +package com.example.yeogiserver.common.application; + +import com.example.yeogiserver.common.exception.CustomException; +import com.example.yeogiserver.common.exception.ErrorCode; +import com.example.yeogiserver.member.domain.Member; +import com.example.yeogiserver.member.domain.MemberRepository; +import com.example.yeogiserver.member.repository.DefaultMemberRepository; +import com.example.yeogiserver.security.domain.CustomUserDetails; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Optional; + +@Component +@RequiredArgsConstructor +public class CommonService { + + private final DefaultMemberRepository memberRepository; + + public String getTime() { + + ZonedDateTime dateTime = ZonedDateTime.now(ZoneId.of("Asia/Seoul")); + + return dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + } + + public String getName(CustomUserDetails customUserDetails) { + Member member = memberRepository.findById(customUserDetails.getId()).orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND)); + return member.getNickname(); + } +} diff --git a/src/main/java/com/example/yeogiserver/common/exception/ErrorCode.java b/src/main/java/com/example/yeogiserver/common/exception/ErrorCode.java index 05b28c3..4c251a8 100644 --- a/src/main/java/com/example/yeogiserver/common/exception/ErrorCode.java +++ b/src/main/java/com/example/yeogiserver/common/exception/ErrorCode.java @@ -23,8 +23,13 @@ public enum ErrorCode { HEADER_REFRESH_TOKEN_NOT_EXISTS(404 , "헤더에 Refresh 토큰이 존재하지 않습니다."), //OAuth - ILLEGAL_REGISTRATION_ID(400 , "올바르지 않은 registrationId 입니다."); + ILLEGAL_REGISTRATION_ID(400 , "올바르지 않은 registrationId 입니다."), + //POST + POST_NOT_FOUND(500 , "포스트를 찾을 수 없습니다."), + NOT_MY_POST(501 , "사용자가 작성한 포스트가 아닙니다."), + MEMBER_ALREADY_LIKE_POST(502 , "사용자가 이미 좋아요를 누른 포스트입니다."), + MEMBER_ALREADY_DISLIKE_POST(503 , "사용자가 이미 싫어요를 누른 포스트입니다."); private int status; diff --git a/src/main/java/com/example/yeogiserver/post/application/PostService.java b/src/main/java/com/example/yeogiserver/post/application/PostService.java index 7deaf51..82028b4 100644 --- a/src/main/java/com/example/yeogiserver/post/application/PostService.java +++ b/src/main/java/com/example/yeogiserver/post/application/PostService.java @@ -1,5 +1,7 @@ package com.example.yeogiserver.post.application; +import com.example.yeogiserver.common.exception.CustomException; +import com.example.yeogiserver.common.exception.ErrorCode; import com.example.yeogiserver.event.recommand.RecommandEvent; import com.example.yeogiserver.member.application.MemberQueryService; import com.example.yeogiserver.member.domain.Member; @@ -33,12 +35,13 @@ public class PostService { private final ApplicationEventPublisher applicationEventPublisher; - public Post getPost(Long id) { - return postRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Post not found")); + private Post getPost(Long id) { + return postRepository.findById(id).orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND)); } public void addViewCount(Long postId){ - postRepository.addViewCount(postId); + Post post = getPost(postId); + postRepository.addViewCount(post.getId()); } public Long createPost(Long memberId, PostRequestDto postRequestDto) { @@ -67,7 +70,7 @@ public void updatePost(Long id, PostUpdateRequest postUpdateRequest, Long member Post post = getPost(id); if (!Objects.equals(post.getAuthor().getId(), memberId)) { - throw new IllegalArgumentException("Not my Post"); + throw new CustomException(ErrorCode.NOT_MY_POST); } post.updateFields(postUpdateRequest.continent(), postUpdateRequest.country(), postUpdateRequest.tripStartDate(), postUpdateRequest.tripEndDate(), postUpdateRequest.title(), postUpdateRequest.content(), postUpdateRequest.address()); @@ -104,14 +107,20 @@ private void updateProjectTheme(List themeList, Post post) { post.replaceThemeList(postThemeList); } - public void delete(Long id) { + public void delete(Long id , Long memberId) { + Post post = getPost(id); + + if (!Objects.equals(post.getAuthor().getId(), memberId)) { + throw new CustomException(ErrorCode.NOT_MY_POST); + } + postRepository.deleteById(id); } public void likePost(Long memberId, Long postId){ boolean likeExist = postRepository.isLikeExist(postId, memberId); if (likeExist){ - throw new IllegalArgumentException("Member already like this post"); + throw new CustomException(ErrorCode.MEMBER_ALREADY_LIKE_POST); } Post post = getPost(postId); @@ -122,7 +131,7 @@ public void likePost(Long memberId, Long postId){ public void dislikePost(Long memberId, Long postId){ PostLike postLike = postRepository.findPostLikeByPostIdAndMemberId(postId, memberId).orElseThrow( - () -> new IllegalArgumentException("Member has not like this post") + () -> new CustomException(ErrorCode.MEMBER_ALREADY_DISLIKE_POST) ); Post post = getPost(postId); diff --git a/src/main/java/com/example/yeogiserver/post/presentation/PostController.java b/src/main/java/com/example/yeogiserver/post/presentation/PostController.java index ab1aa83..6f2319d 100644 --- a/src/main/java/com/example/yeogiserver/post/presentation/PostController.java +++ b/src/main/java/com/example/yeogiserver/post/presentation/PostController.java @@ -1,5 +1,6 @@ package com.example.yeogiserver.post.presentation; +import com.example.yeogiserver.common.application.CommonService; import com.example.yeogiserver.post.application.PostService; import com.example.yeogiserver.post.application.dto.request.PostRequestDto; import com.example.yeogiserver.post.application.dto.request.PostUpdateRequest; @@ -8,6 +9,7 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.DeleteMapping; @@ -24,9 +26,11 @@ @RequiredArgsConstructor @SecurityRequirement(name = "Bearer Authentication") @Tag(name = "게시글 쓰기(C,U,D) 컨트롤러") +@Slf4j public class PostController { private final PostService postService; + private final CommonService commonService; @PostMapping("/posts") @Operation(description = "게시글을 생성한다.") @@ -45,7 +49,7 @@ public ResponseEntity createPost(@RequestBody PostRequestDto postRequestDt @PostMapping("/posts/{postId}/views") @Operation(description = "postId 에 해당하는 게시글의 조회수를 추가 한다.") - public void addViewCount(@PathVariable Long postId){ + public void addViewCount(@PathVariable Long postId , @AuthenticationPrincipal CustomUserDetails userDetails){ postService.addViewCount(postId); } @@ -57,8 +61,8 @@ public void updatePost(@PathVariable Long postId, @RequestBody PostUpdateRequest @DeleteMapping("/posts/{postId}") @Operation(description = "postId 에 해당하는 게시글을 삭제한다.") - public void deletePost(@PathVariable Long postId) { - postService.delete(postId); + public void deletePost(@PathVariable Long postId , @AuthenticationPrincipal CustomUserDetails userDetails) { + postService.delete(postId , userDetails.getId()); } @PostMapping("/posts/{postId}/likes")