|
|
|
@ -31,22 +31,55 @@ namespace WF_WebAdmin.Pages
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
|
|
|
{
|
|
|
|
|
Dailyquote = await Http.GetFromJsonAsync<Quote[]>($"{NavigationManager.BaseUri}fake-dataDailyQuote.json");
|
|
|
|
|
var url = $"{NavigationManager.BaseUri}fake-dataDailyQuote.json";
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var response = await Http.GetFromJsonAsync<Quote[]>(url);
|
|
|
|
|
Dailyquote = response ?? Array.Empty<Quote>(); // Assurer qu'on ne stocke pas null
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Erreur lors du chargement des citations : {ex.Message}");
|
|
|
|
|
Dailyquote = Array.Empty<Quote>(); // Éviter une exception en cas d'erreur réseau
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private async void RandomDailyquote()
|
|
|
|
|
private async Task RandomDailyquote()
|
|
|
|
|
{
|
|
|
|
|
string _jsonFilePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "fake-dataDailyQuote.json");
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string _jsonFilePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "fake-dataDailyQuote.json");
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
|
|
|
|
|
Quote[] quotes = await Http.GetFromJsonAsync<Quote[]>($"{NavigationManager.BaseUri}fake-dataQuote.json");
|
|
|
|
|
var response = await Http.GetFromJsonAsync<Quote[]>($"{NavigationManager.BaseUri}fake-dataQuote.json");
|
|
|
|
|
Quote[] quotes = response ?? Array.Empty<Quote>(); // Empêche null
|
|
|
|
|
|
|
|
|
|
Dailyquote = new Quote[] { quotes.OrderBy(x => random.Next()).First() };
|
|
|
|
|
if (quotes.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
Dailyquote = new Quote[] { quotes.OrderBy(x => random.Next()).First() };
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Aucune citation disponible dans le fichier JSON.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var json = JsonSerializer.Serialize(Dailyquote, new JsonSerializerOptions { WriteIndented = true });
|
|
|
|
|
await File.WriteAllTextAsync(_jsonFilePath, json);
|
|
|
|
|
var json = JsonSerializer.Serialize(Dailyquote, new JsonSerializerOptions { WriteIndented = true });
|
|
|
|
|
await File.WriteAllTextAsync(_jsonFilePath, json);
|
|
|
|
|
|
|
|
|
|
LoggerSaveStub.Log(Logger, LogLevel.Information, "Random change of quote of the day");
|
|
|
|
|
if (Logger != null)
|
|
|
|
|
{
|
|
|
|
|
LoggerSaveStub.Log(Logger, LogLevel.Information, "Random change of quote of the day");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Logger non initialisé.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Erreur dans RandomDailyquote: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|