Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<ErrorResponse> 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())));
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

이 컴포넌트는 어디에서 쓰이고있을까요?


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();
}
Comment on lines +31 to +34
Copy link
Contributor

Choose a reason for hiding this comment

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

요거는 어디에쓰는걸까요??

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 , "사용자가 이미 싫어요를 누른 포스트입니다.");

Comment on lines +28 to 33
Copy link
Contributor

Choose a reason for hiding this comment

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

죠습니다


private int status;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -104,14 +107,20 @@ private void updateProjectTheme(List<Theme> 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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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 = "게시글을 생성한다.")
Expand All @@ -45,7 +49,7 @@ public ResponseEntity<Void> 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){
Copy link
Contributor

Choose a reason for hiding this comment

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

조회수 추가하는데에 인증정보는 필요 없을 것으로 보입니다

postService.addViewCount(postId);
}

Expand All @@ -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")
Expand Down