57 lines
1.7 KiB
Java
57 lines
1.7 KiB
Java
package com.guams.review.service;
|
|
|
|
import com.guams.review.model.AuthorRepository;
|
|
import com.guams.review.model.PostRepository;
|
|
import com.guams.review.model.dao.Author;
|
|
import com.guams.review.model.dao.Post;
|
|
import com.guams.review.service.mapper.Mapper;
|
|
import com.guams.review.service.mapper.ReturnableAuthor;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
import java.util.UUID;
|
|
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class AuthorService
|
|
{
|
|
private final Mapper mapper;
|
|
private final AuthorRepository authorRepository;
|
|
private final PostRepository postRepository;
|
|
|
|
public List<ReturnableAuthor> list() {
|
|
return authorRepository.findAll().stream()
|
|
.map(mapper::mapAuthor)
|
|
.toList();
|
|
}
|
|
|
|
public List<Post> listPublicationOfAuthor(UUID authorId) {
|
|
return postRepository.findPostsOfAuthor(authorId);
|
|
}
|
|
|
|
public Optional<Author> findById(UUID id) {
|
|
return authorRepository.findById(id);
|
|
}
|
|
|
|
public ReturnableAuthor insert(Author author) {
|
|
return mapper.mapAuthor(authorRepository.save(author));
|
|
}
|
|
|
|
@Transactional
|
|
public void insertPublications(UUID authorId, List<Long> postIds) {
|
|
for (Long postId : postIds) {
|
|
authorRepository.deletePublication(authorId, postId);
|
|
if (postRepository.findById(postId).isPresent()) {
|
|
authorRepository.insertPublication(authorId, postId);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void delete(Author author) {
|
|
authorRepository.delete(author);
|
|
}
|
|
}
|