-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : 3차 세미나 실습 과제_post 생성 service 구현 #7
- Loading branch information
1 parent
9009462
commit 99d4503
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
week02/seminar/demo/src/main/java/com/example/demo/service/PostService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.example.demo.service; | ||
|
||
|
||
import com.example.demo.common.dto.ErrorMessage; | ||
import com.example.demo.domain.Blog; | ||
import com.example.demo.domain.Member; | ||
import com.example.demo.domain.Post; | ||
import com.example.demo.exception.NotFoundException; | ||
import com.example.demo.repository.MemberRepository; | ||
import com.example.demo.repository.PostRepository; | ||
import com.example.demo.service.dto.post.PostCreateRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PostService { | ||
|
||
private final PostRepository postRepository; | ||
private final MemberService memberService; | ||
private final BlogService blogService; | ||
|
||
@Transactional | ||
public String create(Long memberId, Long blogId, PostCreateRequest postCreateRequest) | ||
{ | ||
// blog 찾기 | ||
Blog blog = blogService.findById(blogId); | ||
Long findMemberId = blog.getMember().getId(); | ||
|
||
// 요청한 사람이 블로그 소유주인지 확인 | ||
blogService.validateOwner(memberId, findMemberId); | ||
|
||
Post post = postRepository.save(Post.create(blog, postCreateRequest)); | ||
// 블로그 글 생성하고 DB에 저장 후 생성된 id 반환 | ||
return post.getId().toString(); | ||
} | ||
} |