CommentService.java 2.9 KB
Newer Older
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
package cn.itcast.article.service;

import cn.itcast.article.dao.CommentRepository;
import cn.itcast.article.po.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private CommentRepository commentRepository;

    /**
     * 保存一个评论
     *
     * @param comment
     */
    public void saveComment(Comment comment) {
        //如果需要自定义主键,可以在这里指定主键;如果不指定主键,MongoDB会自动生成主键
        //设置一些默认初始值。。。
        //调用dao
        commentRepository.save(comment);
    }

    /**
     * 更新评论
     *
     * @param comment
     */
    public void updateComment(Comment comment) {
        //调用dao
        commentRepository.save(comment);
    }

    /**
     * 根据id删除评论
     *
     * @param id
     */
    public void deleteCommentById(String id) {
        //调用dao
        commentRepository.deleteById(id);
    }

    /**
     * 查询所有评论
     *
     * @return
     */
    public List<Comment> findCommentList() {
        //调用dao
        return commentRepository.findAll();
    }

    /**
     * 根据id查询评论
     *
     * @param id
     * @return
     */
    public Comment findCommentById(String id) {
        //调用dao
        return commentRepository.findById(id).get();
    }

76 77
    public Page<Comment> findByUserid(String userid, int page, int size) {
        return commentRepository.findByUserid(userid, PageRequest.of(page - 1, size));
78 79 80 81 82 83 84 85 86 87
    }

    public void updateCommentLikenum(String id) {
        //  查询条件
        Query query = Query.query(Criteria.where("_id").is(id));
        //  更新条件
        Update update = new Update();
        update.inc("likenum");
        mongoTemplate.updateFirst(query, update, Comment.class);
    }
88 89 90 91 92 93 94 95 96 97 98 99

    public Page<Comment> findByUseridAndLikenum(String userid, int likenum, int page, int size) {
        return commentRepository.findByUseridAndLikenum(userid, likenum, PageRequest.of(page - 1, size));
    }

    public Page<Comment> findByUseridContainingAndLikenumGreaterThanEqual(String userid, int likenum, int page, int size) {
        return commentRepository.findByUseridContainingAndLikenumGreaterThanEqual(userid, likenum, PageRequest.of(page - 1, size));
    }

    public List<Comment> findByUseridAndLikenum(String userid, int likenum) {
        return commentRepository.findByUseridAndLikenum(userid, likenum);
    }
100
}