origin/apps/server/src/models/book/book.service.ts

48 lines
1.3 KiB
TypeScript
Raw Normal View History

2025-06-15 22:37:47 +08:00
import { Injectable } from '@nestjs/common';
import { BaseService } from '../base/base.service';
import { db, ObjectType, Prisma } from '@nice/common';
import EventBus, { CrudOperation } from '@server/utils/event-bus';
@Injectable()
export class BookService extends BaseService<Prisma.BookDelegate> {
constructor() {
super(db, ObjectType.LIBRARY, false);
}
async create(args: Prisma.BookCreateArgs) {
const result = await super.create(args);
this.emitDataChanged(CrudOperation.CREATED, result);
return result;
}
async update(args: Prisma.BookUpdateArgs) {
const result = await super.update(args);
this.emitDataChanged(CrudOperation.UPDATED, result);
return result;
}
async findMany(args: Prisma.BookFindManyArgs) {
const result = await super.findMany(args);
return result;
}
async findFirst(args: Prisma.BookFindFirstArgs) {
const result = await super.findFirst(args);
return result;
}
async softDeleteByIds(ids: string[]) {
const result = await super.softDeleteByIds(ids);
this.emitDataChanged(CrudOperation.DELETED, result);
return result;
}
private emitDataChanged(operation: CrudOperation, data: any) {
EventBus.emit('dataChanged', {
type: ObjectType.BOOK,
operation,
data,
});
}
}