website refactor
This commit is contained in:
34
core/media/application/use-cases/GetUploadedMediaUseCase.ts
Normal file
34
core/media/application/use-cases/GetUploadedMediaUseCase.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { MediaStoragePort } from '../ports/MediaStoragePort';
|
||||
|
||||
export type GetUploadedMediaInput = {
|
||||
storageKey: string;
|
||||
};
|
||||
|
||||
export type GetUploadedMediaResult = {
|
||||
bytes: Buffer;
|
||||
contentType: string;
|
||||
};
|
||||
|
||||
export class GetUploadedMediaUseCase {
|
||||
constructor(private readonly mediaStorage: MediaStoragePort) {}
|
||||
|
||||
async execute(input: GetUploadedMediaInput): Promise<Result<GetUploadedMediaResult | null>> {
|
||||
try {
|
||||
const bytes = await this.mediaStorage.getBytes!(input.storageKey);
|
||||
if (!bytes) {
|
||||
return Result.ok(null);
|
||||
}
|
||||
|
||||
const metadata = await this.mediaStorage.getMetadata!(input.storageKey);
|
||||
const contentType = metadata?.contentType || 'application/octet-stream';
|
||||
|
||||
return Result.ok({
|
||||
bytes: Buffer.from(bytes),
|
||||
contentType,
|
||||
});
|
||||
} catch (error) {
|
||||
return Result.err(error instanceof Error ? error : new Error(String(error)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { MediaReference } from '@core/domain/media/MediaReference';
|
||||
import type { MediaResolverPort } from '@core/ports/media/MediaResolverPort';
|
||||
|
||||
export type ResolveMediaReferenceInput = {
|
||||
reference: MediaReference;
|
||||
};
|
||||
|
||||
export class ResolveMediaReferenceUseCase {
|
||||
constructor(private readonly mediaResolver: MediaResolverPort) {}
|
||||
|
||||
async execute(input: ResolveMediaReferenceInput): Promise<Result<string | null>> {
|
||||
try {
|
||||
const resolvedPath = await this.mediaResolver.resolve(input.reference);
|
||||
return Result.ok(resolvedPath);
|
||||
} catch (error) {
|
||||
return Result.err(error instanceof Error ? error : new Error(String(error)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Application Use Case: GetAllNotificationsUseCase
|
||||
*
|
||||
* Retrieves all notifications for a recipient.
|
||||
*/
|
||||
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { Notification } from '../../domain/entities/Notification';
|
||||
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
|
||||
|
||||
export type GetAllNotificationsInput = {
|
||||
recipientId: string;
|
||||
};
|
||||
|
||||
export interface GetAllNotificationsResult {
|
||||
notifications: Notification[];
|
||||
totalCount: number;
|
||||
}
|
||||
|
||||
export type GetAllNotificationsErrorCode = 'REPOSITORY_ERROR';
|
||||
|
||||
export class GetAllNotificationsUseCase {
|
||||
constructor(
|
||||
private readonly notificationRepository: INotificationRepository,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: GetAllNotificationsInput,
|
||||
): Promise<Result<GetAllNotificationsResult, ApplicationErrorCode<GetAllNotificationsErrorCode, { message: string }>>> {
|
||||
const { recipientId } = input;
|
||||
this.logger.debug(
|
||||
`Attempting to retrieve all notifications for recipient ID: ${recipientId}`,
|
||||
);
|
||||
|
||||
try {
|
||||
const notifications = await this.notificationRepository.findByRecipientId(
|
||||
recipientId,
|
||||
);
|
||||
this.logger.info(
|
||||
`Successfully retrieved ${notifications.length} notifications for recipient ID: ${recipientId}`,
|
||||
);
|
||||
|
||||
return Result.ok<GetAllNotificationsResult, ApplicationErrorCode<GetAllNotificationsErrorCode, { message: string }>>({
|
||||
notifications,
|
||||
totalCount: notifications.length,
|
||||
});
|
||||
} catch (error) {
|
||||
const err = error instanceof Error ? error : new Error(String(error));
|
||||
this.logger.error(
|
||||
`Failed to retrieve notifications for recipient ID: ${recipientId}`,
|
||||
err,
|
||||
);
|
||||
|
||||
return Result.err<GetAllNotificationsResult, ApplicationErrorCode<GetAllNotificationsErrorCode, { message: string }>>({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
20
core/racing/application/use-cases/GetDriverUseCase.ts
Normal file
20
core/racing/application/use-cases/GetDriverUseCase.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { Driver } from '../../domain/entities/Driver';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
|
||||
export type GetDriverInput = {
|
||||
driverId: string;
|
||||
};
|
||||
|
||||
export class GetDriverUseCase {
|
||||
constructor(private readonly driverRepository: IDriverRepository) {}
|
||||
|
||||
async execute(input: GetDriverInput): Promise<Result<Driver | null>> {
|
||||
try {
|
||||
const driver = await this.driverRepository.findById(input.driverId);
|
||||
return Result.ok(driver);
|
||||
} catch (error) {
|
||||
return Result.err(error instanceof Error ? error : new Error(String(error)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user