Mise à jour de 'WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceLocal.cs'
continuous-integration/drone/push Build is passing Details

master
parent 72e29790a3
commit f6936c7caa

@ -1,198 +1,198 @@
using WF_WebAdmin.Converter; using WF_WebAdmin.Converter;
using WF_WebAdmin.Model; using WF_WebAdmin.Model;
using Npgsql; using Npgsql;
namespace WF_WebAdmin.Service namespace WF_WebAdmin.Service
{ {
public class QuoteServiceLocal: IQuoteService public class QuoteServiceLocal: IQuoteService
{ {
private readonly string? _connectionString = "Host=localhost;Port=5432;Username=loguichard3;Password=Reglisse15.;Database=dbloguichard3"; private readonly string? _connectionString = "Host=localhost;Port=5432;Username=;Password=;Database=";
/// <summary> /// <summary>
/// Asynchronously adds a new quote to the database and returns the corresponding <see cref="QuoteDTO"/>. /// Asynchronously adds a new quote to the database and returns the corresponding <see cref="QuoteDTO"/>.
/// </summary> /// </summary>
/// <param name="quote">The <see cref="Quote"/> object to be added to the database.</param> /// <param name="quote">The <see cref="Quote"/> object to be added to the database.</param>
/// <returns>A task representing the asynchronous operation, with a <see cref="QuoteDTO"/> result containing the added quote's data.</returns> /// <returns>A task representing the asynchronous operation, with a <see cref="QuoteDTO"/> result containing the added quote's data.</returns>
/// <remarks> /// <remarks>
/// This method converts the provided <see cref="Quote"/> object into a <see cref="QuoteDTO"/> using <see cref="QuoteExtension"/>. /// This method converts the provided <see cref="Quote"/> object into a <see cref="QuoteDTO"/> using <see cref="QuoteExtension"/>.
/// It then inserts the quote into the PostgreSQL database using a parameterized SQL query with the help of Npgsql. /// It then inserts the quote into the PostgreSQL database using a parameterized SQL query with the help of Npgsql.
/// After successfully inserting the quote, the corresponding <see cref="QuoteDTO"/> is returned to the caller. /// After successfully inserting the quote, the corresponding <see cref="QuoteDTO"/> is returned to the caller.
/// Error handling is in place to catch any issues during the database insertion process, with the exception message logged in case of failure. /// Error handling is in place to catch any issues during the database insertion process, with the exception message logged in case of failure.
/// </remarks> /// </remarks>
public async Task<QuoteDTO> AddQuoteAsync(Quote quote) public async Task<QuoteDTO> AddQuoteAsync(Quote quote)
{ {
QuoteExtension extension = new QuoteExtension(); QuoteExtension extension = new QuoteExtension();
QuoteDTO quoteDTO = extension.QuoteToDTO(quote); QuoteDTO quoteDTO = extension.QuoteToDTO(quote);
// Utilisation de NpgsqlConnection pour PostgreSQL // Utilisation de NpgsqlConnection pour PostgreSQL
using (var connection = new NpgsqlConnection(_connectionString)) using (var connection = new NpgsqlConnection(_connectionString))
{ {
// Définir la requête SQL d'insertion // 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) " + 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)"; "VALUES (@content, @langue, @reason, @source, @character, @user, @img_path)";
// Créer une commande Npgsql // Créer une commande Npgsql
var command = new NpgsqlCommand(commandText, connection); var command = new NpgsqlCommand(commandText, connection);
/* /*
// Ajouter des paramètres à la commande // Ajouter des paramètres à la commande
command.Parameters.AddWithValue("@content", quote.Content); command.Parameters.AddWithValue("@content", quote.Content);
command.Parameters.AddWithValue("@langue", quote.Langue); command.Parameters.AddWithValue("@langue", quote.Langue);
command.Parameters.AddWithValue("@reason", "À vérifier"); // Vous pouvez changer ça si nécessaire command.Parameters.AddWithValue("@reason", "À vérifier"); // Vous pouvez changer ça si nécessaire
command.Parameters.AddWithValue("@source", quote.Source); command.Parameters.AddWithValue("@source", quote.Source);
command.Parameters.AddWithValue("@character", quote.Character); command.Parameters.AddWithValue("@character", quote.Character);
command.Parameters.AddWithValue("@user", quote.User); // Assurez-vous que `quote.User` est correctement défini command.Parameters.AddWithValue("@user", quote.User); // Assurez-vous que `quote.User` est correctement défini
command.Parameters.AddWithValue("@img_path", quote.ImgPath); command.Parameters.AddWithValue("@img_path", quote.ImgPath);
*/ */
try try
{ {
// Ouvrir la connexion à la base de données // Ouvrir la connexion à la base de données
await connection.OpenAsync(); await connection.OpenAsync();
// Exécuter la commande d'insertion // Exécuter la commande d'insertion
await command.ExecuteNonQueryAsync(); await command.ExecuteNonQueryAsync();
} }
catch (Exception ex) catch (Exception ex)
{ {
// Gérer les erreurs ici (par exemple, afficher ou enregistrer les erreurs) // 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}"); Console.WriteLine($"Une erreur est survenue lors de l'ajout de la citation : {ex.Message}");
} }
finally finally
{ {
// Fermer la connexion (automatiquement géré avec `using`, mais ajouté pour explicitement montrer le processus) // Fermer la connexion (automatiquement géré avec `using`, mais ajouté pour explicitement montrer le processus)
await connection.CloseAsync(); await connection.CloseAsync();
} }
} }
// Retourner l'objet DTO pour que vous puissiez l'utiliser ailleurs dans votre application // Retourner l'objet DTO pour que vous puissiez l'utiliser ailleurs dans votre application
return quoteDTO; return quoteDTO;
} }
/// <summary> /// <summary>
/// Asynchronously handles the removal of a quote and returns the corresponding <see cref="QuoteDTO"/>. /// Asynchronously handles the removal of a quote and returns the corresponding <see cref="QuoteDTO"/>.
/// </summary> /// </summary>
/// <param name="quote">The <see cref="Quote"/> object to be removed.</param> /// <param name="quote">The <see cref="Quote"/> object to be removed.</param>
/// <returns>A task representing the asynchronous operation, with a <see cref="QuoteDTO"/> result corresponding to the removed quote.</returns> /// <returns>A task representing the asynchronous operation, with a <see cref="QuoteDTO"/> result corresponding to the removed quote.</returns>
/// <remarks> /// <remarks>
/// This method takes a <see cref="Quote"/> object, converts it into a <see cref="QuoteDTO"/> using the /// This method takes a <see cref="Quote"/> object, converts it into a <see cref="QuoteDTO"/> using the
/// <see cref="QuoteExtension"/>, and then returns the DTO. Note that while this function is named `RemoveQuote`, /// <see cref="QuoteExtension"/>, and then returns the DTO. Note that while this function is named `RemoveQuote`,
/// it currently only converts the quote to a DTO and does not actually perform any database removal operation. /// it currently only converts the quote to a DTO and does not actually perform any database removal operation.
/// You may need to implement additional logic to remove the quote from the database. /// You may need to implement additional logic to remove the quote from the database.
/// </remarks> /// </remarks>
public Task RemoveQuote(Quote quote) public Task RemoveQuote(Quote quote)
{ {
QuoteExtension extension = new QuoteExtension(); QuoteExtension extension = new QuoteExtension();
QuoteDTO quoteDTO = extension.QuoteToDTO(quote); QuoteDTO quoteDTO = extension.QuoteToDTO(quote);
// Return the DTO as the result of this asynchronous operation (though no removal logic is currently implemented) // Return the DTO as the result of this asynchronous operation (though no removal logic is currently implemented)
return Task.FromResult(quoteDTO); return Task.FromResult(quoteDTO);
} }
/// <summary> /// <summary>
/// Asynchronously validates a quote and returns the corresponding <see cref="QuoteDTO"/>. /// Asynchronously validates a quote and returns the corresponding <see cref="QuoteDTO"/>.
/// </summary> /// </summary>
/// <param name="quote">The <see cref="Quote"/> object to be validated.</param> /// <param name="quote">The <see cref="Quote"/> object to be validated.</param>
/// <returns>A task representing the asynchronous operation, with a <see cref="QuoteDTO"/> result corresponding to the validated quote.</returns> /// <returns>A task representing the asynchronous operation, with a <see cref="QuoteDTO"/> result corresponding to the validated quote.</returns>
/// <remarks> /// <remarks>
/// This method takes a <see cref="Quote"/> object, converts it into a <see cref="QuoteDTO"/> using the /// This method takes a <see cref="Quote"/> object, converts it into a <see cref="QuoteDTO"/> using the
/// <see cref="QuoteExtension"/>, and returns the DTO. The method is named `validQuote`, but currently, it only /// <see cref="QuoteExtension"/>, and returns the DTO. The method is named `validQuote`, but currently, it only
/// converts the quote into a DTO and does not perform any actual validation logic. /// converts the quote into a DTO and does not perform any actual validation logic.
/// If you intend to validate the quote (e.g., updating its status in a database), you will need to implement /// If you intend to validate the quote (e.g., updating its status in a database), you will need to implement
/// the actual validation logic separately. /// the actual validation logic separately.
/// </remarks> /// </remarks>
public Task validQuote(Quote quote) public Task validQuote(Quote quote)
{ {
QuoteExtension extension = new QuoteExtension(); QuoteExtension extension = new QuoteExtension();
QuoteDTO quoteDTO = extension.QuoteToDTO(quote); QuoteDTO quoteDTO = extension.QuoteToDTO(quote);
// Return the DTO as the result of this asynchronous operation (though no validation logic is currently implemented) // Return the DTO as the result of this asynchronous operation (though no validation logic is currently implemented)
return Task.FromResult(quoteDTO); return Task.FromResult(quoteDTO);
} }
/// <summary> /// <summary>
/// Asynchronously updates a quote and returns the corresponding <see cref="QuoteDTO"/>. /// Asynchronously updates a quote and returns the corresponding <see cref="QuoteDTO"/>.
/// </summary> /// </summary>
/// <param name="quote">The <see cref="Quote"/> object to be updated.</param> /// <param name="quote">The <see cref="Quote"/> object to be updated.</param>
/// <returns>A task representing the asynchronous operation, with a <see cref="QuoteDTO"/> result corresponding to the updated quote.</returns> /// <returns>A task representing the asynchronous operation, with a <see cref="QuoteDTO"/> result corresponding to the updated quote.</returns>
/// <remarks> /// <remarks>
/// This method takes a <see cref="Quote"/> object, converts it into a <see cref="QuoteDTO"/> using the /// This method takes a <see cref="Quote"/> object, converts it into a <see cref="QuoteDTO"/> using the
/// <see cref="QuoteExtension"/>, and returns the DTO. The method is named `updateQuote`, but currently, it only /// <see cref="QuoteExtension"/>, and returns the DTO. The method is named `updateQuote`, but currently, it only
/// converts the quote into a DTO and does not perform any actual update logic. /// converts the quote into a DTO and does not perform any actual update logic.
/// If you intend to update the quote (e.g., modifying the quote in a database or data source), /// If you intend to update the quote (e.g., modifying the quote in a database or data source),
/// you will need to implement the actual update logic separately. /// you will need to implement the actual update logic separately.
/// </remarks> /// </remarks>
public Task updateQuote(Quote quote) public Task updateQuote(Quote quote)
{ {
QuoteExtension extension = new QuoteExtension(); QuoteExtension extension = new QuoteExtension();
QuoteDTO quoteDTO = extension.QuoteToDTO(quote); QuoteDTO quoteDTO = extension.QuoteToDTO(quote);
// Return the DTO as the result of this asynchronous operation (though no update logic is currently implemented) // Return the DTO as the result of this asynchronous operation (though no update logic is currently implemented)
return Task.FromResult(quoteDTO); return Task.FromResult(quoteDTO);
} }
public Task addQuote(Quote quote) public Task addQuote(Quote quote)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task removeQuote(Quote quote) public Task removeQuote(Quote quote)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<List<Quote>> getAllQuote() public Task<List<Quote>> getAllQuote()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<List<Quote>> getSomeQuote(int nb, int page) public Task<List<Quote>> getSomeQuote(int nb, int page)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<List<Quote>> reserchQuote(string reserch, List<string> argument) public Task<List<Quote>> reserchQuote(string reserch, List<string> argument)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<List<Quote>> getAllQuoteInvalid() public Task<List<Quote>> getAllQuoteInvalid()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<List<Quote>> getSomeQuoteInvalid(int nb, int page) public Task<List<Quote>> getSomeQuoteInvalid(int nb, int page)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<Quote> getOnequote(int id) public Task<Quote> getOnequote(int id)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<int> getNbQuote() public Task<int> getNbQuote()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<List<Character>> getChar() public Task<List<Character>> getChar()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<List<Source>> getSrc() public Task<List<Source>> getSrc()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
} }

Loading…
Cancel
Save