问答平台(7),置顶、加精、删除

功能实现

1
2
- 点击“置顶”,修改帖子的类型。
- 点击“加精”、“删除”,修改帖子的状态。

引入依赖

1
2
3
4
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>

数据访问层

  • DiscussPostMapper: 增加内容
    1
    2
    int updateType(int id, int type);
    int updateStatus(int id, int status);
  • discusspost-mapper.xml: 增加内容
    1
    2
    3
    4
    5
    6
    7
    <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}
    </update>

业务层

  • DiscussPostService: 增加内容
    1
    2
    3
    4
    5
    6
    7
    public int updateType(int id, int type) {
    return discussPostMapper.updateType(id, type);
    }

    public int updateStatus(int id, int status) {
    return discussPostMapper.updateStatus(id, status);
    }

表现层

  • DiscussPostController: 增加内容
    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
    // 置顶
    @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: 增加内容
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // 消费删帖事件
    @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: 修改(置顶、加精、删除)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <!-- 声明:命名空间 -->
    <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
    <!-- 内容 -->
    <!-- 标题 -->
    <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:disabled="${post.type==1}">置顶</button>
    <button type="button" class="btn btn-danger btn-sm" id="wonderfulBtn" th:disabled="${post.status==1}">加精</button>
    <button type="button" class="btn btn-danger btn-sm" id="deleteBtn" th:disabled="${post.status==2}">删除</button>
    </div>
  • discuss.js: 修改
    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
    $(function () {
    $("#topBtn").click(setTop);
    $("#wonderfulBtn").click(setWonderful);
    $("#deleteBtn").click(setDelete);
    });

    // 置顶
    function setTop() {
    $.post(
    CONTEXT_PATH + "/discuss/top",
    {"id": $("#postId").val()},
    function (data) {
    data = $.parseJSON(data);
    if (data.code == 0) {
    $("#topBtn").attr("disabled", "disabled");
    } else {
    alert(data.msg);
    }
    }
    );
    }

    // 加精
    function setWonderful() {
    $.post(
    CONTEXT_PATH + "/discuss/wonderful",
    {"id": $("#postId").val()},
    function (data) {
    data = $.parseJSON(data);
    if (data.code == 0) {
    $("#wonderfulBtn").attr("disabled", "disabled");
    } else {
    alert(data.msg);
    }
    }
    );
    }

    // 删除
    function setDelete() {
    $.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);
    }
    }
    );
    }

权限管理

1
2
- 版主:可以执行“置顶”、“加精”操作。
- 管理员:可以执行“删除”操作。

权限配置

  • SecurityConfig: 修改
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     @Override
    protected void configure(HttpSecurity http) throws Exception {
    // 授权
    http.authorizeRequests()
    .antMatchers(
    "/discuss/top",
    "/discuss/wonderful"
    )
    .hasAnyAuthority(
    AUTHORITY_MODERATOR
    )
    .antMatchers(
    "/discuss/delete"
    )
    .hasAnyAuthority(
    AUTHORITY_ADMIN
    )
    .anyRequest().permitAll()
    .and().csrf().disable();

    // 权限不够时的处理(略)
    }

按钮显示

1
2
- 版主:可以看到“置顶”、“加精”按钮。
- 管理员:可以看到“删除”按钮。

页面

  • discuss-detail.html: 修改
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <!-- 标题 -->
    <h6 class="mb-4">
    <img src="http://static.nowcoder.com/images/img/icons/ico-discuss.png"/>
    <span th:utext="${post.title}">备战春招,面试刷题跟他复习,一个月全搞定!</span>
    <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:disabled="${post.type==1}" sec:authorize="hasAnyAuthority('moderator')">置顶
    </button>
    <button type="button" class="btn btn-danger btn-sm" id="wonderfulBtn"
    th:disabled="${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>
    </h6>

参考资料


问答平台(7),置顶、加精、删除
https://lcf163.github.io/2020/06/13/问答平台(7),置顶、加精、删除/
作者
乘风的小站
发布于
2020年6月13日
许可协议