147 lines
4.0 KiB
TypeScript
147 lines
4.0 KiB
TypeScript
import { InjectQueue } from '@nestjs/bullmq';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { Queue } from 'bullmq';
|
|
import { PinoLogger } from 'nestjs-pino';
|
|
import { BooksService } from 'src/books/books.service';
|
|
import { BookSearchResultDto } from 'src/providers/dto/book-search-result.dto';
|
|
import { CreateSeriesDto } from 'src/series/dto/create-series.dto';
|
|
import { SeriesSubscriptionDto } from 'src/series/dto/series-subscription.dto';
|
|
import { SeriesDto } from 'src/series/dto/series.dto';
|
|
import { SeriesService } from 'src/series/series.service';
|
|
import { BookOriginType } from 'src/shared/enums/book_origin_type';
|
|
|
|
@Injectable()
|
|
export class LibraryService {
|
|
constructor(
|
|
private readonly books: BooksService,
|
|
private readonly series: SeriesService,
|
|
@InjectQueue('library') private readonly jobs: Queue,
|
|
private readonly logger: PinoLogger,
|
|
) { }
|
|
|
|
|
|
async addSeries(series: CreateSeriesDto) {
|
|
const result = await this.series.addSeries(series);
|
|
|
|
this.logger.debug({
|
|
class: LibraryService.name,
|
|
method: this.addSubscription.name,
|
|
series: series,
|
|
msg: 'Series saved to database.',
|
|
});
|
|
|
|
this.jobs.add('new_series', series);
|
|
return {
|
|
success: true,
|
|
};
|
|
}
|
|
|
|
async addSubscription(series: SeriesSubscriptionDto) {
|
|
return await this.series.addSeriesSubscription({
|
|
userId: series.userId,
|
|
providerSeriesId: series.providerSeriesId,
|
|
provider: series.provider,
|
|
});
|
|
}
|
|
|
|
async addBook(book: BookSearchResultDto) {
|
|
this.logger.debug({
|
|
class: LibraryService.name,
|
|
method: this.addBook.name,
|
|
book: book,
|
|
msg: 'Saving book to database...',
|
|
});
|
|
|
|
const bookData = await this.books.createBook({
|
|
title: book.title,
|
|
desc: book.desc,
|
|
providerSeriesId: book.providerSeriesId,
|
|
providerBookId: book.providerBookId,
|
|
volume: book.volume,
|
|
provider: book.provider,
|
|
publishedAt: book.publishedAt,
|
|
});
|
|
const bookId = bookData.identifiers[0]['bookId'];
|
|
|
|
const tasks = [];
|
|
|
|
if (book.authors && book.authors.length > 0) {
|
|
tasks.push(book.authors.map(author => this.books.addBookOrigin({
|
|
bookId,
|
|
type: BookOriginType.AUTHOR,
|
|
value: author,
|
|
})));
|
|
}
|
|
if (book.categories && book.categories.length > 0) {
|
|
tasks.push(book.categories.map(category => this.books.addBookOrigin({
|
|
bookId,
|
|
type: BookOriginType.CATEGORY,
|
|
value: category
|
|
})));
|
|
}
|
|
if (book.language) {
|
|
tasks.push(this.books.addBookOrigin({
|
|
bookId,
|
|
type: BookOriginType.LANGUAGE,
|
|
value: book.language,
|
|
}));
|
|
}
|
|
if (book.maturityRating) {
|
|
tasks.push(this.books.addBookOrigin({
|
|
bookId,
|
|
type: BookOriginType.MATURITY_RATING,
|
|
value: book.maturityRating,
|
|
}));
|
|
}
|
|
if (book.thumbnail) {
|
|
tasks.push(this.books.addBookOrigin({
|
|
bookId,
|
|
type: BookOriginType.PROVIDER_THUMBNAIL,
|
|
value: book.thumbnail,
|
|
}));
|
|
}
|
|
if (book.url) {
|
|
tasks.push(this.books.addBookOrigin({
|
|
bookId,
|
|
type: BookOriginType.PROVIDER_URL,
|
|
value: book.url,
|
|
}));
|
|
}
|
|
|
|
if ('ISBN_10' in book.industryIdentifiers) {
|
|
tasks.push(this.books.addBookOrigin({
|
|
bookId,
|
|
type: BookOriginType.ISBN_10,
|
|
value: book.industryIdentifiers['ISBN_10'],
|
|
}));
|
|
}
|
|
|
|
if ('ISBN_13' in book.industryIdentifiers) {
|
|
tasks.push(this.books.addBookOrigin({
|
|
bookId,
|
|
type: BookOriginType.ISBN_10,
|
|
value: book.industryIdentifiers['ISBN_13'],
|
|
}));
|
|
}
|
|
|
|
await Promise.all(tasks);
|
|
|
|
this.logger.info({
|
|
class: LibraryService.name,
|
|
method: this.addBook.name,
|
|
book: book,
|
|
msg: 'Book saved to database.',
|
|
});
|
|
|
|
return bookId;
|
|
}
|
|
|
|
async updateSeries(series: CreateSeriesDto) {
|
|
return await this.jobs.add('update_series', series);
|
|
}
|
|
|
|
async getBooksFromSeries(series: SeriesDto) {
|
|
return await this.books.findBooksFromSeries(series);
|
|
}
|
|
}
|