Skip to content

Commit

Permalink
refactor : 3차 세미나 실습 과제_CustomValidateException 이름 변경 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
PicturePark1101 committed May 1, 2024
1 parent 448350c commit e1438ea
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 21 deletions.
Binary file not shown.
Binary file modified week02/seminar/demo/.gradle/8.7/fileHashes/fileHashes.lock
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
@AllArgsConstructor
@Getter
public enum ErrorMessage {

MEMBER_NOT_FOUND_BY_ID_EXCEPTION(HttpStatus.NOT_FOUND.value(), "ID에 해당하는 사용자가 존재하지 않습니다."),
BLOG_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "ID에 해당하는 블로그가 없습니다."),
BLOG_UNAUTHORIZED(HttpStatus.NOT_FOUND.value(), "해당 블로그의 소유자가 아닙니다."),
POST_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "ID에 해당하는 포스트가 없습니다."),
;

private final int status;
private final String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,21 @@

import com.example.demo.common.dto.SuccessMessage;
import com.example.demo.common.dto.SuccessStatusResponse;
import com.example.demo.domain.Blog;
import com.example.demo.domain.Post;
import com.example.demo.service.MemberService;
import com.example.demo.service.PostService;
import com.example.demo.service.dto.blog.BlogCreateRequest;
import com.example.demo.service.dto.member.MemberCreateDto;
import com.example.demo.service.dto.post.PostCreateRequest;
import com.example.demo.service.dto.post.PostFindDto;
import com.example.demo.service.dto.post.PostListFindDto;
import jakarta.validation.Valid;
import java.net.URI;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand Down Expand Up @@ -62,4 +55,20 @@ public ResponseEntity<SuccessStatusResponse> findPostList(
));

}

@GetMapping("post/{blogId}/{postId}")
public ResponseEntity<SuccessStatusResponse> findPost(
@RequestHeader(name = "memberId") Long memberId,
@PathVariable(name = "blogId") Long blogId,
@PathVariable(name = "postId") Long postId
) {

PostFindDto post = postService.findPost(memberId, blogId, postId);

return ResponseEntity.ok(SuccessStatusResponse.of(
SuccessMessage.POST_FIND_SUCCESS,
post
));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.demo.exception;

import com.example.demo.common.dto.ErrorMessage;
import lombok.Getter;

@Getter
public class CustomValidateException extends BusinessException {

public CustomValidateException(ErrorMessage errorMessage) {
super(errorMessage);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.example.demo.domain.Blog;
import com.example.demo.domain.Member;
import com.example.demo.exception.NotFoundException;
import com.example.demo.exception.ValidateException;
import com.example.demo.exception.CustomValidateException;
import com.example.demo.repository.BlogRepository;
import com.example.demo.service.dto.blog.BlogCreateRequest;
import com.example.demo.service.dto.blog.BlogTitleUpdateRequest;
Expand Down Expand Up @@ -41,7 +41,7 @@ public void updateTitle(Long blogId, BlogTitleUpdateRequest blogTitleUpdateReque

public void validateOwner(Long requestMemberId, Long findMemberId) {
if (!requestMemberId.equals(findMemberId)) {
throw new ValidateException(ErrorMessage.BLOG_UNAUTHORIZED);
throw new CustomValidateException(ErrorMessage.BLOG_UNAUTHORIZED);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.example.demo.service;


import com.example.demo.common.dto.ErrorMessage;
import com.example.demo.domain.Blog;
import com.example.demo.domain.Post;
import com.example.demo.exception.NotFoundException;
import com.example.demo.repository.PostRepository;
import com.example.demo.service.dto.post.PostCreateRequest;
import com.example.demo.service.dto.post.PostFindDto;
Expand Down Expand Up @@ -45,4 +47,23 @@ public PostListFindDto findAllPost(Long memberId, Long blogId){

return PostListFindDto.of(postRepository.findByBlog(blog));
}

public Post findById(Long postId) {
return postRepository.findById(postId).orElseThrow(
() -> new NotFoundException(ErrorMessage.POST_NOT_FOUND)
);
}

// PostFindDto post = postService.findPost(memberId, blogId, postId);

public PostFindDto findPost(Long memberId, Long blogId, Long postId){
// blog 찾기
Blog blog = blogService.findById(blogId);
Long findMemberId = blog.getMember().getId();

// 요청한 사람이 블로그 소유주인지 확인
blogService.validateOwner(memberId, findMemberId);

return PostFindDto.of(findById(postId));
}
}

0 comments on commit e1438ea

Please sign in to comment.