仿牛客社区——7.5置顶、加精、删除

实现功能

功能实现

点击 置顶,修改帖子的类型

点击“加精”、“删除”,修改帖子的状态

权限管理

版主可以执行“置顶“、“取消置顶”、“加精”、“取消加精”操作。

管理员可以执行“删除”操作。

按钮显示

“加精”按钮版主可以看到“置顶“、“加精”按钮

管理员可以看到“删除”按钮。

新增常量

/**
 * 主题:删帖
 */

String TOPIC_DELETE="delete";

Controller

	//置顶与取消置顶
	@RequestMapping(value = "/top",method = RequestMethod.POST)
	@ResponseBody //结果返回为json字符串
	public String setTop(int id){
		//获取帖子
		DiscussPost post = discussPostService.findDiscussPostById(id);
		//int type = post.getType();  //获取帖子置顶状态
		//if(type==0){
		//	type=1;
		//}else{
		//	type=0;
		//}
		/**也可采取异或方式*/
		// 获取置顶状态,1为置顶,0为正常状态,1^1=0 0^1=1
		int type = post.getType()^1;
		//置顶与取消置顶(更改帖子类型
		discussPostService.updateType(id,type);
		//将返回的结果
		Map<String,Object> map=new HashMap<>();
		map.put("type",type);


		//更改完帖子后,要同步更新elasticsearch中的结果
		//触发发帖事件
		Event event=new Event()
				.setTopic(TOPIC_PUBLISH)
				.setUserId(hostHolder.getUser().getId())
				.setEntityId(id)
				.setEntityType(ENTITY_TYPE_POST);

		eventProducer.fireEvent(event);

		return CommunityUtil.getJsonString(0,null,map);
	}

	//加精
	@RequestMapping(value = "/wonderful",method = RequestMethod.POST)
	@ResponseBody //结果返回为json字符串
	public String setWonderful(int id){
		DiscussPost post = discussPostService.findDiscussPostById(id);
		// 1为加精,0为正常, 1^1=0, 0^1=1
		int status = post.getStatus()^1;
		//更改帖子状态
		discussPostService.updateStatus(id,status);
		//将返回的结果
		Map<String,Object> map=new HashMap<>();
		map.put("status",status);


		//更改完帖子后,要同步更新elasticsearch中的结果
		//触发发帖事件
		Event event=new Event()
				.setTopic(TOPIC_PUBLISH)
				.setUserId(hostHolder.getUser().getId())
				.setEntityId(id)
				.setEntityType(ENTITY_TYPE_POST);

		eventProducer.fireEvent(event);

		return CommunityUtil.getJsonString(0,null,map);
	}

	//删除
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	@ResponseBody //结果返回为json字符串
	public String setDelete(int id){
		//更改帖子状态
		discussPostService.updateStatus(id,2);

		//更改完帖子后,要同步更新elasticsearch中的结果
		//触发删帖事件
		Event event=new Event()
				.setTopic(TOPIC_DELETE)
				.setUserId(hostHolder.getUser().getId())
				.setEntityId(id)
				.setEntityType(ENTITY_TYPE_POST);

		eventProducer.fireEvent(event);

		return CommunityUtil.getJsonString(0);
	}

service

 //置顶:更改类型为1
    public int updateType(int id,int type){
        return discussPostMapper.updateType(id,type);
    }

    //加精:更改状态为1
    public int updateStatus(int id,int status){
        return discussPostMapper.updateStatus(id,status);
    }

dao

  //置顶:更改类型为1
    int updateType(int id,int type);

    //加精:更改状态为1
    int updateStatus(int id ,int status);

mapper.xml

 <!-- 置顶:更改类型为1 -->
    <!-- int updateType(int id,int type); -->
    <update id="updateType">
        update discuss_post
        set type=#{type}
        where id=#{id}
    </update>

    <!-- 加精:更改状态为1 -->
    <!-- int updateStatus(int id ,int status); -->
    <update id="updateStatus">
        update discuss_post
        set status=#{status}
        where id=#{id}
    </update>

consumer

此处新增了删帖事件,发帖事件在之前的笔记中有给出

//消费者消费删帖事件--->同步到elasticsearch中
	@KafkaListener(topics = TOPIC_DELETE)
	public void handleDelete(ConsumerRecord record){
		//先进行判断record是否为空:未发事件或者发送的事件为空
		if(record==null|| record.value()==null){
			logger.error("发送的消息为空!");
			return;
		}

		//事件不为空:将事件转换为Event对象
		Event event= JSONObject.parseObject(record.value().toString(),Event.class);
		//判断对象是否为空
		if(event==null){
			logger.error("消息格式错误!");
			return;
		}

		elasticsearchService.deleteDiscussPost(event.getEntityId());
	}

securityConfig

//授权相关配置(对现有路径权限配置
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests()
				.antMatchers(
						"/comment/add/**",
						"/discuss/add",
						"/follow",
						"/unfollow",
						"/like",
						"/letter/**",
						"/user/setting",
						"/user/upload",
						"/notice/**"
				)
				.hasAnyAuthority(
						AUTHORITY_ADMIN,
						AUTHORITY_MODERATOR,
						AUTHORITY_USER
				)
				.antMatchers(
						"/discuss/top",
						"/discuss/wonderful"
				)
				.hasAnyAuthority(
						AUTHORITY_MODERATOR
				)
				.antMatchers(
						"/discuss/delete"
				)
				.hasAnyAuthority(
						AUTHORITY_ADMIN
				)
				.anyRequest().permitAll(); //除了这些请求以外任何请求都是允许访问的
				//.and().csrf().disable();   //不启用防止CSRF攻击

html

<div class="float-right">
						<input type="hidden" id="postId" th:value="${post.id}">
						<button type="button" class="btn btn-danger btn-sm" id="topBtn"
								th:text="${post.type==1?'取消置顶':'置顶'}" sec:authorize="hasAnyAuthority('moderator')">置顶</button>
						<button type="button" class="btn btn-danger btn-sm" id="wonderfulBtn"
								th:text="${post.status==1?'取消加精':'加精'}" sec:authorize="hasAnyAuthority('moderator')">加精</button>
						<button type="button" class="btn btn-danger btn-sm" id="deleteBtn"
								th:disabled="${post.status==2}" sec:authorize="hasAnyAuthority('admin')">删除</button>
					</div>




<!--

头部要引入   xmlns:sec="http://www.thymeleaf.org/extras/spring-security"

--!>

js

$(function(){  //在刚初始化完html页面时加载该方法
    $("#topBtn").click(setTop);
    $("#wonderfulBtn").click(setWonderful);
    $("#deleteBtn").click(setDelete);
});


//置顶与取消置顶
function setTop(){
 //发送Ajax请求之前,提前将CSRF令牌设置到请求的消息头中
    	var token=$("meta[name='_csrf']").attr("content");
    	var header=$("meta[name='_csrf_header']").attr("content");
    	//在发送请求之前对页面进行设置   xhr:发送异步请求的核心对象
    	$(document).ajaxSend(function(e,xhr,options){
    	    xhr.setRequestHeader(header,token);  //设置请求头
    	});
    $.post(
        CONTEXT_PATH+"/discuss/top",
        {"id":$("#postId").val()},
        function(data){
            data=$.parseJSON(data);
            if(data.code==0){
                $("#topBtn").text(data.type==1?'取消置顶':'置顶');
            }else{
                alert(data.msg);
            }
        }

    );
}

//加精与取消加精
function setWonderful(){
 //发送Ajax请求之前,提前将CSRF令牌设置到请求的消息头中
    	var token=$("meta[name='_csrf']").attr("content");
    	var header=$("meta[name='_csrf_header']").attr("content");
    	//在发送请求之前对页面进行设置   xhr:发送异步请求的核心对象
    	$(document).ajaxSend(function(e,xhr,options){
    	    xhr.setRequestHeader(header,token);  //设置请求头
    	});
    $.post(
        CONTEXT_PATH+"/discuss/wonderful",
        {"id":$("#postId").val()},
        function(data){
            data=$.parseJSON(data);
            if(data.code==0){
                $("#wonderfulBtn").text(data.status==1?'取消加精':'加精');
            }else{
                alert(data.msg);
            }
        }

    );
}

//删除
function setDelete(){
 //发送Ajax请求之前,提前将CSRF令牌设置到请求的消息头中
    	var token=$("meta[name='_csrf']").attr("content");
    	var header=$("meta[name='_csrf_header']").attr("content");
    	//在发送请求之前对页面进行设置   xhr:发送异步请求的核心对象
    	$(document).ajaxSend(function(e,xhr,options){
    	    xhr.setRequestHeader(header,token);  //设置请求头
    	});
    $.post(
        CONTEXT_PATH+"/discuss/delete",
        {"id":$("#postId").val()},
        function(data){
            data=$.parseJSON(data);
            if(data.code==0){
                //删除成功返回到首页
                location.href=CONTEXT_PATH+"/index";
            }else{
                alert(data.msg);
            }
        }

    );
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值