From 77d8653ffde8f7cb04473a4d4dc7ee5ec7a712d7 Mon Sep 17 00:00:00 2001 From: Louis GUICHARD-MONTGUERS Date: Thu, 9 Jan 2025 10:15:39 +0100 Subject: [PATCH] ajout --- .../WF-WebAdmin/Service/QuoteServiceLocal.cs | 119 ++++++++++++++++++ WF-WebAdmin/WF-WebAdmin/_Imports.razor | 1 + 2 files changed, 120 insertions(+) create mode 100644 WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceLocal.cs diff --git a/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceLocal.cs b/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceLocal.cs new file mode 100644 index 0000000..433e5d4 --- /dev/null +++ b/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceLocal.cs @@ -0,0 +1,119 @@ +using WF_WebAdmin.Converter; +using WF_WebAdmin.Model; +using Npgsql; + +namespace WF_WebAdmin.Service +{ + public class QuoteServiceLocal: IQuoteService + { + private readonly string? _connectionString = "Host=localhost;Port=5432;Username=loguichard3;Password=Reglisse15.;Database=dbloguichard3"; + + + + + public async Task AddQuoteAsync(Quote quote) + { + QuoteExtension extension = new QuoteExtension(); + QuoteDTO quoteDTO = extension.QuoteToDTO(quote); + + // Utilisation de NpgsqlConnection pour PostgreSQL + using (var connection = new NpgsqlConnection(_connectionString)) + { + // Définir la requête SQL d'insertion + var commandText = "INSERT INTO Quote (content, langue, reason, id_source, id_caracter, id_user_verif, img_path) " + + "VALUES (@content, @langue, @reason, @source, @character, @user, @img_path)"; + + // Créer une commande Npgsql + var command = new NpgsqlCommand(commandText, connection); + + // Ajouter des paramètres à la commande + command.Parameters.AddWithValue("@content", quote.Content); + command.Parameters.AddWithValue("@langue", quote.Langue); + command.Parameters.AddWithValue("@reason", "À vérifier"); // Vous pouvez changer ça si nécessaire + command.Parameters.AddWithValue("@source", quote.Source); + command.Parameters.AddWithValue("@character", quote.Character); + command.Parameters.AddWithValue("@user", quote.User); // Assurez-vous que `quote.User` est correctement défini + command.Parameters.AddWithValue("@img_path", quote.ImgPath); + + try + { + // Ouvrir la connexion à la base de données + await connection.OpenAsync(); + + // Exécuter la commande d'insertion + await command.ExecuteNonQueryAsync(); + } + catch (Exception ex) + { + // Gérer les erreurs ici (par exemple, afficher ou enregistrer les erreurs) + Console.WriteLine($"Une erreur est survenue lors de l'ajout de la citation : {ex.Message}"); + } + finally + { + // Fermer la connexion (automatiquement géré avec `using`, mais ajouté pour explicitement montrer le processus) + await connection.CloseAsync(); + } + } + + // Retourner l'objet DTO pour que vous puissiez l'utiliser ailleurs dans votre application + return quoteDTO; + } + } + + public Task RemoveQuote(Quote quote) + { + QuoteExtension extension = new QuoteExtension(); + QuoteDTO quoteDTO = extension.QuoteToDTO(quote); + + return Task.FromResult(quoteDTO); + } + + public Task validQuote(Quote quote) + { + QuoteExtension extension = new QuoteExtension(); + QuoteDTO quoteDTO = extension.QuoteToDTO(quote); + + return Task.FromResult(quoteDTO); + } + + public Task updateQuote(Quote quote) + { + QuoteExtension extension = new QuoteExtension(); + QuoteDTO quoteDTO = extension.QuoteToDTO(quote); + + return Task.FromResult(quoteDTO); + } + + public Task> getAllQuote() + { + + } + + public Task> getSomeQuote(int nb, int page) + { + + } + + public Task> getOnequote(int id) + { + + } + + public Task> reserchQuote(string reserch, List argument) + { + + } + + public Task> getAllQuoteInvalid() + { + + } + + public Task> getSomeQuoteInvalid(int nb, int page) + { + + } + + + } +} diff --git a/WF-WebAdmin/WF-WebAdmin/_Imports.razor b/WF-WebAdmin/WF-WebAdmin/_Imports.razor index f261bea..cbc1f08 100644 --- a/WF-WebAdmin/WF-WebAdmin/_Imports.razor +++ b/WF-WebAdmin/WF-WebAdmin/_Imports.razor @@ -9,3 +9,4 @@ @using WF_WebAdmin @using WF_WebAdmin.Shared @using Blazorise.DataGrid +