Skip to content

Commit

Permalink
refactor : 3차 세미나 리뷰 반영 리팩토 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
PicturePark1101 committed May 30, 2024
1 parent 310f238 commit f688898
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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(), "해당 블로그의 소유자가 아닙니다."),
BLOG_UNAUTHORIZED(HttpStatus.FORBIDDEN.value(), "해당 블로그의 소유자가 아닙니다."),
POST_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "ID에 해당하는 포스트가 없습니다."),
JWT_UNAUTHORIZED_EXCEPTION(HttpStatus.UNAUTHORIZED.value(), "사용자의 로그인 검증을 실패했습니다."),
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static SuccessStatusResponse of(SuccessMessage successMessage) {
return new SuccessStatusResponse(successMessage.getStatus(), successMessage.getMessage(), null);
}

public static <T> SuccessStatusResponse of(SuccessMessage successMessage, T responseDto) {
public static <T> SuccessStatusResponse<T> of(SuccessMessage successMessage, T responseDto) {
return new SuccessStatusResponse(successMessage.getStatus(), successMessage.getMessage(), responseDto);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
@RequestMapping("/api/v1/blogs")
@RequiredArgsConstructor
public class BlogController {

private final BlogService blogService;

private final PrincipalHandler principalHandler;

@PostMapping("/blog")
@PostMapping("")
public ResponseEntity createBlog(
@ModelAttribute BlogCreateRequest blogCreateRequest
) {
return ResponseEntity.created(URI.create(blogService.create(
principalHandler.getUserIdFromPrincipal(), blogCreateRequest))).build();
}

@PatchMapping("/blog/{blogId}/title")
@PatchMapping("/{blogId}/title")
public ResponseEntity updateBlogTitle(
@PathVariable Long blogId,
@Valid @RequestBody BlogTitleUpdateRequest blogTitleUpdateRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/member") // 버전1의 api를 만든다.
@RequestMapping("/api/v1/members") // 버전1의 api를 만든다.
public class MemberController {

private final MemberService memberService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
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 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.PathVariable;
Expand All @@ -25,27 +25,24 @@ public class PostController {

private final PostService postService;

@PostMapping("post/{blogId}")
@PostMapping(" /blogs/{blogId}/posts")
public ResponseEntity<SuccessStatusResponse> publishPost(
@RequestHeader(name = "memberId") Long memberId,
@PathVariable(name = "blogId") Long blogId,
@Valid @RequestBody PostCreateRequest postCreateRequest
) {

// Location은 생성된 리소스의 위치를 나타낸다. 이 코드의 경우 생성된 post의 id
return ResponseEntity.status(HttpStatus.CREATED).header(
"Location",
postService.create(memberId, blogId, postCreateRequest))
.body(SuccessStatusResponse.of(SuccessMessage.POST_CREATE_SUCCESS));
return ResponseEntity.created(
URI.create(postService.create(memberId, blogId, postCreateRequest))).build();
}

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

PostListFindDto postList = postService.findAllPost(memberId, blogId);
PostListFindDto postList = postService.findAllPost(blogId);

return ResponseEntity.ok(SuccessStatusResponse.of(
SuccessMessage.POST_FIND_SUCCESS,
Expand All @@ -54,14 +51,14 @@ public ResponseEntity<SuccessStatusResponse> findPostList(

}

@GetMapping("post/{blogId}/{postId}")
@GetMapping("/blogs/{blogId}/posts/{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);
PostFindDto post = postService.findPost(postId);

return ResponseEntity.ok(SuccessStatusResponse.of(
SuccessMessage.POST_FIND_SUCCESS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class BlogService {

private final BlogRepository blogRepository;
private final MemberService memberService;

private final S3Service s3Service;
private static final String BLOG_S3_UPLOAD_FOLER = "blog/";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ public Member findMemberById(
);
}


@Transactional(readOnly = true) // 써도 되고 안 써도 됨.
public MemberFindDto findMemberById2(Long memberId) {
return MemberFindDto.of(memberRepository.findById(memberId).orElseThrow(
() -> new EntityNotFoundException("ID에 해당하는 사용자가 존재하지 않습니다.")));
}


@Transactional
public void deleteMemberById(Long memberId) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.example.demo.domain.Post;
import com.example.demo.exception.NotFoundException;
import com.example.demo.repository.PostRepository;
import com.example.demo.service.BlogService;
import com.example.demo.service.dto.post.PostCreateRequest;
import com.example.demo.service.dto.post.PostFindDto;
import com.example.demo.service.dto.post.PostListFindDto;
Expand All @@ -30,9 +31,8 @@ public String create(Long memberId, Long blogId, PostCreateRequest postCreateReq
return post.getId().toString();
}

public PostListFindDto findAllPost(Long memberId, Long blogId){

Blog findBlog = blogService.validateOwner(blogId, memberId);
public PostListFindDto findAllPost(Long blogId){
Blog findBlog = blogService.findById(blogId);
return PostListFindDto.of(postRepository.findByBlog(findBlog));
}

Expand All @@ -42,9 +42,8 @@ public Post findById(Long postId) {
);
}

public PostFindDto findPost(Long memberId, Long blogId, Long postId){
public PostFindDto findPost(Long postId){

blogService.validateOwner(blogId, memberId);
return PostFindDto.of(findById(postId));
}
}

0 comments on commit f688898

Please sign in to comment.