124 lines
4.1 KiB
TypeScript
124 lines
4.1 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { BookEntity } from './entities/book.entity';
|
|
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';
|
|
import { CreateBookDto } from './dto/create-book.dto';
|
|
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 {
|
|
constructor(
|
|
@InjectRepository(BookEntity)
|
|
private bookRepository: Repository<BookEntity>,
|
|
@InjectRepository(BookOriginEntity)
|
|
private bookOriginRepository: Repository<BookOriginEntity>,
|
|
@InjectRepository(BookStatusEntity)
|
|
private bookStatusRepository: Repository<BookStatusEntity>,
|
|
) { }
|
|
|
|
|
|
async createBook(book: CreateBookDto): Promise<InsertResult> {
|
|
const entity = this.bookRepository.create(book);
|
|
return await this.bookRepository.createQueryBuilder()
|
|
.insert()
|
|
.into(BookEntity)
|
|
.values(entity)
|
|
.returning('book_id')
|
|
.execute();
|
|
}
|
|
|
|
async addBookOrigin(origin: CreateBookOriginDto): Promise<InsertResult> {
|
|
return await this.bookOriginRepository.insert(origin);
|
|
}
|
|
|
|
async deleteBookOrigin(origin: BookOriginDto[]): Promise<DeleteResult> {
|
|
return await this.bookOriginRepository.createQueryBuilder()
|
|
.delete()
|
|
.where({
|
|
whereFactory: {
|
|
bookOriginId: In(origin.map(o => o.bookOriginId)),
|
|
},
|
|
})
|
|
.execute();
|
|
}
|
|
|
|
async deleteBookStatus(status: DeleteBookStatusDto): Promise<DeleteResult> {
|
|
return await this.bookStatusRepository.createQueryBuilder()
|
|
.delete()
|
|
.where({
|
|
whereFactory: status,
|
|
})
|
|
.execute();
|
|
}
|
|
|
|
async findBooksByIds(bookIds: UUID[]): Promise<BookEntity[]> {
|
|
return await this.bookRepository.find({
|
|
where: {
|
|
bookId: In(bookIds)
|
|
}
|
|
});
|
|
}
|
|
|
|
async findBooksFromSeries(series: SeriesDto): Promise<BookEntity[]> {
|
|
return await this.bookRepository.find({
|
|
where: {
|
|
providerSeriesId: series.providerSeriesId,
|
|
provider: series.provider,
|
|
}
|
|
});
|
|
}
|
|
|
|
async findBooks(): Promise<BookEntity[]> {
|
|
return await this.bookRepository.find();
|
|
}
|
|
|
|
async findActualBookStatusesTrackedBy(userId: UUID, series: SeriesDto): Promise<BookStatusEntity[]> {
|
|
return await this.bookStatusRepository.createQueryBuilder('s')
|
|
.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 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();
|
|
}
|
|
|
|
async updateBook(bookId: UUID, update: CreateBookDto) {
|
|
return await this.bookRepository.update({
|
|
bookId,
|
|
}, update);
|
|
}
|
|
|
|
async updateBookOrigin(bookOriginId: UUID, update: CreateBookOriginDto) {
|
|
return await this.bookOriginRepository.update({
|
|
bookOriginId
|
|
}, update);
|
|
}
|
|
|
|
async updateBookStatus(status: CreateBookStatusDto) {
|
|
status.modifiedAt = new Date();
|
|
await this.bookStatusRepository.createQueryBuilder()
|
|
.insert()
|
|
.values(status)
|
|
.orUpdate(['state', 'modified_at'], ['user_id', 'book_id'])
|
|
.execute();
|
|
}
|
|
}
|