Search in sources :

Example 1 with TagDTO

use of com.nixmash.blog.jpa.dto.TagDTO 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 TagDTO

use of com.nixmash.blog.jpa.dto.TagDTO in project nixmash-blog by mintster.

the class PostsRestController method getTagCloud.

@RequestMapping(value = "/tagcloud", produces = "text/html;charset=UTF-8")
public String getTagCloud(@RequestParam(value = "alltags", required = false, defaultValue = "false") Boolean alltags) {
    int tagCount = alltags ? -1 : applicationSettings.getSidebarTagCloudCount();
    List<TagDTO> tags = postService.getTagCloud(tagCount);
    maxTagCount = tags.stream().mapToInt(TagDTO::getTagCount).max().orElse(0);
    minTagCount = tags.stream().mapToInt(TagDTO::getTagCount).min().orElse(0);
    StringBuilder tagHtml = new StringBuilder();
    tagHtml.append("<ul class='taglist'>");
    for (TagDTO tag : tags) {
        tagHtml.append(tagHtml(tag));
    }
    tagHtml.append("</ul>");
    return tagHtml.toString();
}
Also used : TagDTO(com.nixmash.blog.jpa.dto.TagDTO) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) JsonRequestMapping(com.nixmash.blog.mvc.annotations.JsonRequestMapping)

Example 3 with TagDTO

use of com.nixmash.blog.jpa.dto.TagDTO in project nixmash-blog by mintster.

the class PostServiceTests method updatePostWithTags.

@Test
public void updatePostWithTags() throws DuplicatePostNameException, PostNotFoundException {
    // Post(5L) is loaded with 2 tags in H2
    Post post = postService.getPostById(5L);
    PostDTO postDTO = PostUtils.postToPostDTO(post);
    postDTO.getTags().add(new TagDTO("updatePostWithTags1"));
    Post updated = postService.update(postDTO);
    assertEquals(updated.getTags().size(), 3);
    Post retrieved = postService.getPostById(5L);
    assertEquals(retrieved.getTags().size(), 3);
}
Also used : Post(com.nixmash.blog.jpa.model.Post) PostUtils.postDtoToPost(com.nixmash.blog.jpa.utils.PostUtils.postDtoToPost) TagDTO(com.nixmash.blog.jpa.dto.TagDTO) PostDTO(com.nixmash.blog.jpa.dto.PostDTO) Test(org.junit.Test)

Example 4 with TagDTO

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

Example 5 with TagDTO

use of com.nixmash.blog.jpa.dto.TagDTO in project nixmash-blog by mintster.

the class PostServiceTests method addPostWithTags.

// endregion
// region Tags
@Test
public void addPostWithTags() throws DuplicatePostNameException, PostNotFoundException {
    PostDTO postDTO = PostTestUtils.createPostDTO(3);
    postDTO.getTags().add(new TagDTO("addPostWithTags1"));
    postDTO.getTags().add(new TagDTO("addPostWithTags2"));
    Post post = postService.add(postDTO);
    assertEquals(post.getTags().size(), 2);
    Post retrieved = postService.getPostById(post.getPostId());
    assertEquals(retrieved.getTags().size(), 2);
}
Also used : Post(com.nixmash.blog.jpa.model.Post) PostUtils.postDtoToPost(com.nixmash.blog.jpa.utils.PostUtils.postDtoToPost) TagDTO(com.nixmash.blog.jpa.dto.TagDTO) PostDTO(com.nixmash.blog.jpa.dto.PostDTO) Test(org.junit.Test)

Aggregations

TagDTO (com.nixmash.blog.jpa.dto.TagDTO)6 Transactional (org.springframework.transaction.annotation.Transactional)3 CachePostUpdate (com.nixmash.blog.jpa.annotations.CachePostUpdate)2 PostDTO (com.nixmash.blog.jpa.dto.PostDTO)2 Post (com.nixmash.blog.jpa.model.Post)2 PostUtils.postDtoToPost (com.nixmash.blog.jpa.utils.PostUtils.postDtoToPost)2 Test (org.junit.Test)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 JsonRequestMapping (com.nixmash.blog.mvc.annotations.JsonRequestMapping)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1