ValidQuiz #21

Open
tommy.nguyen2 wants to merge 1 commits from ValidQuiz into master

@ -7,7 +7,7 @@ namespace WF_WebAdmin.Pages
public partial class ValidQuiz public partial class ValidQuiz
{ {
private Quiz[] quizzes; private Quiz[] quizzes;
private int totalQuizzes;
[Inject] [Inject]
public HttpClient Http { get; set; } public HttpClient Http { get; set; }

@ -3,34 +3,46 @@
<h3>Citations non validées</h3> <h3>Citations non validées</h3>
@if (quotes is null) @if (quotes == null)
{ {
<p>Chargement des citations...</p> <p> Chargement des quotes ... </p>
}
else if (quotes.Count == 0)
{
<p>Aucune citation en attente de validation.</p>
} }
else else
{ {
<p>Citations en attente de validation :</p> <p> Quotes en attente de validation : </p>
@foreach (var quote in quotes)
{
<div class="QuoteDiv">
<p><strong>ID :</strong> @quote.Id</p>
<p><strong>Contenu :</strong> @quote.Content</p>
<p><strong>Langue :</strong> @quote.Langue</p>
<p><strong>Likes :</strong> @quote.Like</p>
<p><strong>Personnage :</strong> @quote.Charac</p>
<p><strong>Image :</strong> @quote.ImgPath</p>
<p><strong>Source :</strong> @quote.TitleSrc</p>
<p><strong>Date de source :</strong> @quote.DateSrc.ToShortDateString()</p>
<p><strong>Utilisateur proposition :</strong> @quote.UserProposition</p>
<button @onclick="() => ValiderQuote(quote.Id)">Valider</button> <table>
<button @onclick="() => RejeterQuote(quote.Id)">Rejeter</button> <thead>
</div> <tr>
} <th>#</th>
<th>citation</th>
<th>personnage</th>
<th>source</th>
<th>langue</th>
<th>date</th>
<th>actions</th>
</tr>
</thead>
<tbody>
@foreach (var quote in quotes)
{
<tr>
<td>@quote.Id</td>
<td>@quote.Content</td>
<td>@quote.Charac</td>
<td>@quote.TitleSrc</td>
<td>@quote.Langue</td>
<td>@quote.DateSrc</td>
<div class="boutons">
<button style="background-color: darkseagreen" @onclick="() => OnValidButton(quote)">
<img alt="validate" src="check.png" />
</button>
<button style="background-color: indianred" @onclick="() => OnRejectButton(quote)">
<img alt="reject" src="cross.png" />
</button>
</div>
</tr>
}
</tbody>
</table>
} }

@ -11,129 +11,39 @@ namespace WF_WebAdmin.Pages
{ {
public partial class ValidQuote public partial class ValidQuote
{ {
// Chaîne de connexion à adapter private Quote[] quotes;
private const string connectionString =
"Host=localhost;Port=5432;Database=wikifantasy3;Username=postgres;Password=postgres";
private List<Quote> quotes;
[Inject]
public HttpClient Http { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
// On charge toutes les citations dont isValide = false quotes = await Http.GetFromJsonAsync<Quote[]>($"{NavigationManager.BaseUri}fake-dataValidQuote.json");
quotes = await LoadNotValidatedQuotesAsync();
} }
/// <summary> private void OnValidButton(Quote quote)
/// Charge toutes les citations non validées (isValide = false)
/// et mappe les colonnes vers ton modèle.
/// </summary>
private async Task<List<Quote>> LoadNotValidatedQuotesAsync()
{ {
var result = new List<Quote>(); ValidateQuote(quote);
try
{
using var con = new NpgsqlConnection(connectionString);
await con.OpenAsync();
// Sélection des colonnes réellement présentes en DB
// + placeholders pour les autres
var sql = @"
SELECT
id_quote AS Id,
content AS Content,
likes AS ""Like"",
langue AS Langue,
-- Champs pas vraiment en DB : placeholders
'' AS Charac,
'' AS ImgPath,
'' AS TitleSrc,
now() AS DateSrc,
'' AS UserProposition
FROM quote
WHERE isValide = false
";
using var cmd = new NpgsqlCommand(sql, con);
using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
var q = new Quote
{
Id = reader.GetInt32(reader.GetOrdinal("Id")),
Content = reader.GetString(reader.GetOrdinal("Content")),
Like = reader.GetInt32(reader.GetOrdinal("Like")),
Langue = reader.GetString(reader.GetOrdinal("Langue")),
// placeholders
Charac = reader.GetString(reader.GetOrdinal("Charac")),
ImgPath = reader.GetString(reader.GetOrdinal("ImgPath")),
TitleSrc = reader.GetString(reader.GetOrdinal("TitleSrc")),
DateSrc = reader.GetDateTime(reader.GetOrdinal("DateSrc")),
UserProposition = reader.GetString(reader.GetOrdinal("UserProposition"))
};
result.Add(q);
}
}
catch (Exception ex)
{
Console.WriteLine($"[Erreur] LoadNotValidatedQuotesAsync : {ex.Message}");
}
return result;
} }
/// <summary> private void ValidateQuote(Quote quote)
/// Met à jour isValide = true pour la citation.
/// </summary>
private async Task ValiderQuote(int quoteId)
{ {
try Console.WriteLine($"Quote {quote.Id} validated!");
{
using var con = new NpgsqlConnection(connectionString);
await con.OpenAsync();
var sql = "UPDATE quote SET isValide = true WHERE id_quote = @Id";
using var cmd = new NpgsqlCommand(sql, con);
cmd.Parameters.AddWithValue("Id", quoteId);
int rowsAffected = await cmd.ExecuteNonQueryAsync();
if (rowsAffected > 0)
{
// Supprime la quote de la liste pour l'enlever de l'affichage
quotes.RemoveAll(q => q.Id == quoteId);
}
}
catch (Exception ex)
{
Console.WriteLine($"[Erreur] ValiderQuote : {ex.Message}");
}
} }
/// <summary> private void OnRejectButton(Quote quote)
/// Supprime complètement la citation de la base.
/// </summary>
private async Task RejeterQuote(int quoteId)
{ {
try RejectQuote(quote);
{ }
using var con = new NpgsqlConnection(connectionString);
await con.OpenAsync();
var sql = "DELETE FROM quote WHERE id_quote = @Id";
using var cmd = new NpgsqlCommand(sql, con);
cmd.Parameters.AddWithValue("Id", quoteId);
int rowsAffected = await cmd.ExecuteNonQueryAsync(); private void RejectQuote(Quote quote)
if (rowsAffected > 0) {
{ Console.WriteLine($"Quote {quote.Id} rejected!");
quotes.RemoveAll(q => q.Id == quoteId);
}
}
catch (Exception ex)
{
Console.WriteLine($"[Erreur] RejeterQuote : {ex.Message}");
}
} }
} }
} }

Loading…
Cancel
Save