Spring Boot 项目实战5(评论、分类页面、标签页面)

接上:
https://blog.youkuaiyun.com/qq_43923042/article/details/107799148
上次讲到新闻详情,详情界面如下:
在这里插入图片描述显然,单单有新闻是不够的,还要有底下评论
1、新建实体类Comment

@Entity
@Table(name = "t_comment")
public class Comment {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String nickname;
    private String email;
    private String content;
    private   String avatar;
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;
    @ManyToOne
    private News news;
    @ManyToOne
    Comment parentComment;
    private boolean adminComment;
    @OneToMany(mappedBy = "parentComment")
    List<Comment> replyComments = new ArrayList<>();
}

2、DAO层:

public interface CommentReposity extends JpaRepository<Comment, Long> {
    List<Comment> findByNewsIdAndParentCommentNull(Long newId, Sort sort);
}

3、Service层:

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentReposity commentReposity;

    @Override
    public List<Comment> listCommentByNewId(Long newId) {
        Sort sort = Sort.by("createTime");
        //所有顶级回复
        List<Comment> comments = commentReposity.findByNewsIdAndParentCommentNull(newId, sort);
        return eachComment(comments);
    }

    @Override
    public Comment saveComment(Comment comment) {
        Long parenCommentId = comment.getParentComment().getId();
        comment.setCreateTime(new Date());
        if (parenCommentId != -1) {
            comment.setParentComment(commentReposity.findById(parenCommentId).orElse(null));
        } else {
            comment.setParentComment(null);
        }
        return commentReposity.save(comment);
    }

    private List<Comment> eachComment(List<Comment> comments) {
        List<Comment> commentsView = new ArrayList<>();
        for (Comment comment : comments) {
            Comment c = new Comment();
            BeanUtils.copyProperties(comment, c);
            commentsView.add(c);
        }
//        合并子评论到第一级集合中
        combineChildren(commentsView);
        return commentsView;
    }

    private void combineChildren(List<Comment> comments) {
        for (Comment comment : comments) {
            List<Comment> replys = comment.getReplyComments();
            for (Comment reply1 : replys) {
                recursively(reply1);
            }
            comment.setReplyComments(tempReplys);
            tempReplys = new ArrayList<>();
        }
    }

    private List<Comment> tempReplys = new ArrayList<>();

    private void recursively(Comment comment) {
        tempReplys.add(comment);
        if (comment.getReplyComments().size() > 0) {
            List<Comment> replys = comment.getReplyComments();
            for (Comment reply : replys) {
                tempReplys.add(reply);
                if (reply.getReplyComments().size() > 0) {
                    recursively(reply);
                }
            }
        }
    }
}

4、Controller层:

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private NewService newService;

    private String avatar = "https://c-ssl.duitang.com/uploads/item/201804/23/2018042312947_wykaJ.thumb.1000_0.jpeg";

    @GetMapping("/comments/{newId}")
    String comments(@PathVariable Long newId, Model model) {
        model.addAttribute("comments", commentService.listCommentByNewId(newId));
        return "new :: commentList";
    }

    @PostMapping("/comments")
    public String post(Comment comment, HttpSession session) {
        Long newId = comment.getNews().getId();
        comment.setNews(newService.getNew(newId));
        System.out.println(comment.getParentComment().getId());
        User user = (User) session.getAttribute("user");
        if (user != null) {
            comment.setAdminComment(true);
            comment.setAvatar(avatar);
        } else {
            comment.setAvatar(avatar);
        }
        System.out.println("newId:" + newId);
        commentService.saveComment(comment);
        return "redirect:/comments/" + newId;
    }
}

在这里插入图片描述
再就是分类页面,将数据展现出来
在这里插入图片描述主要是通过统计不同类别的新闻
5、DAO不用新增函数,使用jpa原装的就行了
Service层新增以下函数:

    @Override
    public Page<Type> listType(Pageable pageable) {
        return typeRepository.findAll(pageable);
    }

6、Controller层:

@Controller
public class TypeShowController {
    @Autowired
    TypeService typeService;

    @Autowired
    NewService newService;

    @GetMapping("/types/{id}")
    public String types(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        @PathVariable Long id, Model model) {
        List<Type> types = typeService.listTypeTop(20);
        if (id == -1) {
            id = types.get(0).getId();
        }
        NewQuery newQuery = new NewQuery();
        newQuery.setTypeId(id);
        model.addAttribute("types", types);
        model.addAttribute("page", newService.listNews(pageable, newQuery));
        model.addAttribute("activeTypeId", id);
        return "types";
    }
}

标签页面也是大同小异。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值