project refactoring en clean archi

This commit is contained in:
styve Lioumba
2025-10-23 14:10:53 +02:00
parent ef02c6a537
commit 02637235e3
52 changed files with 3873 additions and 875 deletions

View File

@@ -5,30 +5,45 @@ import { Profile } from '@app/domain/profiles/profile.model';
import { Injectable } from '@angular/core';
import { ProfilePresenter } from '@app/ui/profiles/profile.presenter';
import { ProfileViewModel } from '@app/ui/profiles/profile.presenter.model';
import { LoaderAction } from '@app/domain/loader-action.util';
import { ActionType } from '@app/domain/action-type.util';
import { ErrorResponse } from '@app/domain/error-response.util';
@Injectable({
providedIn: 'root',
})
export class ProfileFacade {
private profileRepository = inject(PROFILE_REPOSITORY_TOKEN);
private useCase = new ListProfilesUseCase(this.profileRepository);
readonly loading = signal(false);
private listUseCase = new ListProfilesUseCase(this.profileRepository);
readonly profiles = signal<ProfileViewModel[]>([]);
readonly error = signal<string | null>(null);
readonly loading = signal<LoaderAction>({ isLoading: false, action: ActionType.NONE });
readonly error = signal<ErrorResponse>({
action: ActionType.NONE,
hasError: false,
message: null,
});
load(search?: string) {
this.loading.set(true);
this.error.set(null);
this.handleError(ActionType.READ, false, null, true);
this.useCase.execute({ search }).subscribe({
this.listUseCase.execute({ search }).subscribe({
next: (profiles) => {
this.profiles.set(ProfilePresenter.toViewModels(profiles));
this.loading.set(false);
this.handleError(ActionType.READ, false, null, false);
},
error: (err) => {
this.error.set('Failed to load profiles');
this.loading.set(false);
this.handleError(ActionType.READ, false, err, false);
},
});
}
private handleError(
action: ActionType = ActionType.NONE,
hasError: boolean,
message: string | null = null,
isLoading = false
) {
this.error.set({ action, hasError, message });
this.loading.set({ action, isLoading });
}
}

View File

@@ -0,0 +1,116 @@
import { inject, Injectable, signal } from '@angular/core';
import { PROJECT_REPOSITORY_TOKEN } from '@app/infrastructure/projects/project-repository.token';
import { CreateProjectUseCase } from '@app/usecase/projects/create-project.usecase';
import { ListProjectUseCase } from '@app/usecase/projects/list-project.usecase';
import { GetProjectUseCase } from '@app/usecase/projects/get-project.usecase';
import { UpdateProjectUseCase } from '@app/usecase/projects/update-project.usecase';
import { Project } from '@app/domain/projects/project.model';
import { ProjectViewModel } from '@app/ui/projects/project.presenter.model';
import { ProjectPresenter } from '@app/ui/projects/project.presenter';
import { CreateProjectDto } from '@app/domain/projects/dto/create-project.dto';
import { ErrorResponse } from '@app/domain/error-response.util';
import { ActionType } from '@app/domain/action-type.util';
import { LoaderAction } from '@app/domain/loader-action.util';
@Injectable({
providedIn: 'root',
})
export class ProjectFacade {
private readonly projectRepo = inject(PROJECT_REPOSITORY_TOKEN);
private readonly createUseCase = new CreateProjectUseCase(this.projectRepo);
private readonly listUseCase = new ListProjectUseCase(this.projectRepo);
private readonly getUseCase = new GetProjectUseCase(this.projectRepo);
private readonly UpdateUseCase = new UpdateProjectUseCase(this.projectRepo);
readonly projects = signal<ProjectViewModel[]>([]);
readonly project = signal<ProjectViewModel>({} as ProjectViewModel);
readonly loading = signal<LoaderAction>({ isLoading: false, action: ActionType.NONE });
readonly error = signal<ErrorResponse>({
action: ActionType.NONE,
hasError: false,
message: null,
});
private readonly projectPresenter = new ProjectPresenter();
load(userId: string) {
this.loading.set({
action: ActionType.READ,
isLoading: true,
});
this.listUseCase.execute(userId).subscribe({
next: (projects: Project[]) => {
this.projects.set(this.projectPresenter.toViewModels(projects));
this.handleError(ActionType.READ, false, null, false);
},
error: (err) => {
this.handleError(ActionType.READ, false, err, false);
},
complete: () => {},
});
}
loadOne(projectId: string) {
this.loading.set({
action: ActionType.READ,
isLoading: true,
});
this.getUseCase.execute(projectId).subscribe({
next: (project: Project) => {
this.project.set(this.projectPresenter.toViewModel(project));
this.handleError(ActionType.READ, false, null, false);
},
error: (err) => {
this.handleError(ActionType.READ, false, err, false);
},
});
}
create(projectDto: CreateProjectDto) {
this.loading.set({
action: ActionType.CREATE,
isLoading: true,
});
this.createUseCase.execute(projectDto).subscribe({
next: (project: Project) => {
this.project.set(this.projectPresenter.toViewModel(project));
this.projects.update((prev) => [...prev, this.projectPresenter.toViewModel(project)]);
this.handleError(ActionType.CREATE, false, null, false);
},
error: (err) => {
this.handleError(ActionType.CREATE, false, err, false);
},
});
}
update(userId: string, data: any) {
this.loading.set({
action: ActionType.UPDATE,
isLoading: true,
});
this.UpdateUseCase.execute(userId, data).subscribe({
next: (project: Project) => {
this.project.set(this.projectPresenter.toViewModel(project));
this.handleError(ActionType.UPDATE, false, null, false);
},
error: (err) => {
this.handleError(ActionType.UPDATE, false, err, false);
},
});
}
private handleError(
action: ActionType = ActionType.NONE,
hasError: boolean,
message: string | null = null,
isLoading = false
) {
this.error.set({ action, hasError, message });
this.loading.set({ action, isLoading });
}
}

View File

@@ -0,0 +1,10 @@
export interface ProjectViewModel {
id: string;
created: string;
updated: string;
nom: string;
lien: string;
description: string;
fichier: string[];
utilisateur: string;
}

View File

@@ -0,0 +1,23 @@
import { ProjectViewModel } from '@app/ui/projects/project.presenter.model';
import { Project } from '@app/domain/projects/project.model';
export class ProjectPresenter {
constructor() {}
toViewModel(project: Project): ProjectViewModel {
return {
id: project.id,
created: project.created,
updated: project.updated,
nom: project.nom,
lien: project.lien,
description: project.description,
fichier: project.fichier,
utilisateur: project.utilisateur,
};
}
toViewModels(projects: Project[]): ProjectViewModel[] {
return projects.map(this.toViewModel);
}
}