diff --git a/src/main/java/com/swpu/uchain/blog/dao/UserLikesMapper.java b/src/main/java/com/swpu/uchain/blog/dao/UserLikesMapper.java new file mode 100644 index 0000000..563009b --- /dev/null +++ b/src/main/java/com/swpu/uchain/blog/dao/UserLikesMapper.java @@ -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 selectAll(); + + int updateByPrimaryKey(UserLikes record); +} \ No newline at end of file diff --git a/src/main/java/com/swpu/uchain/blog/entity/UserLikes.java b/src/main/java/com/swpu/uchain/blog/entity/UserLikes.java new file mode 100644 index 0000000..d42db71 --- /dev/null +++ b/src/main/java/com/swpu/uchain/blog/entity/UserLikes.java @@ -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(); + } +} \ No newline at end of file diff --git a/src/main/java/com/swpu/uchain/blog/service/ArticleService.java b/src/main/java/com/swpu/uchain/blog/service/ArticleService.java index 7ac7f17..ce22195 100644 --- a/src/main/java/com/swpu/uchain/blog/service/ArticleService.java +++ b/src/main/java/com/swpu/uchain/blog/service/ArticleService.java @@ -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; @@ -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); } diff --git a/src/main/java/com/swpu/uchain/blog/service/impl/ArticleServiceImpl.java b/src/main/java/com/swpu/uchain/blog/service/impl/ArticleServiceImpl.java index 432cbb7..03e398b 100644 --- a/src/main/java/com/swpu/uchain/blog/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/swpu/uchain/blog/service/impl/ArticleServiceImpl.java @@ -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; @@ -18,6 +22,7 @@ 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; @@ -25,8 +30,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.io.File; -import java.io.IOException; import java.util.List; /** @@ -48,6 +51,9 @@ public class ArticleServiceImpl implements ArticleService { @Autowired private CommentMapper commentMapper; + @Autowired + private UserLikesMapper likesMapper; + @Autowired private UserService userService; @@ -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); @@ -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 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 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 list = articleMapper.selectArticlesByTagId(form.getTagId()); + PageInfo 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 list = articleMapper.selectByArticlesByTypeId(form.getTypeId()); + PageInfo 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; + } } diff --git a/src/main/resources/mappers/UserLikesMapper.xml b/src/main/resources/mappers/UserLikesMapper.xml new file mode 100644 index 0000000..00112cd --- /dev/null +++ b/src/main/resources/mappers/UserLikesMapper.xml @@ -0,0 +1,36 @@ + + + + + + + + + + delete from user_likes + where user_id = #{userId,jdbcType=BIGINT} + and blog_id = #{blogId,jdbcType=BIGINT} + + + insert into user_likes (user_id, blog_id, is_like + ) + values (#{userId,jdbcType=BIGINT}, #{blogId,jdbcType=BIGINT}, #{isLike,jdbcType=BIT} + ) + + + update user_likes + set is_like = #{isLike,jdbcType=BIT} + where user_id = #{userId,jdbcType=BIGINT} + and blog_id = #{blogId,jdbcType=BIGINT} + + + + \ No newline at end of file