Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

게시글 도메인 개발 본문

Project/블로그 게시판 만들기

게시글 도메인 개발

Seung__ 2021. 10. 3. 20:09

1. Post class


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Post {

    @Id @GeneratedValue
    private Long id;

    private String title;

    private String content;
    
    private Long viewCnt;

    public Post(String title, String content) {
        this.title = title;
        this.content = content;
    }

    public Post() {
    }
}

 

 

2. PostRepository


import hello.blog.domain.post.Post;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Optional;

@Repository
@RequiredArgsConstructor
public class PostJpaRepository implements PostRepository {

    @PersistenceContext
    private final EntityManager em;

    public Post save(Post post){
        em.persist(post);
        return post;
    }

    public Optional<Post> findById(Long id){
        return Optional.ofNullable(em.find(Post.class, id));
    }

    public Optional<Post> findByTitle(String title){
        return  em.createQuery("select p from Post p where p.title= :title", Post.class)
                .setParameter("title", title)
                .getResultList()
                .stream().findAny();
    }

    public Optional<List<Post>> findAll(){
        return Optional.ofNullable(
                em.createQuery("select p from Post p", Post.class)
                .getResultList());
    }

    public void removePost(Post post){
        em.remove(post);
    }

    public void clear(){
        em.clear();
    }

}

 

 

 

3. PostServiceImpl


import hello.blog.domain.post.Post;
import hello.blog.repository.post.PostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.NoResultException;
import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
public class PostServiceImpl implements PostService{

    private final PostRepository postRepository;

    @Override
    public Post savePost(Post post) {
        return postRepository.save(post);
    }

    @Override
    public Post findPostById(Long id) {
        return postRepository.findById(id).orElseThrow(() -> new NoResultException());
    }

    @Override
    public Post findPostByTitle(String title) {
        return  postRepository.findByTitle(title).orElseThrow(() -> new NoResultException());
    }

    @Override
    public List<Post> findAll() {
        return postRepository.findAll().orElseThrow(() -> new NoResultException());
    }

    @Override
    public Post updatePost(Long id, Post postParam) {
        Post post = postRepository.findById(id).orElseThrow(() -> new NoResultException());
        post.changeTitle(postParam.getTitle());
        post.changeContent(postParam.getContent());
        return postRepository.save(post);
    }

    @Override
    public void deletePost(Long id) {
        Post findPost = postRepository.findById(id).orElseThrow(() -> new NoResultException());
        postRepository.removePost(findPost);

    }

    @Override
    public void clearRepository() {
        postRepository.clear();
    }
}

 

 

 

4. Test


import hello.blog.domain.post.Post;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.NoResultException;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;


@SpringBootTest
@Transactional
class PostServiceImplTest {

    @Autowired PostServiceImpl postService;

    @Test
    void savePost() {
        //given
        Post post = createPost();

        //when
        Post savedPost = postService.savePost(post);

        //then
        assertThat(savedPost.getId()).isEqualTo(post.getId());
        assertThat(savedPost.getTitle()).isEqualTo("title");
        assertThat(savedPost.getContent()).isEqualTo("content");
    }

    @Test
    void findPostById() {
        //given
        Long id = postService.findPostByTitle("title1").getId();
        //when
        Post findPost = postService.findPostById(id);
        //then
        assertThat(findPost.getTitle()).isEqualTo("title1");
        assertThat(findPost.getContent()).isEqualTo("content1");
    }

    @Test
    void findPostByIdNoResult(){
        assertThrows(NoResultException.class, () -> postService.findPostById(10000L));
    }

    @Test
    void findPostByTitle() {
        //given, when
        Post findPost = postService.findPostByTitle("title1");

        //then
        assertThat(findPost.getTitle()).isEqualTo("title1");
        assertThat(findPost.getContent()).isEqualTo("content1");
    }

    @Test
    void findPostByTitleNoResult(){
        assertThrows(NoResultException.class, () -> postService.findPostByTitle("noResult"));
    }

    @Test
    void updatePost() {
        //given
        Long id = postService.findPostByTitle("title1").getId();
        Post postParam = new Post("changedTitle", "changedContent");

        //when
        Post updatedPost = postService.updatePost(id, postParam);

        //then
        assertThat(updatedPost.getTitle()).isEqualTo("changedTitle");
        assertThat(updatedPost.getContent()).isEqualTo("changedContent");

    }

    @Test
    void deletePost() {
        //given
        Post post = postService.findPostByTitle("title1");

        //when
        postService.deletePost(post.getId());

        //then
        assertThat(postService.findAll().size()).isEqualTo(3);
    }

    @Test
    void clearRepository() {
        //when
        postService.clearRepository();

        //then
        assertThat(postService.findAll().size()).isEqualTo(0);
    }

    @BeforeEach
    public void beforeEach(){
        postService.savePost(new Post("title1", "content1"));
        postService.savePost(new Post("title2", "content2"));
        postService.savePost(new Post("title3", "content3"));
        postService.savePost(new Post("title4", "content4"));
    }

    @AfterEach
    public void clear(){
        postService.clearRepository();
    }
    
    public Post createPost(){
        return new Post("title", "content");
    }
}

 

 

5. GitHub : 211003 PostDomain


 

GitHub - bsh6463/blog

Contribute to bsh6463/blog development by creating an account on GitHub.

github.com

 

'Project > 블로그 게시판 만들기' 카테고리의 다른 글

댓글 도메인 개발  (0) 2021.10.03
댓글 도메인 설계  (0) 2021.10.03
게시글 도메인 설계  (0) 2021.10.03
회원 도메인 개발  (0) 2021.10.03
회원 도메인 설계  (0) 2021.10.03
Comments