package com.aaa.forum.services.impl;
import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.dao.ArticleMapper;
import com.aaa.forum.exception.ApplicationException;
import com.aaa.forum.model.Article;
import com.aaa.forum.model.Board;
import com.aaa.forum.model.User;
import com.aaa.forum.services.IArticleService;
import com.aaa.forum.services.IBoardService;
import com.aaa.forum.services.IHistoryService;
import com.aaa.forum.services.IUserService;
import com.aaa.forum.uitls.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {
@Resource
private ArticleMapper articleMapper;
// 注入 userService 和 boardService
// 注意不要循环引用, 虽然Spring可以处理循环引用, 但是尽量不要写这样的代码
@Resource
private IUserService userService;
@Resource
private IBoardService boardService;
@Resource
private IHistoryService historyService;
@Override
public void create(Article article) {
// 参数校验
if (article == null || article.getUserId() == null || article.getBoardId() == null
|| StringUtil.isEmpty(article.getTitle()) || StringUtil.isEmpty(article.getContent())) {
log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
}
// 设置默认值
article.setVisitCount(0); // 访问数量
article.setLikeCount(0); // 点赞数量
article.setReplyCount(0); // 回复数量
article.setMarkCount(0); // 收藏数量
article.setState((byte) 1); // 待审核状态
article.setDeleteState((byte) 0);
Date date = new Date();
article.setCreateTime(date); // 创建日期
article.setUpdateTime(date); // 更新日期
// 写入数据库
int row = articleMapper.insertSelective(article);
if (row != 1) {
log.warn(ResultCode.FAILED_CREATE.toString());
throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
}
log.info("发帖成功, userId = " + article.getUserId()
+ ", boardId = " + article.getBoardId()
+ ", articleId = " + article.getId());
// 文章id是数据库自动生成的主键,在mapper中设置了,插入成功后,会拿到这个自动生成的id
}
@Override
public List<Article> selectAll() {
return articleMapper.selectAll();
}
@Override
public List<Article> selectAllByBoardId(Long boardId) {
// 非空校验
if (boardId == null || boardId <= 0) {
log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
}
// 校验板块是否存在
Board board = boardService.selectById(boardId);
if (board == null || board.getDeleteState() == 1) {
log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
}
// 调用dao, 查询并返回结果
return articleMapper.selectAllByBoardId(boardId);
}
@Override
public Article selectDetailById(Long userId, Long articleId) {
// 非空校验
if (userId == null || userId <= 0 || articleId == null || articleId <= 0) {
log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
}
// 调用dao, 查询帖子详情
Article article = articleMapper.selectDetailById(articleId);
// 校验是否为空
if (article == null || article.getDeleteState() == 1) {
log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
}
// 更新帖子访问数
Article updateArticle = new Article();
updateArticle.setId(article.getId());
updateArticle.setVisitCount(article.getVisitCount() + 1);
int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
// 校验是否更新成功
if (row != 1) {
log.warn(ResultCode.ERROR_SERVICES.toString());
throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
}
// 更新历史记录表
historyService.update(userId, articleId);
// 更新返回结果的访问数量
article.setVisitCount(updateArticle.getVisitCount());
return article;
}
@Override
public void modify(Long id, String title, String content, Long boardId) {
// 非空校验
if (id == null || id <= 0 || StringUtil.isEmpty(title) || StringUtil.isEmpty(content)) {
log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
}
// 查询帖子,校验是否为空
Article article = articleMapper.selectByPrimaryKey(id);
if (article == null || article.getDeleteState() == 1) {
log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
}
// 构造要更新的帖子对象
Article updateArticle = new Article();
updateArticle.setId(id);
updateArticle.setTitle(title);
updateArticle.setContent(content);
updateArticle.setBoardId(boardId);
updateArticle.setUpdateTime(new Date());
updateArticle.setState((byte) 1);
// 调用dao, 更新数据库
int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
if (row != 1) {
log.warn(ResultCode.ERROR_SERVICES.toString());
throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
}
if (article.getState() == 0) {
// 更新用户发帖数量
User existsUser = userService.selectById(article.getUserId());
if (existsUser != null && existsUser.getDeleteState() == 0) {
// 用户未被删除
userService.subOneArticleCountById(article.getUserId());
}
// 更新板块帖子数量
Board existsBoard = boardService.selectById(article.getBoardId());
if (existsBoard != null && existsBoard.getDeleteState() == 0) {
// 板块未被删除
boardService.subOneArticleCountById(article.getBoardId());
}
}
}
@Override
public Article selectById(Long id) {
if (id == null || id <= 0) {
log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
}
return articleMapper.selectByPrimaryKey(id);
}
@Override
public void deleteById(Long id) {
// 1. 参数校验
if (id == null || id <= 0) {
log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
}
// 2. 查询id对应的帖子信息
Article article = articleMapper.selectByPrimaryKey(id);
if (article == null || article.getDeleteState() == 1) {
log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS + ", articleId = " + id);
throw new ApplicationE
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本Java论坛系统基于Spring开发,包含1292个文件,包括SVG图形、JavaScript脚本、CSS样式表、HTML页面、Java源代码、JSON配置文件、Map文件、XML配置文件、PNG图片和SCSS样式表。系统界面友好,功能完善,适合用于论坛管理。
资源推荐
资源详情
资源评论
收起资源包目录
基于Spring的Java论坛系统设计源码 (1288个子文件)
AUTHORS 6KB
tabler.css 577KB
tabler.rtl.css 577KB
tabler.rtl.min.css 487KB
tabler.min.css 487KB
skin.css 78KB
skin.css 78KB
editormd.css 76KB
skin.css 75KB
skin.css 75KB
skin.min.css 66KB
skin.min.css 66KB
skin.min.css 63KB
skin.min.css 63KB
editormd.min.css 60KB
editormd.preview.css 55KB
editormd.preview.min.css 44KB
plyr.css 34KB
tabler-vendors.rtl.css 32KB
tabler-vendors.css 32KB
tabler-vendors.rtl.min.css 27KB
tabler-vendors.min.css 27KB
ambiance.css 26KB
content.css 25KB
content.css 25KB
content.inline.css 25KB
content.inline.css 25KB
content.inline.css 25KB
content.inline.css 25KB
content.css 25KB
content.css 25KB
content.min.css 23KB
content.min.css 23KB
content.inline.min.css 23KB
content.inline.min.css 23KB
content.inline.min.css 23KB
content.inline.min.css 23KB
content.min.css 22KB
content.min.css 22KB
tabler-flags.css 18KB
tabler-flags.rtl.css 18KB
tabler-flags.min.css 16KB
tabler-flags.rtl.min.css 16KB
apexcharts.css 12KB
tabler-payments.rtl.css 12KB
tabler-payments.css 12KB
litepicker.css 11KB
tabler-payments.rtl.min.css 10KB
tabler-payments.min.css 10KB
codemirror.css 8KB
nouislider.css 6KB
codemirror.min.css 5KB
mdn-like.css 5KB
jquery.toast.css 5KB
solarized.css 5KB
dropzone.css 5KB
nouislider.min.css 4KB
mobilefriendly.js.css 4KB
ranges.js.css 3KB
merge.css 3KB
lint.css 3KB
xq-dark.css 3KB
lesser-dark.css 2KB
pastel-on-dark.css 2KB
xq-light.css 2KB
tomorrow-night-eighties.css 2KB
editormd.logo.css 2KB
erlang-dark.css 2KB
zenburn.css 2KB
twilight.css 2KB
midnight.css 2KB
vibrant-ink.css 2KB
mbo.css 2KB
base16-dark.css 2KB
base16-light.css 2KB
tern.css 2KB
content.css 2KB
3024-night.css 2KB
paraiso-dark.css 2KB
paraiso-light.css 2KB
content.css 2KB
content.css 2KB
tomorrow-night-bright.css 2KB
3024-day.css 2KB
content.css 2KB
blackboard.css 2KB
content.css 2KB
content.css 2KB
colorforth.css 2KB
editormd.logo.min.css 2KB
the-matrix.css 2KB
multiselect.js.css 2KB
night.css 2KB
rubyblue.css 2KB
monokai.css 1KB
cobalt.css 1KB
simplescrollbars.css 1KB
content.min.css 1KB
content.min.css 1KB
content.min.css 1KB
共 1288 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
资源评论
沐知全栈开发
- 粉丝: 4779
- 资源: 4627
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功