Skip to content

Commit

Permalink
feat : 3차 세미나 실습 과제_ Post 전체조회 Service, Controller 구현 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
PicturePark1101 committed May 1, 2024
1 parent 4ca0050 commit 3305c4f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,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 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;
Expand All @@ -35,12 +40,25 @@ public ResponseEntity<SuccessStatusResponse> publishPost(
@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));
}

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

List<PostFindDto> postList = postService.findAllPost(memberId, blogId);

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

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.example.demo.domain.Post;
import com.example.demo.repository.PostRepository;
import com.example.demo.service.dto.post.PostCreateRequest;
import com.example.demo.service.dto.post.PostFindDto;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -31,4 +33,16 @@ public String create(Long memberId, Long blogId, PostCreateRequest postCreateReq
// 블로그 글 생성하고 DB에 저장 후 생성된 id 반환
return post.getId().toString();
}

public List<PostFindDto> findAllPost(Long memberId, Long blogId){
// blog 찾기
Blog blog = blogService.findById(blogId);
Long findMemberId = blog.getMember().getId();

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

return postRepository.findByBlog(blog)
.stream().map(PostFindDto::of).toList();
}
}

0 comments on commit 3305c4f

Please sign in to comment.