问答平台(4),收到的赞

重构点赞功能

1
2
- 以用户为key,记录点赞数量
- increment(key)、decrement(key)

开发个人主页

收到的赞-图示

1
- 以用户为key,查询点赞数量

工具类

  • RedisKeyUtil:增加内容
    1
    2
    3
    4
    5
    6
    private static final String PREFIX_USER_LIKE = "like:user";
    // 某个用户的赞
    // like:user:userId -> int
    public static String getUserLikeKey(int userId) {
    return PREFIX_USER_LIKE + SPLIT + userId;
    }

业务层

  • LikeService:重构,添加一个参数(entityUserId)
    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
    // 点赞
    public void like(int userId, int entityType, int entityId, int entityUserId) {
    redisTemplate.execute(new SessionCallback() {
    @Override
    public Object execute(RedisOperations operations) throws DataAccessException {
    String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
    // 添加
    String userLikeKey = RedisKeyUtil.getUserLikeKey(entityUserId);

    boolean isMember = operations.opsForSet().isMember(entityLikeKey, userId);

    // 开启事务
    operations.multi();

    if (isMember) {
    operations.opsForSet().remove(entityLikeKey, userId);
    operations.opsForValue().decrement(userLikeKey);
    } else {
    operations.opsForSet().add(entityLikeKey, userId);
    operations.opsForValue().increment(userLikeKey);
    }

    return operations.exec();
    }
    });
    }

    // 查询某个用户获得的赞
    public int findUserLikeCount(int userId) {
    String userLikeKey = RedisKeyUtil.getUserLikeKey(userId);
    Integer count = (Integer) redisTemplate.opsForValue().get(userLikeKey);
    return count == null ? 0 : count.intValue();
    }

表现层

  • LikeController:重构,添加一个参数(entityUserId)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    @RequestMapping(path = "/like", method = RequestMethod.POST)
    @ResponseBody
    public String like(int entityType, int entityId, int entityUserId) {
    User user = hostHolder.getUser();

    // 点赞
    likeService.like(user.getId(), entityType, entityId, entityUserId);
    // 数量
    long likeCount = likeService.findEntityLikeCount(entityType, entityId);
    // 状态
    int likeStatus = likeService.findEntityLikeStatus(user.getId(), entityType, entityId);
    // 返回的结果
    Map<String, Object> map = new HashMap<>();
    map.put("likeCount", likeCount);
    map.put("likeStatus", likeStatus);

    return CommunityUtil.getJSONString(0, null, map);
    }
  • UserController:新增方法
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // 个人主页
    @RequestMapping(path = "/profile/{userId}", method = RequestMethod.GET)
    public String getProfilePage(@PathVariable("userId") int userId, Model model) {
    User user = userService.findUserById(userId);
    if (user == null) {
    throw new RuntimeException("该用户不存在!");
    }

    // 用户
    model.addAttribute("user", user);
    // 点赞数量
    int likeCount = likeService.findUserLikeCount(userId);
    model.addAttribute("likeCount", likeCount);

    return "/site/profile";
    }

页面

  • discuss-detail.html
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <!-- 内容 -->
    <!-- 作者 -->
    <li class="d-inline ml-2">
    <a href="javascript:;" th:onclick="|like(this,1,${post.id},${post.userId});|" class="text-primary">
    <b th:text="${likeStatus==1?'已赞':'赞'}"></b> <i th:text="${likeCount}">11</i>
    </a>
    </li>
    <!-- 回帖列表 -->
    <li class="d-inline ml-2">
    <a href="javascript:;" th:onclick="|like(this,2,${cvo.comment.id},${cvo.comment.userId});|" class="text-primary">
    <b th:text="${cvo.likeStatus==1?'已赞':'赞'}"></b> (<i th:text="${cvo.likeCount}">1</i>)
    </a>
    </li>
    <!-- 回复列表 -->
    <li class="d-inline ml-2">
    <a href="javascript:;" th:onclick="|like(this,2,${rvo.reply.id},${rvo.reply.userId});|" class="text-primary">
    <b th:text="${rvo.likeStatus==1?'已赞':'赞'}"></b> (<i th:text="${rvo.likeCount}">1</i>)
    </a>
    </li>
  • discuss.js:重构,添加一个参数(entityUserId)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    function like(btn, entityType, entityId, entityUserId) {
    $.post(
    CONTEXT_PATH + "/like",
    {"entityType": entityType, "entityId": entityId, "entityUserId": entityUserId},
    function (data) {
    data = $.parseJSON(data);
    if (data.code == 0) {
    $(btn).children("i").text(data.likeCount);
    $(btn).children("b").text(data.likeStatus == 1 ? '已赞' : '赞');
    } else {
    alert(data.msg);
    }
    }
    );
    }
  • index.html
    1
    2
    3
    4
    5
    6
    7
    8
    <!-- 头部 -->
    <!-- 功能 -->
    <a class="dropdown-item text-center"
    th:href="@{|/user/profile/${loginUser.id}|}">个人主页</a>
    <!-- 帖子列表 -->
    <a th:href="@{|/user/profile/${map.user.id}|}">
    <img th:src="${map.user.headerUrl}" class="mr-4 rounded-circle" alt="用户头像" style="width:50px;height:50px;">
    </a>
  • profile.html
    1
    2
    3
    4
    5
    6
    <!-- 内容 -->
    <!-- 个人信息 -->
    <img th:src="${user.headerUrl}" class="align-self-start mr-4 rounded-circle" alt="用户头像" style="width:50px;">
    <span th:utext="${user.username}">nowcoder</span>
    <span>注册于 <i class="text-muted" th:text="${#dates.format(user.createTime, 'yyyy-MM-dd HH:mm:ss')}">2015-06-12 15:20:12</i></span>
    <span class="ml-4">获得了 <i class="text-danger" th:text="${likeCount}">87</i> 个赞</span>

问答平台(4),收到的赞
https://lcf163.github.io/2020/05/25/问答平台(4),收到的赞/
作者
乘风的小站
发布于
2020年5月25日
许可协议