package ru.mcs.sopds.service;
import org.springframework.data.jpa.repository.Query;
import ru.mcs.sopds.entity.*;
import ru.mcs.sopds.repository.*;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class BookService {
private final BookRepository bookRepository;
private final AuthorRepository authorRepository;
private final GenreRepository genreRepository;
private final SeriesRepository seriesRepository;
public Long getBooksCount() {
return bookRepository.count();
}
public Long getAuthorsCount() {
return authorRepository.count();
}
public Long getSeriesCount() {
return seriesRepository.count();
}
public Long getGenresCount() {
return genreRepository.count();
}
public List<Book> getRecentBooks(int limit) {
return bookRepository.findTopNByOrderByRegisterDateDesc(PageRequest.of(0, limit));
}
public List<Genre> getPopularGenres(int limit) {
List<Genre> genres = genreRepository.findPopularGenres(limit);
// Для нативных запросов нам нужно установить bookCount вручную
// или использовать Projection/DTO
return genres;
}
public Page<Book> getAllBooks(Pageable pageable) {
return bookRepository.findAll(pageable);
}
public Page<Author> getAllAuthors(Pageable pageable) {
return authorRepository.findAll(pageable);
}
public Page<Author> getAuthorsByLetter(String letter, Pageable pageable) {
return authorRepository.findByFullNameStartingWith(letter, pageable);
}
public List<Genre> getAllGenres() {
return genreRepository.findAll();
}
public Page<Series> getAllSeries(Pageable pageable) {
return seriesRepository.findAll(pageable);
}
public Book getBookById(Long id) {
return bookRepository.findById(id).orElse(null);
}
public Author getAuthorById(Long id) {
return authorRepository.findById(id).orElse(null);
}
public Series getSeriesById(Long id) {
return seriesRepository.findById(id).orElse(null);
}
public Genre getGenreById(Long id) {
return genreRepository.findById(id).orElse(null);
}
public Page<Book> getBooksByAuthor(Long authorId, Pageable pageable) {
return bookRepository.findByAuthorsId(authorId, pageable);
}
public Page<Book> getBooksBySeries(Long seriesId, Pageable pageable) {
return bookRepository.findBySeriesId(seriesId, pageable);
}
public Page<Book> getBooksByGenre(Long genreId, Pageable pageable) {
return bookRepository.findByGenresId(genreId, pageable);
}
public Page<Book> searchBooks(String query, Pageable pageable) {
return bookRepository.findByTitleContainingIgnoreCase(query, pageable);
}
public Page<Book> getBooksByLanguage(String lang, Pageable pageable) {
return bookRepository.findByLang(lang, pageable);
}
public Page<Author> getAuthorsByLanguage(String lang, Pageable pageable) {
return authorRepository.findByBooksLang(lang, pageable);
}
public Page<Series> getSeriesByLanguage(String lang, Pageable pageable) {
return seriesRepository.findByBooksLang(lang, pageable);
}
}