Skip to content

Commit

Permalink
feat : 3차 세미나 실습 과제_post 생성 service 구현 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
PicturePark1101 committed May 1, 2024
1 parent 9009462 commit 99d4503
Showing 1 changed file with 38 additions and 0 deletions.
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();
}
}

0 comments on commit 99d4503

Please sign in to comment.