Added series subscriptions. Added series searching. Fixed database relations. Added logging for library controller.
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { BookEntity } from './entities/book.entity';
|
||||
import { In, InsertResult, Repository } from 'typeorm';
|
||||
import { DeleteResult, In, InsertResult, Repository } from 'typeorm';
|
||||
import { BookOriginEntity } from './entities/book-origin.entity';
|
||||
import { BookStatusEntity } from './entities/book-status.entity';
|
||||
import { UUID } from 'crypto';
|
||||
@ -10,6 +10,8 @@ import { CreateBookOriginDto } from './dto/create-book-origin.dto';
|
||||
import { CreateBookStatusDto } from './dto/create-book-status.dto';
|
||||
import { DeleteBookStatusDto } from './dto/delete-book-status.dto';
|
||||
import { SeriesDto } from 'src/series/dto/series.dto';
|
||||
import { SeriesSubscriptionDto } from 'src/series/dto/series-subscription.dto';
|
||||
import { BookOriginDto } from './dto/book-origin.dto';
|
||||
|
||||
@Injectable()
|
||||
export class BooksService {
|
||||
@ -37,16 +39,18 @@ export class BooksService {
|
||||
return await this.bookOriginRepository.insert(origin);
|
||||
}
|
||||
|
||||
async deleteBookOrigin(origin: CreateBookOriginDto) {
|
||||
async deleteBookOrigin(origin: BookOriginDto[]): Promise<DeleteResult> {
|
||||
return await this.bookOriginRepository.createQueryBuilder()
|
||||
.delete()
|
||||
.where({
|
||||
whereFactory: origin,
|
||||
whereFactory: {
|
||||
bookOriginId: In(origin.map(o => o.bookOriginId)),
|
||||
},
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
async deleteBookStatus(status: DeleteBookStatusDto) {
|
||||
async deleteBookStatus(status: DeleteBookStatusDto): Promise<DeleteResult> {
|
||||
return await this.bookStatusRepository.createQueryBuilder()
|
||||
.delete()
|
||||
.where({
|
||||
@ -55,7 +59,7 @@ export class BooksService {
|
||||
.execute();
|
||||
}
|
||||
|
||||
async findBooksByIds(bookIds: UUID[]) {
|
||||
async findBooksByIds(bookIds: UUID[]): Promise<BookEntity[]> {
|
||||
return await this.bookRepository.find({
|
||||
where: {
|
||||
bookId: In(bookIds)
|
||||
@ -63,7 +67,7 @@ export class BooksService {
|
||||
});
|
||||
}
|
||||
|
||||
async findBooksFromSeries(series: SeriesDto) {
|
||||
async findBooksFromSeries(series: SeriesDto): Promise<BookEntity[]> {
|
||||
return await this.bookRepository.find({
|
||||
where: {
|
||||
providerSeriesId: series.providerSeriesId,
|
||||
@ -72,25 +76,23 @@ export class BooksService {
|
||||
});
|
||||
}
|
||||
|
||||
async findBookStatusesTrackedBy(userId: UUID): Promise<BookStatusEntity[]> {
|
||||
async findActualBookStatusesTrackedBy(userId: UUID, series: SeriesDto): Promise<BookStatusEntity[]> {
|
||||
return await this.bookStatusRepository.createQueryBuilder('s')
|
||||
.select(['s.book_id', 's.user_id'])
|
||||
.where('s.user_id = :id', { id: userId })
|
||||
.innerJoin('s.book', 'b')
|
||||
.where('s.user_id = :id', { id: userId })
|
||||
.andWhere('b.provider = :provider', { provider: series.provider })
|
||||
.andWhere('b.providerSeriesId = :id', { id: series.providerSeriesId })
|
||||
.addSelect(['b.book_title', 'b.book_desc', 'b.book_volume', 'b.provider', 'b.providerSeriesId'])
|
||||
.getMany();
|
||||
}
|
||||
|
||||
async findSeriesTrackedBy(userId: UUID) {
|
||||
return await this.bookStatusRepository.createQueryBuilder('s')
|
||||
.where({
|
||||
whereFactory: {
|
||||
userId: userId
|
||||
}
|
||||
})
|
||||
.innerJoin('s.book', 'b')
|
||||
.addSelect(['b.provider', 'b.providerSeriesId'])
|
||||
.distinctOn(['b.provider', 'b.providerSeriesId'])
|
||||
async findBookStatusesTrackedBy(subscription: SeriesSubscriptionDto): Promise<any> {
|
||||
return await this.bookRepository.createQueryBuilder('b')
|
||||
.where('b.provider = :provider', { provider: subscription.provider })
|
||||
.andWhere(`b.provider_series_id = :id`, { id: subscription.providerSeriesId })
|
||||
.leftJoin('b.statuses', 's')
|
||||
.where(`s.user_id = :id`, { id: subscription.userId })
|
||||
.addSelect(['s.state'])
|
||||
.getMany();
|
||||
}
|
||||
|
||||
@ -111,7 +113,7 @@ export class BooksService {
|
||||
await this.bookStatusRepository.createQueryBuilder()
|
||||
.insert()
|
||||
.values(status)
|
||||
.orUpdate(['state', 'modified_at'], ['user_id', 'book_id'], { skipUpdateIfNoValuesChanged: true })
|
||||
.orUpdate(['state', 'modified_at'], ['user_id', 'book_id'])
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user