Files
LoreMind/web/src/app/services/template.service.ts
IETM_FIXE\ietm6 88278bd1dd
Some checks failed
Build & Push Images / build (brain) (push) Successful in 1m1s
Build & Push Images / build (core) (push) Successful in 1m32s
E2E Tests / e2e (push) Failing after 4m13s
Build & Push Images / build (web) (push) Successful in 1m53s
Fix : problème d'ascenseur en bas de la page au niveau des templates
Sélection du template par défaut lors de la création d'une page en fonction du dossier
Passage v0.6.2
2026-04-25 01:39:05 +02:00

61 lines
2.0 KiB
TypeScript

import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { shareReplay, tap } from 'rxjs/operators';
import { Template, TemplateCreate } from './template.model';
/**
* Service HTTP pour la gestion des Templates.
* Port de sortie du Frontend vers le Backend Java (/api/templates).
*
* `getByLoreId` est cache via shareReplay(1) — toute mutation
* (create/update/delete) invalide l'ensemble du cache.
*/
@Injectable({ providedIn: 'root' })
export class TemplateService {
private apiUrl = '/api/templates';
private byLoreIdCache = new Map<string, Observable<Template[]>>();
constructor(private http: HttpClient) {}
private invalidate(): void {
this.byLoreIdCache.clear();
}
/** Tous les templates d'un Lore (alimente le panneau sidebar). */
getByLoreId(loreId: string): Observable<Template[]> {
let obs = this.byLoreIdCache.get(loreId);
if (!obs) {
const params = new HttpParams().set('loreId', loreId);
obs = this.http.get<Template[]>(this.apiUrl, { params }).pipe(
tap({ error: () => this.byLoreIdCache.delete(loreId) }),
shareReplay(1)
);
this.byLoreIdCache.set(loreId, obs);
}
return obs;
}
getById(id: string): Observable<Template> {
return this.http.get<Template>(`${this.apiUrl}/${id}`);
}
create(payload: TemplateCreate): Observable<Template> {
return this.http.post<Template>(this.apiUrl, payload).pipe(tap(() => this.invalidate()));
}
update(id: string, template: Template): Observable<Template> {
return this.http.put<Template>(`${this.apiUrl}/${id}`, template).pipe(tap(() => this.invalidate()));
}
delete(id: string): Observable<void> {
return this.http.delete<void>(`${this.apiUrl}/${id}`).pipe(tap(() => this.invalidate()));
}
search(q: string): Observable<Template[]> {
const params = new HttpParams().set('q', q);
return this.http.get<Template[]>(`${this.apiUrl}/search`, { params });
}
}