Skip to content

Commit

Permalink
新增点赞功能
Browse files Browse the repository at this point in the history
  • Loading branch information
TreasureJade committed Dec 18, 2019
1 parent 610c39f commit 56da7b7
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 19 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/swpu/uchain/blog/dao/UserLikesMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.swpu.uchain.blog.dao;

import com.swpu.uchain.blog.entity.UserLikes;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface UserLikesMapper {
int deleteByPrimaryKey(@Param("userId") Long userId, @Param("blogId") Long blogId);

int insert(UserLikes record);

UserLikes selectByPrimaryKey(@Param("userId") Long userId, @Param("blogId") Long blogId);

List<UserLikes> selectAll();

int updateByPrimaryKey(UserLikes record);
}
51 changes: 51 additions & 0 deletions src/main/java/com/swpu/uchain/blog/entity/UserLikes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.swpu.uchain.blog.entity;

import java.io.Serializable;

public class UserLikes implements Serializable {
private Long userId;

private Long blogId;

private Boolean isLike;

private static final long serialVersionUID = 1L;

public Long getUserId() {
return userId;
}

public void setUserId(Long userId) {
this.userId = userId;
}

public Long getBlogId() {
return blogId;
}

public void setBlogId(Long blogId) {
this.blogId = blogId;
}

public Boolean getIsLike() {
return isLike;
}

public void setIsLike(Boolean isLike) {
this.isLike = isLike;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", userId=").append(userId);
sb.append(", blogId=").append(blogId);
sb.append(", isLike=").append(isLike);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
18 changes: 14 additions & 4 deletions src/main/java/com/swpu/uchain/blog/service/ArticleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.swpu.uchain.blog.entity.Article;
import com.swpu.uchain.blog.form.CreatArticleForm;
import com.swpu.uchain.blog.form.SelectByTagForm;
import com.swpu.uchain.blog.form.SelectByTypeForm;
import com.swpu.uchain.blog.form.UpdateArticleForm;
import com.swpu.uchain.blog.vo.ResultVO;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -90,16 +92,24 @@ public interface ArticleService {
/**
* 根据标签查询文章
*
* @param tagId
* @param form
* @return
*/
ResultVO selectArticleByTags(Integer tagId);
ResultVO selectArticleByTags(SelectByTagForm form);

/**
* 根据种类查询文章
*
* @param typeId
* @param form
* @return
*/
ResultVO selectArticleByTypes(SelectByTypeForm form);

/**
* 点赞/取消点赞
* @param blogId
* @param isLike
* @return
*/
ResultVO selectArticleByTypes(Integer typeId);
ResultVO likeArticle(long blogId,boolean isLike);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import com.github.pagehelper.PageInfo;
import com.swpu.uchain.blog.dao.ArticleMapper;
import com.swpu.uchain.blog.dao.CommentMapper;
import com.swpu.uchain.blog.dto.ArticleDTO;
import com.swpu.uchain.blog.dao.UserLikesMapper;
import com.swpu.uchain.blog.entity.Article;
import com.swpu.uchain.blog.entity.User;
import com.swpu.uchain.blog.entity.UserLikes;
import com.swpu.uchain.blog.enums.ResultEnum;
import com.swpu.uchain.blog.exception.GlobalException;
import com.swpu.uchain.blog.form.CreatArticleForm;
import com.swpu.uchain.blog.form.SelectByTagForm;
import com.swpu.uchain.blog.form.SelectByTypeForm;
import com.swpu.uchain.blog.form.UpdateArticleForm;
import com.swpu.uchain.blog.redis.RedisService;
import com.swpu.uchain.blog.redis.key.ArticleKey;
Expand All @@ -18,15 +22,14 @@
import com.swpu.uchain.blog.util.ResultVOUtil;
import com.swpu.uchain.blog.util.TimeUtil;
import com.swpu.uchain.blog.vo.ArticleListVO;
import com.swpu.uchain.blog.vo.ArticleVO;
import com.swpu.uchain.blog.vo.CommentVO;
import com.swpu.uchain.blog.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
Expand All @@ -48,6 +51,9 @@ public class ArticleServiceImpl implements ArticleService {
@Autowired
private CommentMapper commentMapper;

@Autowired
private UserLikesMapper likesMapper;

@Autowired
private UserService userService;

Expand Down Expand Up @@ -103,8 +109,8 @@ public ResultVO updateArticle(UpdateArticleForm form) {
if (article == null) {
return ResultVOUtil.error(ResultEnum.ARTICLE_NOT_EXIST);
}
BeanUtils.copyProperties(form,article);
String updateTime = TimeUtil.getNowTime();
BeanUtils.copyProperties(form, article);
String updateTime = TimeUtil.getTimeCN();
article.setUpdateTime(updateTime);
if (update(article)) {
return ResultVOUtil.success(article);
Expand Down Expand Up @@ -149,31 +155,97 @@ public void addReading(Long id) {

@Override
public ResultVO selectArticleDetail(Long blogId) {
Article article = articleMapper.selectByPrimaryKey(blogId);
ArticleVO article = articleMapper.selectByArticleId(blogId);
if (article == null) {
return ResultVOUtil.error(ResultEnum.ARTICLE_NOT_EXIST);
}
List<CommentVO> commentList = commentService.getAllCommentByBlogId(blogId);
ArticleDTO articleDTO = new ArticleDTO();
BeanUtils.copyProperties(article, articleDTO);
articleDTO.setCommentList(commentList);
// 阅读量 +1
addReading(blogId);
return ResultVOUtil.success(articleDTO);
User user = userService.getCurrentUser();
if (user != null) {
UserLikes userLikes = likesMapper.selectByPrimaryKey(user.getUserId(), blogId);
if (userLikes != null) {
article.setIsLike(userLikes.getIsLike());
} else {
article.setIsLike(false);
}
}else {
article.setIsLike(false);
}
List<CommentVO> commentList = commentService.getAllCommentByBlogId(blogId);
article.setCommentList(commentList);

return ResultVOUtil.success(article);
}


@Override
public ResultVO selectArticleByTags(Integer tagId) {
ArticleListVO list = articleMapper.selectArticlesByTagId(tagId);
public ResultVO selectArticleByTags(SelectByTagForm form) {
PageHelper.startPage(form.getPageNum(), form.getPageSize());
List<ArticleListVO> list = articleMapper.selectArticlesByTagId(form.getTagId());
PageInfo<ArticleListVO> result = new PageInfo<>(list);
return ResultVOUtil.success(list);
}

@Override
public ResultVO selectArticleByTypes(Integer typeId) {
ArticleListVO list = articleMapper.selectByArticlesByTypeId(typeId);
public ResultVO selectArticleByTypes(SelectByTypeForm form) {
PageHelper.startPage(form.getPageNum(), form.getPageSize());
List<ArticleListVO> list = articleMapper.selectByArticlesByTypeId(form.getTypeId());
PageInfo<ArticleListVO> result = new PageInfo<>(list);
return ResultVOUtil.success(list);
}

@Override
public ResultVO likeArticle(long blogId, boolean isLike) {
User user = userService.getCurrentUser();
if (user == null) {
return ResultVOUtil.error(ResultEnum.USER_NOT_LOGIN);
}
UserLikes userLikes = likesMapper.selectByPrimaryKey(user.getUserId(), blogId);
// 点赞
if (isLike) {
if (userLikes == null) {
UserLikes like = new UserLikes();
like.setUserId(user.getUserId());
like.setBlogId(blogId);
like.setIsLike(isLike);
if (likesMapper.insert(like) == 1) {
if (updateLike(blogId, isLike)) {
return ResultVOUtil.success();
}
}
} else {
userLikes.setIsLike(isLike);
if (likesMapper.updateByPrimaryKey(userLikes) == 1) {
if (updateLike(blogId, isLike)) {
return ResultVOUtil.success();
}
}
}
} else {
userLikes.setIsLike(isLike);
if (likesMapper.updateByPrimaryKey(userLikes) == 1) {
if (updateLike(blogId, isLike)) {
return ResultVOUtil.success();
}
}
}

return ResultVOUtil.error(ResultEnum.SERVER_ERROR);
}

private boolean updateLike(long blogId, boolean isLike) {
Article article = articleMapper.selectByPrimaryKey(blogId);
if (article != null) {
if (isLike) {
article.setLikes(article.getLikes() + 1);
return update(article);
} else {
article.setLikes(article.getLikes() - 1);
return update(article);
}
}
return false;
}

}
36 changes: 36 additions & 0 deletions src/main/resources/mappers/UserLikesMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.swpu.uchain.blog.dao.UserLikesMapper" >
<resultMap id="BaseResultMap" type="com.swpu.uchain.blog.entity.UserLikes" >
<id column="user_id" property="userId" jdbcType="BIGINT" />
<id column="blog_id" property="blogId" jdbcType="BIGINT" />
<result column="is_like" property="isLike" jdbcType="BIT" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="map" >
delete from user_likes
where user_id = #{userId,jdbcType=BIGINT}
and blog_id = #{blogId,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="com.swpu.uchain.blog.entity.UserLikes" >
insert into user_likes (user_id, blog_id, is_like
)
values (#{userId,jdbcType=BIGINT}, #{blogId,jdbcType=BIGINT}, #{isLike,jdbcType=BIT}
)
</insert>
<update id="updateByPrimaryKey" parameterType="com.swpu.uchain.blog.entity.UserLikes" >
update user_likes
set is_like = #{isLike,jdbcType=BIT}
where user_id = #{userId,jdbcType=BIGINT}
and blog_id = #{blogId,jdbcType=BIGINT}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="map" >
select user_id, blog_id, is_like
from user_likes
where user_id = #{userId,jdbcType=BIGINT}
and blog_id = #{blogId,jdbcType=BIGINT}
</select>
<select id="selectAll" resultMap="BaseResultMap" >
select user_id, blog_id, is_like
from user_likes
</select>
</mapper>

0 comments on commit 56da7b7

Please sign in to comment.