mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-24 15:10:48 +08:00
73 lines
2.8 KiB
Java
73 lines
2.8 KiB
Java
package com.openisle.service;
|
|
|
|
import com.openisle.model.Post;
|
|
import com.openisle.model.PostStatus;
|
|
import com.openisle.model.PublishMode;
|
|
import com.openisle.model.User;
|
|
import com.openisle.model.Category;
|
|
import com.openisle.repository.PostRepository;
|
|
import com.openisle.repository.UserRepository;
|
|
import com.openisle.repository.CategoryRepository;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.List;
|
|
|
|
@Service
|
|
public class PostService {
|
|
private final PostRepository postRepository;
|
|
private final UserRepository userRepository;
|
|
private final CategoryRepository categoryRepository;
|
|
private final PublishMode publishMode;
|
|
|
|
@org.springframework.beans.factory.annotation.Autowired
|
|
public PostService(PostRepository postRepository,
|
|
UserRepository userRepository,
|
|
CategoryRepository categoryRepository,
|
|
@Value("${app.post.publish-mode:DIRECT}") PublishMode publishMode) {
|
|
this.postRepository = postRepository;
|
|
this.userRepository = userRepository;
|
|
this.categoryRepository = categoryRepository;
|
|
this.publishMode = publishMode;
|
|
}
|
|
|
|
public Post createPost(String username, Long categoryId, String title, String content) {
|
|
User author = userRepository.findByUsername(username)
|
|
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
|
Category category = categoryRepository.findById(categoryId)
|
|
.orElseThrow(() -> new IllegalArgumentException("Category not found"));
|
|
Post post = new Post();
|
|
post.setTitle(title);
|
|
post.setContent(content);
|
|
post.setAuthor(author);
|
|
post.setCategory(category);
|
|
post.setStatus(publishMode == PublishMode.REVIEW ? PostStatus.PENDING : PostStatus.PUBLISHED);
|
|
return postRepository.save(post);
|
|
}
|
|
|
|
public Post getPost(Long id) {
|
|
Post post = postRepository.findById(id)
|
|
.orElseThrow(() -> new IllegalArgumentException("Post not found"));
|
|
if (post.getStatus() != PostStatus.PUBLISHED) {
|
|
throw new IllegalArgumentException("Post not found");
|
|
}
|
|
post.setViews(post.getViews() + 1);
|
|
return postRepository.save(post);
|
|
}
|
|
|
|
public List<Post> listPosts() {
|
|
return postRepository.findByStatus(PostStatus.PUBLISHED);
|
|
}
|
|
|
|
public List<Post> listPendingPosts() {
|
|
return postRepository.findByStatus(PostStatus.PENDING);
|
|
}
|
|
|
|
public Post approvePost(Long id) {
|
|
Post post = postRepository.findById(id)
|
|
.orElseThrow(() -> new IllegalArgumentException("Post not found"));
|
|
post.setStatus(PostStatus.PUBLISHED);
|
|
return postRepository.save(post);
|
|
}
|
|
}
|