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. 21:03

1. Comment Class


import lombok.Getter;

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

@Entity
@Getter
public class Comment {
    
    @Id
    @GeneratedValue
    private Long id;
    
    private String content;

    public Comment() {
    }

    public Comment(String content) {
        this.content = content;
    }

    public void changeContent(String content){
        this.content = content;
    }
}

 

 

 

2. CommentRepository


import hello.blog.domain.comment.Comment;
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 CommentJpaRepository implements CommentRepository{

    @PersistenceContext
    private final EntityManager em;


    @Override
    public Comment save(Comment comment) {
        em.persist(comment);
        return comment;
    }

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


    @Override
    public Optional<List<Comment>> findAll() {
        return Optional.ofNullable(
                em.createQuery("select c from Comment c", Comment.class).getResultList());
    }


    @Override
    public void removeComment(Comment comment) {
        em.remove(findById(comment.getId()));

    }

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

 

 

3. CommentServiceImpl


import hello.blog.domain.comment.Comment;
import hello.blog.repository.comment.CommentRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.NoResultException;
import java.util.List;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class CommentServiceImpl implements CommentService{

    private final CommentRepository commentRepository;

    @Override
    @Transactional
    public Comment saveComment(Comment comment) {
        return commentRepository.save(comment);
    }

    @Override
    public Comment findCommentById(Long id) {
        return commentRepository.findById(id).orElseThrow(NoResultException::new);
    }

    @Override
    public List<Comment> findAll() {
        return commentRepository.findAll().orElseThrow(NoResultException::new);
    }

    @Override
    public Comment updateComment(Long id, Comment updatedPost) {
        Comment comment = commentRepository.findById(id).orElseThrow(NoResultException::new);
        comment.changeContent(updatedPost.getContent());
        return  commentRepository.save(comment);
    }

    @Override
    public void deleteComment(Long id) {
        commentRepository.removeComment(findCommentById(id));
    }

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

 

 

4. GitHub : 211003 CommentDomain


 

GitHub - bsh6463/blog

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

github.com

 

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

회원가입 개발  (0) 2021.10.04
도메인 모델, 연관관계  (0) 2021.10.03
댓글 도메인 설계  (0) 2021.10.03
게시글 도메인 개발  (0) 2021.10.03
게시글 도메인 설계  (0) 2021.10.03
Comments