栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

学习项目第二天(forsetblog中controller类backcommentcontroller代码学习)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

学习项目第二天(forsetblog中controller类backcommentcontroller代码学习)

package com.liuyanzhao.ssm.blog.controller.admin;

import cn.hutool.http.HtmlUtil;
import com.github.pagehelper.PageInfo;
import com.liuyanzhao.ssm.blog.entity.Article;
import com.liuyanzhao.ssm.blog.entity.Comment;
import com.liuyanzhao.ssm.blog.entity.User;
import com.liuyanzhao.ssm.blog.enums.ArticleStatus;
import com.liuyanzhao.ssm.blog.enums.Role;
import com.liuyanzhao.ssm.blog.enums.UserRole;
import com.liuyanzhao.ssm.blog.service.ArticleService;
import com.liuyanzhao.ssm.blog.service.CommentService;
import com.liuyanzhao.ssm.blog.util.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/admin/comment")
public class BackConn {
    //声明对象,应该是自动传值,等一下需要传值
    @Autowired
    private CommentService commentService;
    @Autowired
    private ArticleService articleService;
    //评论页面
    @RequestMapping(value = "")
    //设置参数 传入会话,等一下好操作
    public String commentList(@RequestParam(required = false,defaultValue = "1")Integer pageIndex,
                              @RequestParam(required = false,defaultValue = "10")Integer pageSize,
                              HttpSession session,
                              Model model){
        //获取对象
        User user = (User) session.getAttribute("user");
        //创造评论列表
        HashMapcriteria=new HashMap<>();
        //判断是管理员还是普通用户
        if(!UserRole.ADMIN.getValue().equals(user.getUserRole())){
            criteria.put("userId",user.getUserId());
        }
        //获取初始评论,评论的总数,用户的角色
        PageInfo commentPageInfo = commentService.listCommentByPage(pageIndex,pageSize,criteria);
        //载入视图
        model.addAttribute("pageInfo",commentPageInfo);
        model.addAttribute("pageUrlPrefix","/admin/comment?pageIndex");
        return  "Admin/Comment/index";
    }
    //添加评论
    @RequestMapping(value = "/insert",method = {RequestMethod.POST},produces = {"text/plain;charset=UTF-8"})
    @ResponseBody
    public void insertComment(HttpServletRequest request,Comment comment,HttpSession session){
        User user = (User) session.getAttribute("user");
        //获得评论的文章
        Article article = articleService.getArticleByStatusAndId(null, comment.getCommentArticleId());
        if(article == null){
            return;
        }
        //添加评论进文章中 添加用户id
        comment.setCommentUserId(user.getUserId());
        //通过常熟类添加ip
        comment.setCommentIp(MyUtils.getIpAddr(request));
        //添加修改时间
        comment.setCommentCreateTime(new Date());
        //拿过来修改后重新插入
        commentService.insertComment(comment);
        //更新文章评论数
        articleService.updateCommentCount(article.getArticleId());
    }
    //删除评论
    @RequestMapping(value = "/delete/{id}")
    //为什么要传入会话
    public void delectComment(@PathVariable("id") Integer id,HttpSession session){
        //获取该评论
        Comment comment = commentService.getCommentById(id);
        //判断有没有权限 所以创造一个user传入会话的user的值
        User user = (User) session.getAttribute("user");
        //判断
        if(user.getUserRole()!=UserRole.ADMIN.getValue()&&user.getUserId()!=comment.getCommentUserId()){
            return;
        }
        //删除评论
        commentService.deleteComment(id);
        //删除子评论
        ListchileCommentList=commentService.listChildComment(id);
        for(int i=0;i< chileCommentList.size();i++){
            //逐条删除
            commentService.deleteComment(chileCommentList.get(i).getCommentId());
        }
        //更新评论
        Article article = articleService.getArticleByStatusAndId(null, comment.getCommentArticleId());
        //调用更新serv
        articleService.updateCommentCount(article.getArticleId());

    }
    //编辑评论页面显示
    @RequestMapping(value = "/edit/{id}")
    public String editCommentView(@PathVariable("id") Integer id,Model model,HttpSession session){
        //判断是否是管理员
        User user = (User) session.getAttribute("user");
        if(!Objects.equals(user.getUserRole(),UserRole.ADMIN.getValue())){
            return "redirect:/403";
        }
        //不然就获取该评论,调用删除服务
        Comment comment = commentService.getCommentById(id);
        model.addAttribute("comment",comment);
        return "Admin/Comment/edit";
    }
    //编辑评论提交
    @RequestMapping(value = "/editSubmit",method = RequestMethod.POST)
    public String editCommentSubmit(Comment comment,HttpSession session){
        User user = (User) session.getAttribute("user");
        if(!Objects.equals(user.getUserRole(),UserRole.ADMIN.getValue())){
            return "redirect:/403";
        }
        commentService.updateComment(comment);
        return "redirect:/admin/comment";
    }
    //回复评论页面
    @RequestMapping(value = "/reply/{id}")
    //传入id和视图
    public String replyCommentView(@PathVariable("id") Integer id,Model model){
        //获取评论
        Comment comment = commentService.getCommentById(id);
        //增加评论
        model.addAttribute("comment",comment);
        return "Admin/Comment/reply";
    }
    //回复评论提交
    @RequestMapping(value = "/replySubmit",method = RequestMethod.POST)
    //传入请求(用于设置评论发布地址) 评论 会话
    public String replyCommentSubmit(HttpServletRequest request,Comment comment,HttpSession session){
        //获取当前文章
        Article article = articleService.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), comment.getCommentArticleId());
        if(article==null){
            return "redirect:/404";
        }
        //获取对象,更新文章
        User user = (User) session.getAttribute("user");
        //HtmlUtil.escape防止有些同学恶意输入
        comment.setCommentContent(HtmlUtil.escape(comment.getCommentContent()));
        //获取该输入人的昵称
        comment.setCommentAuthorName(user.getUserNickname());
        comment.setCommentAuthorEmail(user.getUserEmail());
        //获取发布位置
        comment.setCommentAuthorUrl(user.getUserUrl());
        //文章字数
        article.setArticleCommentCount(article.getArticleCommentCount()+1);
        //键入文章
        articleService.updateArticle(article);
        //添加评论 设置时间
        comment.setCommentCreateTime(new Date());
        //设置地址
        comment.setCommentIp(MyUtils.getIpAddr(request));
        if(Objects.equals(user.getUserId(),article.getArticleUserId())){
            //如果是作者
            comment.setCommentRole(Role.OWNER.getValue());
        }else{
            comment.setCommentRole(Role.VISITOR.getValue());
        }
        //编辑好后会话调用服务
        commentService.insertComment(comment);
        return "redirect:/admin/comment";
    }
}













转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/891805.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号