原创文章不允许转载, 版权归开发俱乐部所有
dao(repository)层也写好了,下面开始来写service层
我们博客有五个功能块,用户、博客、评论、分类、标签,所以创建五个service
- UserService
- PostService
- CommentService
- CategoryService
- TagService
注解说明
注解 | 说明 |
---|---|
@Service | 标注这是一个服务类,这个注解会被Spring的IOC扫描并管理 |
@Transactional | 事务注解,我这里偷懒,标注在类上了,也可以放在方法上,如果方法里执行数据操作出错了,会回滚保证数据的准备性 |
UserService
用户服务类,里面就一个方法,用户登录,不多说
@Service
@Transactional
public class UserService {
@Autowired
private UserRepository userRepository;
public User login(String username, String password) {
User user = userRepository.findByUsernameAndPassword(username, password);
return user;
}
}
PostService
博客相关操作方法都在这里
@Service
@Transactional
public class PostService {
@Autowired
private PostRepository postRepository;
@Autowired
private CommentRepository commentRepository;
// 分页查询博客
public Page<Post> page(Integer pageNo, Integer pageSize) {
Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Direction.DESC, "inTime");
return postRepository.findAll(pageable);
}
// 发布博客
public Post createPost(Integer userId, String title, String content, String categories, String tags) {
Post post = new Post();
post.setUserId(userId);
post.setTitle(title);
post.setContent(content);
post.setView(0);
post.setCommentCount(0);
post.setCategories(categories);
post.setTags(tags);
post.setInTime(new Date());
return postRepository.save(post);
}
// 更新博客
public void update(Integer id, String title, String content, String categories, String tags) {
Post post = postRepository.findById(id).orElse(null);
if (post != null) {
post.setTitle(title);
post.setContent(content);
post.setCategories(categories);
post.setTags(tags);
post.setModifyTime(new Date());
postRepository.save(post);
}
}
// 只查询博客供修改方法用
public Post findByIdForUpdate(Integer id) {
return postRepository.findById(id).orElse(null);
}
// 根据博客id,查询出博客以及评论
public Map<String, Object> findById(Integer id) {
Map<String, Object> map = new HashMap<>();
Post post = postRepository.findById(id).orElse(null);
if (post != null) {
// 查看数+1
post.setView(post.getView() + 1);
postRepository.save(post);
map.put("post", post);
// 查询出博客下的评论
List<Comment> comments = commentRepository.findByPostId(id);
map.put("comments", comments);
}
return map;
}
public void deleteById(Integer id) {
postRepository.deleteById(id);
}
public List<Post> findPostByTitleAndUser(String title, Integer userId) {
Post post = new Post();
if (userId != null) {
post.setUserId(userId);
}
if (!StringUtils.isEmpty(title)) {
post.setTitle(title);
}
return postRepository.findAll(Example.of(post));
}
}
CommentService
游客可以发布评论,博主可以删除评论,所以只有两个方法
@Service
@Transactional
public class CommentService {
@Autowired
private CommentRepository commentRepository;
@Autowired
private PostRepository postRepository;
// 发布评论
public Comment createComment(Integer postId, String content, String username, String email, String url) {
Post post = postRepository.findById(postId).orElse(null);
if (post != null) {
Comment comment = new Comment();
comment.setContent(content);
comment.setInTime(new Date());
comment.setPostId(postId);
comment.setUsername(username);
comment.setEmail(email);
comment.setUrl(url);
commentRepository.save(comment);
// 将博客的评论数+1
post.setCommentCount(post.getCommentCount() + 1);
postRepository.save(post);
return comment;
}
return null;
}
// 删除评论
public void deleteById(Integer id) {
Comment comment = commentRepository.findById(id).orElse(null);
if (comment != null) {
// 将博客的评论数-1
Post post = postRepository.findById(comment.getPostId()).get();
post.setCommentCount(post.getCommentCount() - 1);
postRepository.save(post);
// 删除评论
commentRepository.deleteById(id);
}
}
}
CategoryService TagService
分类跟标签这里说一下,前一节没有创建它们相关的实体类,也就是说它们没有表结构储存,我这里实现的逻辑是从博客里取出来,然后组装好后交给页面渲染,具体看下面代码
@Service
public class CategoryService {
@Autowired
private PostRepository postRepository;
/**
* 统计所有的分类,从每一篇博客的分类字段里拿,然后封装成一个Map
*/
public Map<String, List<Post>> findAllCategory() {
// 首先查出所有的博客
List<Post> posts = postRepository.findAll();
// 新建一个Map用来装分类
Map<String, List<Post>> categories = new HashMap<>();
for (Post post : posts) {
// 将分类从博客里取出来然后通过,分割,然后再循环
String[] _categories = post.getCategories().split(",");
for (String _category : _categories) {
// 判断分类map里有没有这个分类,有的话就取出map里分类对应的value,再将当前这个post存进去
if (categories.containsKey(_category)) {
categories.get(_category).add(post);
} else {
// 如果没有这个分类,就新建一个列表,把当前post放进去,最后再put进分类map里
List<Post> _posts = new ArrayList<>();
_posts.add(post);
categories.put(_category, _posts);
}
}
}
// 最后将分类map返回
return categories;
}
}
标签跟分类的处理逻辑一样
/**
* 统计所有的标签,从每一篇博客的标签字段里拿,然后封装成一个Map
*/
public Map<String, List<Post>> findAllTag() {
// 首先查出所有的博客
List<Post> posts = postRepository.findAll();
// 新建一个Map用来装标签
Map<String, List<Post>> tags = new HashMap<>();
for (Post post : posts) {
String[] _tags = post.getTags().split(",");
for (String _tag : _tags) {
if (tags.containsKey(_tag)) {
tags.get(_tag).add(post);
} else {
List<Post> _posts = new ArrayList<>();
_posts.add(post);
tags.put(_tag, _posts);
}
}
}
return tags;
}
好了,准备工作到现在就完成了,下一节可以开始写页面了
目前还没有回答,快来帮帮TA吧!