携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第33天,点击查看活动详情
7.3 置顶、加精、删除
在thymeleaf中其实支持一些Spring Security的东西,去做不同权限是否可以看到相应按钮管理,我们要引入一下它们的依赖(其实在第三个阶段按钮显示时才会用到这个依赖,我们提前引入)
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
功能实现
查看帖子的数据库表
CREATE TABLE `discuss_post` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` varchar(45) DEFAULT NULL,
`title` varchar(100) DEFAULT NULL,
`content` text,
`type` int(11) DEFAULT NULL COMMENT '0-普通; 1-置顶;',
`status` int(11) DEFAULT NULL COMMENT '0-正常; 1-精华; 2-拉黑;',
`create_time` timestamp NULL DEFAULT NULL,
`comment_count` int(11) DEFAULT NULL,
`score` double DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index_user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=286 DEFAULT CHARSET=utf8;
type为1表示置顶
status为1表示精华,为2表示拉黑(删除,页面不显示,但是数据库还有)
数据访问层(dao)
DiscussPostMapper
// 修改帖子类型
int updateType(int id, int type);
// 修改帖子状态
int updateStatus(int id, int status);
discusspost-mapper.xml
<!--更新类型-->
<update id="updateType">
update discuss_post set type = #{type} where id = #{id}
</update>
<!--更新状态-->
<update id="updateStatus">
update discuss_post set status = #{status} where id = #{id}
业务层(service)
DiscussPostService
// 更新帖子类型
public int updateType(int id, int type) {
return discussPostMapper.updateType(id, type);
}
// 更新帖子状态
public int updateStatus(int id, int status) {
return discussPostMapper.updateStatus(id, status);
}
表现层
常量接口 CommunityConstant中定义一个常量表示删除帖子
/**
* 主题: 删帖
*/
String TOPIC_DELETE = "delete";
DiscussPostController
// 置顶
@RequestMapping(path = "/top", method = RequestMethod.POST)
@ResponseBody // 异步请求
public String setTop(int id) {
discussPostService.updateType(id, 1);
// 触发发帖事件
Event event = new Event()
.setTopic(TOPIC_PUBLISH)
.setUserId(hostHolder.getUser().getId())
.setEntityType(ENTITY_TYPE_POST)
.setEntityId(id);
eventProducer.fireEvent(event);
return CommunityUtil.getJSONString(0);
}
// 加精
@RequestMapping(path = "/wonderful", method = RequestMethod.POST)
@ResponseBody
public String setWonderful(int id) {
discussPostService.updateStatus(id, 1);
// 触发发帖事件
Event event = new Event()
.setTopic(TOPIC_PUBLISH)
.setUserId(hostHolder.getUser().getId())
.setEntityType(ENTITY_TYPE_POST)
.setEntityId(id);
eventProducer.fireEvent(event);
return CommunityUtil.getJSONString(0);
}
// 删除
@RequestMapping(path = "/delete", method = RequestMethod.POST)
@ResponseBody
public String setDelete(int id) {
discussPostService.updateStatus(id, 2);
// 触发删帖事件
Event event = new Event()
.setTopic(TOPIC_DELETE)
.setUserId(hostHolder.getUser().getId())
.setEntityType(ENTITY_TYPE_POST)
.setEntityId(id);
eventProducer.fireEvent(event);
return CommunityUtil.getJSONString(0);
}
因为删帖事件是新加的事件,之前没处理过,所以需要在事件消费者里把这个事件也消费一下
EventConsumer
// 消费删帖事件
@KafkaListener(topics = {TOPIC_DELETE})
public void handleDeleteMessage(ConsumerRecord record) {
if (record == null || record.value() == null) {
logger.error("消息的内容为空!");
return;
}
Event event = JSONObject.parseObject(record.value().toString(), Event.class);
if (event == null) {
logger.error("消息格式错误!");
return;
}
elasticsearchService.deleteDiscussPost(event.getEntityId());
}
然后就是处理帖子详情页面 discuss-detail.html
discuss.js
权限管理
对于权限管理我们要配置 SecurityConfig
按钮显示
这个需要thymeleaf对Spring Security的支持,在模板上获得当前用户的权限从而对这个按钮是否显示做出判断,这个需要我们已经引入 thymeleaf-extras-springsecurity5 依赖,之前已经引入
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
在模板上声明命名空间去使用它
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
只有版主可以看到 “置顶” “加精”按钮 ,只有管理员可以看到“删除”按钮