articleserviceimpl.java

来自「实现留言薄和发表文章的功能」· Java 代码 · 共 65 行

JAVA
65
字号
package com.test.bbs.service.impl;

import java.util.Date;
import java.util.List;

import com.test.bbs.dao.ArticleDao;
import com.test.bbs.domain.Reply;
import com.test.bbs.domain.Subject;
import com.test.bbs.domain.User;
import com.test.bbs.exception.ServiceException;
import com.test.bbs.service.ArticleService;

public class ArticleServiceImpl implements ArticleService {

	private ArticleDao articleDao = null;

	public ArticleServiceImpl(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	public void addReply(Reply reply) {
		Subject subject = reply.getSubject();
		reply.setFloor(subject.getReplyCount() + 1);
		this.articleDao.addReply(reply);

		subject.setReplyCount(subject.getReplyCount() + 1);
		subject.setReplyDate(new Date());
		this.articleDao.updateSubject(subject);
	}

	public void addSubject(Subject subject) {
		this.articleDao.addSubject(subject);
	}

	public Reply delReply(User operator, Integer replyId) {
		Reply reply = this.articleDao.getReply(replyId);
		if (operator.getId() != reply.getAuthor().getId())
			throw new ServiceException("不能删除别人的回复");
		this.articleDao.delReply(reply);
		return reply;
	}

	public void delSubject(User operator, Integer subjectId) {
		Subject subject = this.articleDao.getSubject(subjectId);
		if (operator.getId() != subject.getAuthor().getId())
			throw new ServiceException("不能删除别人的文章");
		this.articleDao.delSubject(subject);
	}

	public Subject getSubject(Integer id) {
		Subject subject = this.articleDao.getSubject(id);
		if (subject == null)
			throw new ServiceException("文章" + id + "不存在");
		return subject;
	}

	public List getHotSubjects() {
		return this.articleDao.getHotSubjects();
	}

	public Page getPage(Subject subject, int pageNo, int pageSize) {
		return this.articleDao.getPage(subject, pageNo, pageSize);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?