presenter refactoring
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import type { IDeleteMediaPresenter, DeleteMediaResult } from '@core/media/application/presenters/IDeleteMediaPresenter';
|
||||
import type { DeleteMediaOutputDTO } from '../dtos/DeleteMediaOutputDTO';
|
||||
|
||||
type DeleteMediaOutput = DeleteMediaOutputDTO;
|
||||
|
||||
export class DeleteMediaPresenter implements IDeleteMediaPresenter {
|
||||
private result: DeleteMediaResult | null = null;
|
||||
@@ -7,8 +10,12 @@ export class DeleteMediaPresenter implements IDeleteMediaPresenter {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
get viewModel(): DeleteMediaResult {
|
||||
get viewModel(): DeleteMediaOutput {
|
||||
if (!this.result) throw new Error('Presenter not presented');
|
||||
return this.result;
|
||||
|
||||
return {
|
||||
success: this.result.success,
|
||||
error: this.result.errorMessage,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
import type { IGetAvatarPresenter, GetAvatarResult } from '@core/media/application/presenters/IGetAvatarPresenter';
|
||||
import type { GetAvatarOutputDTO } from '../dtos/GetAvatarOutputDTO';
|
||||
|
||||
export type GetAvatarViewModel = GetAvatarOutputDTO | null;
|
||||
|
||||
export class GetAvatarPresenter implements IGetAvatarPresenter {
|
||||
private result: GetAvatarResult | null = null;
|
||||
@@ -7,8 +10,13 @@ export class GetAvatarPresenter implements IGetAvatarPresenter {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
get viewModel(): GetAvatarResult {
|
||||
if (!this.result) throw new Error('Presenter not presented');
|
||||
return this.result;
|
||||
get viewModel(): GetAvatarViewModel {
|
||||
if (!this.result || !this.result.success || !this.result.avatar) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
avatarUrl: this.result.avatar.mediaUrl,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
import type { IGetMediaPresenter, GetMediaResult } from '@core/media/application/presenters/IGetMediaPresenter';
|
||||
import type { GetMediaOutputDTO } from '../dtos/GetMediaOutputDTO';
|
||||
|
||||
// The HTTP-facing DTO (or null when not found)
|
||||
export type GetMediaViewModel = GetMediaOutputDTO | null;
|
||||
|
||||
export class GetMediaPresenter implements IGetMediaPresenter {
|
||||
private result: GetMediaResult | null = null;
|
||||
@@ -7,8 +11,21 @@ export class GetMediaPresenter implements IGetMediaPresenter {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
get viewModel(): GetMediaResult {
|
||||
if (!this.result) throw new Error('Presenter not presented');
|
||||
return this.result;
|
||||
get viewModel(): GetMediaViewModel {
|
||||
if (!this.result || !this.result.success || !this.result.media) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const media = this.result.media;
|
||||
|
||||
return {
|
||||
id: media.id,
|
||||
url: media.url,
|
||||
type: media.type,
|
||||
// Best-effort mapping from arbitrary metadata
|
||||
category: (media.metadata as { category?: string } | undefined)?.category,
|
||||
uploadedAt: media.uploadedAt,
|
||||
size: media.size,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,21 @@
|
||||
import type { IUpdateAvatarPresenter, UpdateAvatarResult } from '@core/media/application/presenters/IUpdateAvatarPresenter';
|
||||
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(): UpdateAvatarResult {
|
||||
|
||||
get viewModel(): UpdateAvatarOutput {
|
||||
if (!this.result) throw new Error('Presenter not presented');
|
||||
return this.result;
|
||||
|
||||
return {
|
||||
success: this.result.success,
|
||||
error: this.result.errorMessage,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
import type { IUploadMediaPresenter, UploadMediaResult } from '@core/media/application/presenters/IUploadMediaPresenter';
|
||||
import type { UploadMediaOutputDTO } from '../dtos/UploadMediaOutputDTO';
|
||||
|
||||
type UploadMediaOutput = UploadMediaOutputDTO;
|
||||
|
||||
export class UploadMediaPresenter implements IUploadMediaPresenter {
|
||||
private result: UploadMediaResult | null = null;
|
||||
@@ -7,8 +10,20 @@ export class UploadMediaPresenter implements IUploadMediaPresenter {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
get viewModel(): UploadMediaResult {
|
||||
get viewModel(): UploadMediaOutput {
|
||||
if (!this.result) throw new Error('Presenter not presented');
|
||||
return this.result;
|
||||
|
||||
if (this.result.success) {
|
||||
return {
|
||||
success: true,
|
||||
mediaId: this.result.mediaId,
|
||||
url: this.result.url,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
error: this.result.errorMessage || 'Upload failed',
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user