package com.hzh.fitness.service.impl;
import com.hzh.fitness.common.GlobalConstant;
import com.hzh.fitness.exception.GlobalException;
import com.hzh.fitness.mapper.ArticleMapper;
import com.hzh.fitness.po.Article;
import com.hzh.fitness.po.ArticleImg;
import com.hzh.fitness.po.Comment;
import com.hzh.fitness.service.ArticleService;
import com.hzh.fitness.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author hzh
*/
@Service
public class ArticleServiceImpl implements ArticleService {
@Autowired
ArticleMapper articleMapper;
@Override
public Article searchArticle(int id) throws Exception {
return articleMapper.selectArticleById(id);
}
@Override
public Article createArticle(Article article) throws Exception {
byte[][] imgData = article.getImgData();
String[] imgs = article.getImg();
articleMapper.insertArticle(article);
if (imgs != null) {
ArticleImg[] articleImgs = new ArticleImg[imgs.length];
for (int i = 0; i < imgs.length; i++) {
String hex = FileUtils.sha1String(imgData[i]);
String suffix = "." + imgs[i].split("\\.")[1];
if (articleMapper.selectImgHex(hex) == null)
FileUtils.bytesToFile(imgData[i], GlobalConstant.IMAGE_ROOT + "/articleImg/", hex + suffix, false);
articleImgs[i] = new ArticleImg(article.getId(), hex + suffix, FileUtils.sha1String(imgData[i]));
}
articleMapper.insertArticleImg(articleImgs,article.getId());
}
return articleMapper.selectArticleById(article.getId());
}
@Override
public Article[] selectArticles(int id, String type, int page, int perPage) throws Exception {
return new Article[0];
}
@Override
public Article[] selectPersonalArticles(int id, int page, int perPage) throws Exception {
return articleMapper.selectPersonalArticles(id, page, perPage);
}
@Override
public Article[] selectFollowArticles(int id, int page, int perPage) throws Exception {
return articleMapper.selectFollowArticles(id, page, perPage);
}
@Override
public Article[] selectNewestArticles(int page, int perPage) throws Exception {
return articleMapper.selectNewestArticles(page, perPage);
}
@Override
public Article[] selectHotArticles(int page, int perPage) throws Exception {
return articleMapper.selectHotArticles(page, perPage);
}
@Override
public void likeArticle(int userId, int articleId) throws Exception {
int c = articleMapper.selectArticleLikeLog(userId, articleId);
int c2 = articleMapper.selectArticleId(articleId);
if (c == 1) {
throw new GlobalException("已经点赞过了");
}
if (c2 == 0) {
throw new GlobalException("不存在该动态");
}
articleMapper.updateLikeCountToArticle(articleId, 1);
articleMapper.insertLikeArticle(userId, articleId);
}
@Override
public void likeComment(int userId, int commentId) throws Exception {
int c = articleMapper.selectCommentLikeLog(userId, commentId);
int c2 = articleMapper.selectCommentId(commentId);
if (c == 1) {
throw new GlobalException("已经点赞过了");
}
if (c2 == 0) {
throw new GlobalException("不存在该评论");
}
articleMapper.insertLikeComment(userId, commentId);
articleMapper.updateLikeCountToComment(commentId, 1);
}
@Override
public void dislikeArticle(int userId, int articleId) throws Exception {
int c = articleMapper.selectArticleLikeLog(userId, articleId);
int c2 = articleMapper.selectArticleId(articleId);
if (c == 0) {
throw new GlobalException("还未点赞过");
}
if (c2 == 0) {
throw new GlobalException("不存在该动态");
}
articleMapper.updateLikeCountToArticle(articleId, -1);
articleMapper.deleteLikeArticle(userId, articleId);
}
@Override
public void dislikeComment(int userId, int commentId) throws Exception {
int c = articleMapper.selectCommentLikeLog(userId, commentId);
int c2 = articleMapper.selectCommentId(commentId);
if (c == 0) {
throw new GlobalException("还未点赞过");
}
if (c2 == 0) {
throw new GlobalException("不存在该评论");
}
articleMapper.deleteLikeComment(userId, commentId);
articleMapper.updateLikeCountToComment(commentId, -1);
}
@Override
public Comment createComment(Comment comment) throws Exception {
if (comment.getCommentId() != null && articleMapper.checkCommentById(comment.getCommentId()) == 0) {
throw new GlobalException("不存在该评论");
}
if (articleMapper.checkArticleById(comment.getArticleId()) == 0) {
throw new GlobalException("不存在该动态");
}
articleMapper.insertComment(comment);
articleMapper.updateCommentCount(comment.getArticleId());
return articleMapper.selectCommentById(comment.getId());
}
@Override
public Comment[] getSubComment(int commentId) throws Exception {
int c2 = articleMapper.selectCommentId(commentId);
if (c2 == 0) {
throw new GlobalException("不存在该评论");
}
return articleMapper.selectSubComment(commentId);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
新项目开发_基于java开发实现的一个健身app后端系统源码.7z (39个子文件)
新项目开发_基于java开发实现的一个健身app后端系统源码
pom.xml 7KB
src
main
webapp
WEB-INF
web.xml 2KB
index.jsp 52B
resources
log4j2.xml 3KB
config.properties 411B
spring
spring-mvc.xml 3KB
applicationContext.xml 6KB
mybatis
mybatis-config.xml 534B
java
com
hzh
fitness
exception
GlobalException.java 376B
GlobalExceptionHandler.java 976B
aspect
ControllerAspect.java 4KB
Ignore2Json.java 319B
po
User.java 2KB
Article.java 3KB
Comment.java 2KB
ArticleImg.java 860B
mapper
LoginMapper.xml 1KB
UserMapper.java 644B
ArticleMapper.java 2KB
LoginMapper.java 406B
UserMapper.xml 5KB
ArticleMapper.xml 9KB
common
LoginInfo.java 2KB
GlobalConstant.java 2KB
LoginList.java 2KB
MyResponse.java 2KB
ImgClean.java 2KB
controller
FileController.java 4KB
UserController.java 5KB
ArticleController.java 5KB
PythonController.java 830B
service
ArticleService.java 1KB
UserService.java 1KB
impl
ArticleServiceImpl.java 5KB
UserServiceImpl.java 3KB
utils
FileUtils.java 3KB
ImageType.java 2KB
listener
StartListener.java 2KB
.gitignore 89B
共 39 条
- 1
资源评论
m_466683585
- 粉丝: 5492
- 资源: 3568
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功