project refactoring en clean archi
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user