|
@@ -1,26 +1,106 @@
|
|
|
-import { Injectable } from '@nestjs/common';
|
|
|
+import { Injectable, NotFoundException } from '@nestjs/common';
|
|
|
+import { InjectRepository } from '@nestjs/typeorm';
|
|
|
+import { Repository } from 'typeorm';
|
|
|
import { CreateBookDto } from './dto/create-book.dto';
|
|
|
import { UpdateBookDto } from './dto/update-book.dto';
|
|
|
+import { Book, BookLanguage } from './entities/book.entity';
|
|
|
+import { Tag } from '../tag/entities/tag.entity';
|
|
|
|
|
|
@Injectable()
|
|
|
export class BookService {
|
|
|
- create(createBookDto: CreateBookDto) {
|
|
|
- return 'This action adds a new book';
|
|
|
+ constructor(
|
|
|
+ @InjectRepository(Book)
|
|
|
+ private bookRepository: Repository<Book>,
|
|
|
+ @InjectRepository(Tag)
|
|
|
+ private tagRepository: Repository<Tag>,
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ async create(createBookDto: CreateBookDto) {
|
|
|
+ const book = this.bookRepository.create(createBookDto);
|
|
|
+
|
|
|
+ // 转换出版日期字符串为 Date 对象
|
|
|
+ if (createBookDto.publishDate) {
|
|
|
+ book.publishDate = new Date(createBookDto.publishDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果提供了标签ID,则关联标签
|
|
|
+ if (createBookDto.tagIds && createBookDto.tagIds.length > 0) {
|
|
|
+ const tags = await this.tagRepository.findByIds(createBookDto.tagIds);
|
|
|
+ if (tags.length !== createBookDto.tagIds.length) {
|
|
|
+ throw new NotFoundException('部分标签不存在');
|
|
|
+ }
|
|
|
+ book.tags = tags;
|
|
|
+ }
|
|
|
+
|
|
|
+ return this.bookRepository.save(book);
|
|
|
+ }
|
|
|
+
|
|
|
+ async findAll() {
|
|
|
+ return this.bookRepository.find({
|
|
|
+ relations: ['tags'],
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ async findOne(id: number) {
|
|
|
+ const book = await this.bookRepository.findOne({
|
|
|
+ where: { id },
|
|
|
+ relations: ['tags'],
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!book) {
|
|
|
+ throw new NotFoundException(`ID为${id}的书籍不存在`);
|
|
|
+ }
|
|
|
+
|
|
|
+ return book;
|
|
|
+ }
|
|
|
+
|
|
|
+ async update(id: number, updateBookDto: UpdateBookDto) {
|
|
|
+ const book = await this.findOne(id);
|
|
|
+
|
|
|
+ // 更新基本字段
|
|
|
+ Object.assign(book, updateBookDto);
|
|
|
+
|
|
|
+ // 转换出版日期字符串为 Date 对象
|
|
|
+ if (updateBookDto.publishDate) {
|
|
|
+ book.publishDate = new Date(updateBookDto.publishDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果提供了新的标签ID,则更新标签关联
|
|
|
+ if (updateBookDto.tagIds) {
|
|
|
+ const tags = await this.tagRepository.findByIds(updateBookDto.tagIds);
|
|
|
+ if (tags.length !== updateBookDto.tagIds.length) {
|
|
|
+ throw new NotFoundException('部分标签不存在');
|
|
|
+ }
|
|
|
+ book.tags = tags;
|
|
|
+ }
|
|
|
+
|
|
|
+ return this.bookRepository.save(book);
|
|
|
}
|
|
|
|
|
|
- findAll() {
|
|
|
- return `This action returns all book`;
|
|
|
+ async remove(id: number) {
|
|
|
+ const book = await this.findOne(id);
|
|
|
+ return this.bookRepository.remove(book);
|
|
|
}
|
|
|
|
|
|
- findOne(id: number) {
|
|
|
- return `This action returns a #${id} book`;
|
|
|
+ async findByTag(tagId: number) {
|
|
|
+ return this.bookRepository
|
|
|
+ .createQueryBuilder('book')
|
|
|
+ .leftJoinAndSelect('book.tags', 'tag')
|
|
|
+ .where('tag.id = :tagId', { tagId })
|
|
|
+ .getMany();
|
|
|
}
|
|
|
|
|
|
- update(id: number, updateBookDto: UpdateBookDto) {
|
|
|
- return `This action updates a #${id} book`;
|
|
|
+ async findByPublisher(publisher: string) {
|
|
|
+ return this.bookRepository.find({
|
|
|
+ where: { publisher },
|
|
|
+ relations: ['tags'],
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- remove(id: number) {
|
|
|
- return `This action removes a #${id} book`;
|
|
|
+ async findByLanguage(language: BookLanguage) {
|
|
|
+ return this.bookRepository.find({
|
|
|
+ where: { language },
|
|
|
+ relations: ['tags'],
|
|
|
+ });
|
|
|
}
|
|
|
}
|