mirror of
https://github.com/samiyev/puaros.git
synced 2025-12-27 23:06:54 +05:00
feat(core): add application layer components
Add application layer: - UseCase base class for use cases - ResponseDto for standardized responses - Mapper for domain-DTO conversion
This commit is contained in:
31
packages/core/src/application/dtos/ResponseDto.ts
Normal file
31
packages/core/src/application/dtos/ResponseDto.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Standard response wrapper for use cases
|
||||
*/
|
||||
export interface IResponseDto<T> {
|
||||
success: boolean;
|
||||
data?: T;
|
||||
error?: string;
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
export class ResponseDto<T> implements IResponseDto<T> {
|
||||
public readonly success: boolean;
|
||||
public readonly data?: T;
|
||||
public readonly error?: string;
|
||||
public readonly timestamp: Date;
|
||||
|
||||
private constructor(success: boolean, data?: T, error?: string) {
|
||||
this.success = success;
|
||||
this.data = data;
|
||||
this.error = error;
|
||||
this.timestamp = new Date();
|
||||
}
|
||||
|
||||
public static ok<T>(data: T): ResponseDto<T> {
|
||||
return new ResponseDto<T>(true, data);
|
||||
}
|
||||
|
||||
public static fail<T>(error: string): ResponseDto<T> {
|
||||
return new ResponseDto<T>(false, undefined, error);
|
||||
}
|
||||
}
|
||||
3
packages/core/src/application/index.ts
Normal file
3
packages/core/src/application/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './use-cases/UseCase';
|
||||
export * from './dtos/ResponseDto';
|
||||
export * from './mappers/Mapper';
|
||||
0
packages/core/src/application/interfaces/.gitkeep
Normal file
0
packages/core/src/application/interfaces/.gitkeep
Normal file
20
packages/core/src/application/mappers/Mapper.ts
Normal file
20
packages/core/src/application/mappers/Mapper.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Generic mapper interface for converting between domain entities and DTOs
|
||||
*/
|
||||
export interface IMapper<TDomain, TDto> {
|
||||
toDto(domain: TDomain): TDto;
|
||||
toDomain(dto: TDto): TDomain;
|
||||
}
|
||||
|
||||
export abstract class Mapper<TDomain, TDto> implements IMapper<TDomain, TDto> {
|
||||
public abstract toDto(domain: TDomain): TDto;
|
||||
public abstract toDomain(dto: TDto): TDomain;
|
||||
|
||||
public toDtoList(domains: TDomain[]): TDto[] {
|
||||
return domains.map((domain) => this.toDto(domain));
|
||||
}
|
||||
|
||||
public toDomainList(dtos: TDto[]): TDomain[] {
|
||||
return dtos.map((dto) => this.toDomain(dto));
|
||||
}
|
||||
}
|
||||
13
packages/core/src/application/use-cases/UseCase.ts
Normal file
13
packages/core/src/application/use-cases/UseCase.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Base interface for all use cases
|
||||
*/
|
||||
export interface IUseCase<TRequest, TResponse> {
|
||||
execute(request: TRequest): Promise<TResponse>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract base class for use cases
|
||||
*/
|
||||
export abstract class UseCase<TRequest, TResponse> implements IUseCase<TRequest, TResponse> {
|
||||
public abstract execute(request: TRequest): Promise<TResponse>;
|
||||
}
|
||||
Reference in New Issue
Block a user