Transforme la bibliographie en écran de travail D1 à D5
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot
parent
ddc60898da
commit
359f4f59c6
@@ -25,6 +25,7 @@ namespace MaBibli.Client.Services;
|
||||
public sealed class ServiceLivresApi(HttpClient http, CacheHorsLigne cache, EtatReseau reseau)
|
||||
{
|
||||
private static readonly JsonSerializerOptions Json = new(JsonSerializerDefaults.Web);
|
||||
private readonly Dictionary<int, BibliographieDto> _bibliographies = [];
|
||||
|
||||
/// <summary>
|
||||
/// Recharge tous les instantanés depuis le réseau.
|
||||
@@ -615,8 +616,29 @@ public sealed class ServiceLivresApi(HttpClient http, CacheHorsLigne cache, Etat
|
||||
}
|
||||
|
||||
public async Task<ResultatApi<SouhaitDto>> AjouterSouhaitAsync(
|
||||
EnregistrementSouhait saisie, CancellationToken ct = default) =>
|
||||
await EcrireAsync<SouhaitDto>(() => http.PostAsJsonAsync("api/souhaits", saisie, Json, ct), ct);
|
||||
EnregistrementSouhait saisie, CancellationToken ct = default)
|
||||
{
|
||||
var resultat = await EcrireAsync<SouhaitDto>(
|
||||
() => http.PostAsJsonAsync("api/souhaits", saisie, Json, ct), ct);
|
||||
if (resultat.EstOk)
|
||||
{
|
||||
_bibliographies.Clear();
|
||||
}
|
||||
|
||||
return resultat;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<ResultatApi<SouhaitDto>>> AjouterSouhaitsAsync(
|
||||
IReadOnlyList<EnregistrementSouhait> saisies, CancellationToken ct = default)
|
||||
{
|
||||
var resultats = new List<ResultatApi<SouhaitDto>>(saisies.Count);
|
||||
foreach (var saisie in saisies)
|
||||
{
|
||||
resultats.Add(await AjouterSouhaitAsync(saisie, ct));
|
||||
}
|
||||
|
||||
return resultats;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retire une envie. <c>null</c> si c'est fait, sinon le motif à afficher.
|
||||
@@ -735,10 +757,48 @@ public sealed class ServiceLivresApi(HttpClient http, CacheHorsLigne cache, Etat
|
||||
public async Task<BibliographieDto?> ObtenirBibliographieAsync(
|
||||
int auteurId, CancellationToken ct = default)
|
||||
{
|
||||
if (_bibliographies.TryGetValue(auteurId, out var cachee)
|
||||
&& cachee.Etat == EtatSourceBibliographie.Ok)
|
||||
{
|
||||
return cachee;
|
||||
}
|
||||
|
||||
var reponse = await http.GetAsync($"api/auteurs/{auteurId}/bibliographie", ct);
|
||||
return reponse.StatusCode == HttpStatusCode.NotFound
|
||||
var bibliographie = reponse.StatusCode == HttpStatusCode.NotFound
|
||||
? null
|
||||
: await LireAsync<BibliographieDto>(reponse, ct);
|
||||
if (bibliographie?.Etat == EtatSourceBibliographie.Ok)
|
||||
{
|
||||
_bibliographies[auteurId] = bibliographie;
|
||||
}
|
||||
|
||||
return bibliographie;
|
||||
}
|
||||
|
||||
public async Task<string?> MasquerOeuvreBibliographieAsync(
|
||||
int auteurId, string titre, CancellationToken ct = default) =>
|
||||
await ModifierBibliographieAsync(
|
||||
() => http.PostAsJsonAsync(
|
||||
$"api/auteurs/{auteurId}/bibliographie/masque",
|
||||
new MasquageBibliographie { Titre = titre }, Json, ct), ct);
|
||||
|
||||
public async Task<string?> DemasquerOeuvreBibliographieAsync(
|
||||
int auteurId, string titre, CancellationToken ct = default) =>
|
||||
await ModifierBibliographieAsync(
|
||||
() => http.DeleteAsync(
|
||||
$"api/auteurs/{auteurId}/bibliographie/masque?titre={Uri.EscapeDataString(titre)}"), ct);
|
||||
|
||||
private async Task<string?> ModifierBibliographieAsync(
|
||||
Func<Task<HttpResponseMessage>> appel, CancellationToken ct)
|
||||
{
|
||||
var (reponse, motif) = await EnvoyerAsync(appel);
|
||||
if (reponse is null)
|
||||
{
|
||||
return motif;
|
||||
}
|
||||
|
||||
_bibliographies.Clear();
|
||||
return reponse.IsSuccessStatusCode ? null : await MessageErreurAsync(reponse, ct);
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user