Search in sources :

Example 1 with CachePostUpdate

use of com.nixmash.blog.jpa.annotations.CachePostUpdate in project nixmash-blog by mintster.

the class PostServiceImpl method add.

// endregion
// region Add / UpdatePost
@Transactional(rollbackFor = DuplicatePostNameException.class)
@Override
@CachePostUpdate
public Post add(PostDTO postDTO) throws DuplicatePostNameException {
    Post post;
    try {
        post = postRepository.save(PostUtils.postDtoToPost(postDTO));
        em.refresh(post);
    } catch (Exception e) {
        throw new DuplicatePostNameException("Duplicate Post Name for Post Title: " + postDTO.getPostTitle());
    }
    if (postDTO.getTags() != null) {
        saveNewTagsToDataBase(postDTO);
        post.setTags(new HashSet<>());
        for (TagDTO tagDTO : postDTO.getTags()) {
            Tag tag = tagRepository.findByTagValueIgnoreCase(tagDTO.getTagValue());
            post.getTags().add(tag);
        }
    }
    Category category = categoryRepository.findByCategoryId(postDTO.getCategoryId());
    post.setCategory(category);
    return post;
}
Also used : DuplicatePostNameException(com.nixmash.blog.jpa.exceptions.DuplicatePostNameException) TagDTO(com.nixmash.blog.jpa.dto.TagDTO) PostNotFoundException(com.nixmash.blog.jpa.exceptions.PostNotFoundException) CategoryNotFoundException(com.nixmash.blog.jpa.exceptions.CategoryNotFoundException) TagNotFoundException(com.nixmash.blog.jpa.exceptions.TagNotFoundException) DuplicatePostNameException(com.nixmash.blog.jpa.exceptions.DuplicatePostNameException) CachePostUpdate(com.nixmash.blog.jpa.annotations.CachePostUpdate) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with CachePostUpdate

use of com.nixmash.blog.jpa.annotations.CachePostUpdate in project nixmash-blog by mintster.

the class PostServiceImpl method update.

@Transactional(rollbackFor = PostNotFoundException.class)
@Override
@CachePostUpdate
public Post update(PostDTO postDTO) throws PostNotFoundException {
    Post post = postRepository.findByPostId(postDTO.getPostId());
    post.update(postDTO.getPostTitle(), postDTO.getPostContent(), postDTO.getIsPublished(), postDTO.getDisplayType());
    saveNewTagsToDataBase(postDTO);
    post.getTags().clear();
    for (TagDTO tagDTO : postDTO.getTags()) {
        Tag tag = tagRepository.findByTagValueIgnoreCase(tagDTO.getTagValue());
        if (!post.getTags().contains(tag))
            post.getTags().add(tag);
    }
    Category category = categoryRepository.findByCategoryId(postDTO.getCategoryId());
    post.setCategory(category);
    return post;
}
Also used : TagDTO(com.nixmash.blog.jpa.dto.TagDTO) CachePostUpdate(com.nixmash.blog.jpa.annotations.CachePostUpdate) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

CachePostUpdate (com.nixmash.blog.jpa.annotations.CachePostUpdate)2 TagDTO (com.nixmash.blog.jpa.dto.TagDTO)2 Transactional (org.springframework.transaction.annotation.Transactional)2 CategoryNotFoundException (com.nixmash.blog.jpa.exceptions.CategoryNotFoundException)1 DuplicatePostNameException (com.nixmash.blog.jpa.exceptions.DuplicatePostNameException)1 PostNotFoundException (com.nixmash.blog.jpa.exceptions.PostNotFoundException)1 TagNotFoundException (com.nixmash.blog.jpa.exceptions.TagNotFoundException)1