Created
January 9, 2017 09:58
-
-
Save yevgnenll/186b1f0dd8bb6cb9bba42064a6a28341 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yevgnenll.service.Impl; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.data.crossstore.ChangeSetPersister.NotFoundException; | |
import org.springframework.data.domain.Page; | |
import org.springframework.data.domain.PageRequest; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.annotation.Transactional; | |
import com.yevgnenll.domain.Comment; | |
import com.yevgnenll.domain.Post; | |
import com.yevgnenll.domain.User; | |
import com.yevgnenll.error.BadReqeustException; | |
import com.yevgnenll.repository.CommentRepository; | |
import com.yevgnenll.service.CommentService; | |
import com.yevgnenll.utils.UserInfoSecurity; | |
@Service | |
public class CommentServiceImpl implements CommentService { | |
@Autowired | |
private CommentRepository commentRepository; | |
@Autowired | |
private PostServiceImpl postService; | |
private final static Logger logger = LoggerFactory.getLogger(CommentServiceImpl.class); | |
@Transactional | |
@Override | |
public Comment writeComment(Comment comment) throws NotFoundException { | |
Post post = postService.getPost(comment.getPostId()); | |
if(post == null) { | |
throw new NotFoundException(); | |
} | |
int parentId = comment.getParent(); | |
if(parentId != 0) { | |
Comment parent = commentRepository.findOne(parentId); | |
comment.setStart(parent.getEnd()); | |
comment.setEnd(parent.getEnd() + 2); | |
int groupId = comment.getGroupId(); | |
List<Comment> willBeModifiedComment = commentRepository.findByGroupIdAndEnd(groupId, parent.getEnd()); | |
for(Comment modify : willBeModifiedComment) { | |
modify.setStart(parent.getEnd() + 2); | |
modify.setEnd(parent.getEnd() + 2); | |
} | |
commentRepository.save(willBeModifiedComment); | |
} | |
Comment result = commentRepository.save(comment); | |
return result; | |
} | |
@Transactional | |
@Override | |
public Comment modifyComment(int commentId, String content) { | |
Comment comment = commentRepository.findOne(commentId); | |
User securityUser = UserInfoSecurity.securedUserInfo(); | |
comment.modifyComment(content, securityUser); | |
return commentRepository.save(comment); | |
} | |
@Transactional | |
@Override | |
public List<Comment> commentListByPostId(int postId) { | |
List<Comment> comments = commentRepository.findByPostIdAndActiveTrueOrderByGroupIdDescSequenceAsc(postId); | |
if(comments.size() == 0) { | |
return new ArrayList<Comment>(); | |
} | |
return comments; | |
} | |
@Transactional | |
@Override | |
public void deleteComment(int commentId) { | |
Comment comment = commentRepository.findOne(commentId); | |
if(comment == null) { | |
throw new BadReqeustException(); | |
} | |
comment.setActive(false); | |
commentRepository.saveAndFlush(comment); | |
} | |
@Transactional | |
public Page<Comment> getCommentsListUser(PageRequest pageRequest){ | |
User user = UserInfoSecurity.securedUserInfo(); | |
return commentRepository.findByActiveTrueAndUser(user, pageRequest); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment