all
This commit is contained in:
1 parent
90125aede9
commit
2d6c29a1f3
404 files changed
+126983
-85
No files matched your search
@@ -0,0 +1,54 @@
|
||||
import { NotFoundException } from "@nestjs/common";
|
||||
import type { IRepository } from "./repository.interface.js";
|
||||
import type { IService } from "./service.interface.js";
|
||||
|
||||
/**
|
||||
* Generic business layer: delegates to an `IRepository`, translating a
|
||||
* missing row into a `NotFoundException`. Subclasses may override
|
||||
* `notFoundMessage` for a more specific error message.
|
||||
*/
|
||||
export abstract class BaseService<
|
||||
T,
|
||||
CreateInput,
|
||||
UpdateInput,
|
||||
WhereKey,
|
||||
> implements IService<T, CreateInput, UpdateInput, WhereKey> {
|
||||
constructor(
|
||||
protected readonly repository: IRepository<
|
||||
T,
|
||||
CreateInput,
|
||||
UpdateInput,
|
||||
WhereKey
|
||||
>,
|
||||
) {}
|
||||
|
||||
findAll(): Promise<T[]> {
|
||||
return this.repository.findAll();
|
||||
}
|
||||
|
||||
async findById(where: WhereKey): Promise<T> {
|
||||
const entity = await this.repository.findById(where);
|
||||
if (!entity) {
|
||||
throw new NotFoundException(this.notFoundMessage(where));
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
create(data: CreateInput): Promise<T> {
|
||||
return this.repository.create(data);
|
||||
}
|
||||
|
||||
async update(where: WhereKey, data: UpdateInput): Promise<T> {
|
||||
await this.findById(where);
|
||||
return this.repository.update(where, data);
|
||||
}
|
||||
|
||||
async delete(where: WhereKey): Promise<void> {
|
||||
await this.findById(where);
|
||||
await this.repository.delete(where);
|
||||
}
|
||||
|
||||
protected notFoundMessage(where: WhereKey): string {
|
||||
return `Resource not found: ${JSON.stringify(where)}`;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user