72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
|
|
import { Injectable, inject, signal } from '@angular/core';
|
||
|
|
import { MediaRepository } from '../../../../core/media/media-repository';
|
||
|
|
import { MediaAsset } from '../../../../core/media/models/media-asset.model';
|
||
|
|
|
||
|
|
const PAGE_SIZE = 24;
|
||
|
|
|
||
|
|
@Injectable({ providedIn: 'root' })
|
||
|
|
export class MediaLibraryFacade {
|
||
|
|
private readonly repository = inject(MediaRepository);
|
||
|
|
|
||
|
|
readonly items = signal<MediaAsset[]>([]);
|
||
|
|
readonly total = signal(0);
|
||
|
|
readonly loading = signal(false);
|
||
|
|
readonly uploading = signal(false);
|
||
|
|
readonly error = signal<string | null>(null);
|
||
|
|
readonly search = signal('');
|
||
|
|
readonly page = signal(1);
|
||
|
|
|
||
|
|
async load(): Promise<void> {
|
||
|
|
this.loading.set(true);
|
||
|
|
this.error.set(null);
|
||
|
|
try {
|
||
|
|
const result = await this.repository.list({
|
||
|
|
page: this.page(),
|
||
|
|
pageSize: PAGE_SIZE,
|
||
|
|
search: this.search(),
|
||
|
|
});
|
||
|
|
this.items.set(result.items);
|
||
|
|
this.total.set(result.total);
|
||
|
|
} catch {
|
||
|
|
this.error.set('Failed to load media assets.');
|
||
|
|
} finally {
|
||
|
|
this.loading.set(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
setSearch(value: string): void {
|
||
|
|
this.search.set(value);
|
||
|
|
this.page.set(1);
|
||
|
|
void this.load();
|
||
|
|
}
|
||
|
|
|
||
|
|
setPage(page: number): void {
|
||
|
|
this.page.set(page);
|
||
|
|
void this.load();
|
||
|
|
}
|
||
|
|
|
||
|
|
async upload(file: File): Promise<void> {
|
||
|
|
this.uploading.set(true);
|
||
|
|
this.error.set(null);
|
||
|
|
try {
|
||
|
|
await this.repository.upload(file);
|
||
|
|
this.page.set(1);
|
||
|
|
await this.load();
|
||
|
|
} catch {
|
||
|
|
this.error.set('Failed to upload file.');
|
||
|
|
} finally {
|
||
|
|
this.uploading.set(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async remove(id: string): Promise<void> {
|
||
|
|
this.error.set(null);
|
||
|
|
try {
|
||
|
|
await this.repository.remove(id);
|
||
|
|
await this.load();
|
||
|
|
} catch {
|
||
|
|
this.error.set('Failed to delete file.');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|