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.84 kB
text/typescript
/**
* Services and business logic for the User domain.
* @packageDocumentation
*/
import type {
UserEntity,
UserCreationData,
UserUpdateData
} from '../entities/User';
import type {
UserPort
} from '../ports/UserPort';
export class UserError extends Error {
constructor(
public code: string,
message: string,
) {
super(message);
this.name = 'UserError';
}
}
/**
* Service implementation for User domain
*/
export class UserService {
constructor(private readonly port: UserPort) {}
/**
* Get all user entities
*/
async getAll(): Promise<UserEntity[]> {
try {
return await this.port.getAll();
} catch (error) {
throw new UserError('GET_ALL_ERROR', 'Failed to get all user entities');
}
}
/**
* Get a user entity by ID
*/
async getById(id: string): Promise<UserEntity> {
try {
return await this.port.getById(id);
} catch (error) {
throw new UserError('GET_BY_ID_ERROR', `Failed to get user entity with id ${id}`);
}
}
/**
* Create a new user entity
*/
async create(data: UserCreationData): Promise<UserEntity> {
try {
return await this.port.create(data);
} catch (error) {
throw new UserError('CREATE_ERROR', 'Failed to create user entity');
}
}
/**
* Update an existing user entity
*/
async update(id: string, data: UserUpdateData): Promise<UserEntity> {
try {
return await this.port.update(id, data);
} catch (error) {
throw new UserError('UPDATE_ERROR', `Failed to update user entity with id ${id}`);
}
}
/**
* Delete a user entity
*/
async delete(id: string): Promise<void> {
try {
await this.port.delete(id);
} catch (error) {
throw new UserError('DELETE_ERROR', `Failed to delete user entity with id ${id}`);
}
}
}