ruch
Version:
Revolutionary React TypeScript CLI with hexagonal architecture & AI-powered development assistance. Create maintainable, scalable applications with domain-driven design and integrated AI tooling.
87 lines (77 loc) • 1.94 kB
text/typescript
/**
* Services and business logic for the Product domain.
* @packageDocumentation
*/
import type {
ProductEntity,
ProductCreationData,
ProductUpdateData
} from '../entities/Product';
import type {
ProductPort
} from '../ports/ProductPort';
export class ProductError extends Error {
constructor(
public code: string,
message: string,
) {
super(message);
this.name = 'ProductError';
}
}
/**
* Service implementation for Product domain
*/
export class ProductService {
constructor(private readonly port: ProductPort) {}
/**
* Get all product entities
*/
async getAll(): Promise<ProductEntity[]> {
try {
return await this.port.getAll();
} catch (error) {
throw new ProductError('GET_ALL_ERROR', 'Failed to get all product entities');
}
}
/**
* Get a product entity by ID
*/
async getById(id: string): Promise<ProductEntity> {
try {
return await this.port.getById(id);
} catch (error) {
throw new ProductError('GET_BY_ID_ERROR', `Failed to get product entity with id ${id}`);
}
}
/**
* Create a new product entity
*/
async create(data: ProductCreationData): Promise<ProductEntity> {
try {
return await this.port.create(data);
} catch (error) {
throw new ProductError('CREATE_ERROR', 'Failed to create product entity');
}
}
/**
* Update an existing product entity
*/
async update(id: string, data: ProductUpdateData): Promise<ProductEntity> {
try {
return await this.port.update(id, data);
} catch (error) {
throw new ProductError('UPDATE_ERROR', `Failed to update product entity with id ${id}`);
}
}
/**
* Delete a product entity
*/
async delete(id: string): Promise<void> {
try {
await this.port.delete(id);
} catch (error) {
throw new ProductError('DELETE_ERROR', `Failed to delete product entity with id ${id}`);
}
}
}