Search in sources :

Example 1 with DuplicatePostNameException

use of com.nixmash.blog.jpa.exceptions.DuplicatePostNameException 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)

Aggregations

CachePostUpdate (com.nixmash.blog.jpa.annotations.CachePostUpdate)1 TagDTO (com.nixmash.blog.jpa.dto.TagDTO)1 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 Transactional (org.springframework.transaction.annotation.Transactional)1