refactor
This commit is contained in:
@@ -1,21 +1,50 @@
|
||||
import type { IDeleteMediaPresenter, DeleteMediaResult } from '@core/media/application/presenters/IDeleteMediaPresenter';
|
||||
import type { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type {
|
||||
DeleteMediaResult,
|
||||
DeleteMediaErrorCode,
|
||||
} from '@core/media/application/use-cases/DeleteMediaUseCase';
|
||||
import type { DeleteMediaOutputDTO } from '../dtos/DeleteMediaOutputDTO';
|
||||
|
||||
type DeleteMediaOutput = DeleteMediaOutputDTO;
|
||||
type DeleteMediaResponseModel = DeleteMediaOutputDTO;
|
||||
|
||||
export class DeleteMediaPresenter implements IDeleteMediaPresenter {
|
||||
private result: DeleteMediaResult | null = null;
|
||||
export type DeleteMediaApplicationError = ApplicationErrorCode<
|
||||
DeleteMediaErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
present(result: DeleteMediaResult) {
|
||||
this.result = result;
|
||||
export class DeleteMediaPresenter {
|
||||
private model: DeleteMediaResponseModel | null = null;
|
||||
|
||||
reset(): void {
|
||||
this.model = null;
|
||||
}
|
||||
|
||||
get viewModel(): DeleteMediaOutput {
|
||||
if (!this.result) throw new Error('Presenter not presented');
|
||||
present(result: Result<DeleteMediaResult, DeleteMediaApplicationError>): void {
|
||||
if (result.isErr()) {
|
||||
const error = result.unwrapErr();
|
||||
|
||||
return {
|
||||
success: this.result.success,
|
||||
error: this.result.errorMessage,
|
||||
this.model = {
|
||||
success: false,
|
||||
error: error.details?.message ?? 'Failed to delete media',
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
const output = result.unwrap();
|
||||
|
||||
this.model = {
|
||||
success: output.deleted,
|
||||
error: undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
getResponseModel(): DeleteMediaResponseModel | null {
|
||||
return this.model;
|
||||
}
|
||||
|
||||
get responseModel(): DeleteMediaResponseModel {
|
||||
if (!this.model) throw new Error('Presenter not presented');
|
||||
return this.model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,49 @@
|
||||
import type { IGetAvatarPresenter, GetAvatarResult } from '@core/media/application/presenters/IGetAvatarPresenter';
|
||||
import type { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type {
|
||||
GetAvatarResult,
|
||||
GetAvatarErrorCode,
|
||||
} from '@core/media/application/use-cases/GetAvatarUseCase';
|
||||
import type { GetAvatarOutputDTO } from '../dtos/GetAvatarOutputDTO';
|
||||
|
||||
export type GetAvatarViewModel = GetAvatarOutputDTO | null;
|
||||
export type GetAvatarResponseModel = GetAvatarOutputDTO | null;
|
||||
|
||||
export class GetAvatarPresenter implements IGetAvatarPresenter {
|
||||
private result: GetAvatarResult | null = null;
|
||||
export type GetAvatarApplicationError = ApplicationErrorCode<
|
||||
GetAvatarErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
present(result: GetAvatarResult) {
|
||||
this.result = result;
|
||||
export class GetAvatarPresenter {
|
||||
private model: GetAvatarResponseModel | null = null;
|
||||
|
||||
reset(): void {
|
||||
this.model = null;
|
||||
}
|
||||
|
||||
get viewModel(): GetAvatarViewModel {
|
||||
if (!this.result || !this.result.success || !this.result.avatar) {
|
||||
return null;
|
||||
present(result: Result<GetAvatarResult, GetAvatarApplicationError>): void {
|
||||
if (result.isErr()) {
|
||||
const error = result.unwrapErr();
|
||||
|
||||
if (error.code === 'AVATAR_NOT_FOUND') {
|
||||
this.model = null;
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error(error.details?.message ?? 'Failed to get avatar');
|
||||
}
|
||||
|
||||
return {
|
||||
avatarUrl: this.result.avatar.mediaUrl,
|
||||
const output = result.unwrap();
|
||||
|
||||
this.model = {
|
||||
avatarUrl: output.avatar.mediaUrl,
|
||||
};
|
||||
}
|
||||
|
||||
getResponseModel(): GetAvatarResponseModel | null {
|
||||
return this.model;
|
||||
}
|
||||
|
||||
get responseModel(): GetAvatarResponseModel {
|
||||
return this.model ?? null;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,39 @@
|
||||
import type { IGetMediaPresenter, GetMediaResult } from '@core/media/application/presenters/IGetMediaPresenter';
|
||||
import type { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { GetMediaResult, GetMediaErrorCode } from '@core/media/application/use-cases/GetMediaUseCase';
|
||||
import type { GetMediaOutputDTO } from '../dtos/GetMediaOutputDTO';
|
||||
|
||||
// The HTTP-facing DTO (or null when not found)
|
||||
export type GetMediaViewModel = GetMediaOutputDTO | null;
|
||||
export type GetMediaResponseModel = GetMediaOutputDTO | null;
|
||||
|
||||
export class GetMediaPresenter implements IGetMediaPresenter {
|
||||
private result: GetMediaResult | null = null;
|
||||
export type GetMediaApplicationError = ApplicationErrorCode<
|
||||
GetMediaErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
present(result: GetMediaResult) {
|
||||
this.result = result;
|
||||
export class GetMediaPresenter {
|
||||
private model: GetMediaResponseModel | null = null;
|
||||
|
||||
reset(): void {
|
||||
this.model = null;
|
||||
}
|
||||
|
||||
get viewModel(): GetMediaViewModel {
|
||||
if (!this.result || !this.result.success || !this.result.media) {
|
||||
return null;
|
||||
present(result: Result<GetMediaResult, GetMediaApplicationError>): void {
|
||||
if (result.isErr()) {
|
||||
const error = result.unwrapErr();
|
||||
|
||||
if (error.code === 'MEDIA_NOT_FOUND') {
|
||||
this.model = null;
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error(error.details?.message ?? 'Failed to get media');
|
||||
}
|
||||
|
||||
const media = this.result.media;
|
||||
const output = result.unwrap();
|
||||
|
||||
return {
|
||||
const media = output.media;
|
||||
|
||||
this.model = {
|
||||
id: media.id,
|
||||
url: media.url,
|
||||
type: media.type,
|
||||
@@ -28,4 +43,12 @@ export class GetMediaPresenter implements IGetMediaPresenter {
|
||||
size: media.size,
|
||||
};
|
||||
}
|
||||
|
||||
getResponseModel(): GetMediaResponseModel | null {
|
||||
return this.model;
|
||||
}
|
||||
|
||||
get responseModel(): GetMediaResponseModel {
|
||||
return this.model ?? null;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +1,59 @@
|
||||
import type { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type {
|
||||
RequestAvatarGenerationResult,
|
||||
RequestAvatarGenerationErrorCode,
|
||||
} from '@core/media/application/use-cases/RequestAvatarGenerationUseCase';
|
||||
import type { RequestAvatarGenerationOutputDTO } from '../dtos/RequestAvatarGenerationOutputDTO';
|
||||
import type { IRequestAvatarGenerationPresenter, RequestAvatarGenerationResultDTO } from '@core/media/application/presenters/IRequestAvatarGenerationPresenter';
|
||||
|
||||
type RequestAvatarGenerationOutput = RequestAvatarGenerationOutputDTO;
|
||||
type RequestAvatarGenerationResponseModel = RequestAvatarGenerationOutputDTO;
|
||||
|
||||
export class RequestAvatarGenerationPresenter implements IRequestAvatarGenerationPresenter {
|
||||
private result: RequestAvatarGenerationOutput | null = null;
|
||||
export type RequestAvatarGenerationApplicationError = ApplicationErrorCode<
|
||||
RequestAvatarGenerationErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
export class RequestAvatarGenerationPresenter {
|
||||
private model: RequestAvatarGenerationResponseModel | null = null;
|
||||
|
||||
reset() {
|
||||
this.result = null;
|
||||
this.model = null;
|
||||
}
|
||||
|
||||
present(dto: RequestAvatarGenerationResultDTO) {
|
||||
this.result = {
|
||||
success: dto.status === 'completed',
|
||||
requestId: dto.requestId,
|
||||
avatarUrls: dto.avatarUrls,
|
||||
errorMessage: dto.errorMessage,
|
||||
present(
|
||||
result: Result<
|
||||
RequestAvatarGenerationResult,
|
||||
RequestAvatarGenerationApplicationError
|
||||
>,
|
||||
): void {
|
||||
if (result.isErr()) {
|
||||
const error = result.unwrapErr();
|
||||
|
||||
this.model = {
|
||||
success: false,
|
||||
requestId: '',
|
||||
avatarUrls: [],
|
||||
errorMessage: error.details?.message ?? 'Failed to request avatar generation',
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
const output = result.unwrap();
|
||||
|
||||
this.model = {
|
||||
success: output.status === 'completed',
|
||||
requestId: output.requestId,
|
||||
avatarUrls: output.avatarUrls,
|
||||
errorMessage: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
get viewModel(): RequestAvatarGenerationOutput {
|
||||
if (!this.result) throw new Error('Presenter not presented');
|
||||
return this.result;
|
||||
getResponseModel(): RequestAvatarGenerationResponseModel | null {
|
||||
return this.model;
|
||||
}
|
||||
|
||||
getViewModel(): RequestAvatarGenerationOutput {
|
||||
return this.viewModel;
|
||||
get responseModel(): RequestAvatarGenerationResponseModel {
|
||||
if (!this.model) throw new Error('Presenter not presented');
|
||||
return this.model;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,45 @@
|
||||
import type { IUpdateAvatarPresenter, UpdateAvatarResult } from '@core/media/application/presenters/IUpdateAvatarPresenter';
|
||||
import type { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type {
|
||||
UpdateAvatarResult,
|
||||
UpdateAvatarErrorCode,
|
||||
} from '@core/media/application/use-cases/UpdateAvatarUseCase';
|
||||
import type { UpdateAvatarOutputDTO } from '../dtos/UpdateAvatarOutputDTO';
|
||||
|
||||
type UpdateAvatarOutput = UpdateAvatarOutputDTO;
|
||||
|
||||
export class UpdateAvatarPresenter implements IUpdateAvatarPresenter {
|
||||
private result: UpdateAvatarResult | null = null;
|
||||
|
||||
present(result: UpdateAvatarResult) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
get viewModel(): UpdateAvatarOutput {
|
||||
if (!this.result) throw new Error('Presenter not presented');
|
||||
type UpdateAvatarResponseModel = UpdateAvatarOutputDTO;
|
||||
|
||||
return {
|
||||
success: this.result.success,
|
||||
error: this.result.errorMessage,
|
||||
export type UpdateAvatarApplicationError = ApplicationErrorCode<
|
||||
UpdateAvatarErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
export class UpdateAvatarPresenter {
|
||||
private model: UpdateAvatarResponseModel | null = null;
|
||||
|
||||
reset(): void {
|
||||
this.model = null;
|
||||
}
|
||||
|
||||
present(result: Result<UpdateAvatarResult, UpdateAvatarApplicationError>): void {
|
||||
if (result.isErr()) {
|
||||
const error = result.unwrapErr();
|
||||
throw new Error(error.details?.message ?? 'Failed to update avatar');
|
||||
}
|
||||
|
||||
const output = result.unwrap();
|
||||
|
||||
this.model = {
|
||||
success: true,
|
||||
error: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
getResponseModel(): UpdateAvatarResponseModel | null {
|
||||
return this.model;
|
||||
}
|
||||
|
||||
get responseModel(): UpdateAvatarResponseModel {
|
||||
if (!this.model) throw new Error('Presenter not presented');
|
||||
return this.model;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,52 @@
|
||||
import type { IUploadMediaPresenter, UploadMediaResult } from '@core/media/application/presenters/IUploadMediaPresenter';
|
||||
import type { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type {
|
||||
UploadMediaResult,
|
||||
UploadMediaErrorCode,
|
||||
} from '@core/media/application/use-cases/UploadMediaUseCase';
|
||||
import type { UploadMediaOutputDTO } from '../dtos/UploadMediaOutputDTO';
|
||||
|
||||
type UploadMediaOutput = UploadMediaOutputDTO;
|
||||
type UploadMediaResponseModel = UploadMediaOutputDTO;
|
||||
|
||||
export class UploadMediaPresenter implements IUploadMediaPresenter {
|
||||
private result: UploadMediaResult | null = null;
|
||||
export type UploadMediaApplicationError = ApplicationErrorCode<
|
||||
UploadMediaErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
present(result: UploadMediaResult) {
|
||||
this.result = result;
|
||||
export class UploadMediaPresenter {
|
||||
private model: UploadMediaResponseModel | null = null;
|
||||
|
||||
reset(): void {
|
||||
this.model = null;
|
||||
}
|
||||
|
||||
get viewModel(): UploadMediaOutput {
|
||||
if (!this.result) throw new Error('Presenter not presented');
|
||||
present(result: Result<UploadMediaResult, UploadMediaApplicationError>): void {
|
||||
if (result.isErr()) {
|
||||
const error = result.unwrapErr();
|
||||
|
||||
if (this.result.success) {
|
||||
return {
|
||||
success: true,
|
||||
mediaId: this.result.mediaId,
|
||||
url: this.result.url,
|
||||
this.model = {
|
||||
success: false,
|
||||
error: error.details?.message ?? 'Upload failed',
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
error: this.result.errorMessage || 'Upload failed',
|
||||
const output = result.unwrap();
|
||||
|
||||
this.model = {
|
||||
success: true,
|
||||
mediaId: output.mediaId,
|
||||
url: output.url,
|
||||
error: undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
getResponseModel(): UploadMediaResponseModel | null {
|
||||
return this.model;
|
||||
}
|
||||
|
||||
get responseModel(): UploadMediaResponseModel {
|
||||
if (!this.model) throw new Error('Presenter not presented');
|
||||
return this.model;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user