Compare commits

...

7 Commits

Author SHA1 Message Date
Louis GUICHARD-MONTGUERS b337cc3950 ajout getSomeQuote
3 months ago
Louis GUICHARD-MONTGUERS fe7300bd56 ajout ValidQuote
3 months ago
Louis GUICHARD-MONTGUERS 5cd39eeee1 ajout removeQuote & updateQuote
3 months ago
Louis GUICHARD-MONTGUERS 6f7fba61f8 ajout getNbQuote & getOneQuote
3 months ago
Louis GUICHARD-MONTGUERS 8a72544c67 ajout AddQuote
3 months ago
Louis GUICHARD-MONTGUERS 2b00fcbf81 ajout getAllQuote
3 months ago
Louis GUICHARD-MONTGUERS ff62632fb3 ajout getAllInvalidQuote
3 months ago

@ -0,0 +1,273 @@
using Npgsql;
using WF_WebAdmin.Model;
namespace WF_WebAdmin.Service
{
public class QuoteServiceLocal : IQuoteService
{
private const string connectionString =
"Host=localhost;Port=5432;Database=wikifantasy3;Username=postgres;Password=postgres";
public async Task addQuote(Quote quote)
{
using var con = new NpgsqlConnection(connectionString);
await con.OpenAsync();
// SQL pour insérer une nouvelle citation dans la base de données
var sql = @"
INSERT INTO quote (id_quote, content, likes,langue)
VALUES (@Id ,@Content, @Likes, @Langue, @IsValide, @CreatedAt)
";
using var cmd = new NpgsqlCommand(sql, con);
// Ajouter les paramètres pour éviter les injections SQL
cmd.Parameters.AddWithValue("@Id", quote.Id);
cmd.Parameters.AddWithValue("@Content", quote.Content);
cmd.Parameters.AddWithValue("@Likes", quote.Like);
cmd.Parameters.AddWithValue("@Langue", quote.Langue);
// Exécution de la commande
await cmd.ExecuteNonQueryAsync();
}
public async Task<List<Quote>> getAllQuote()
{
var result = new List<Quote>();
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
";
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);
}
return result;
}
public async Task<List<Quote>> getAllQuoteInvalid()
{
var result = new List<Quote>();
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);
}
return result;
}
public async Task<int> getNbQuote()
{
using var con = new NpgsqlConnection(connectionString);
await con.OpenAsync();
// SQL pour compter le nombre total de citations
var sql = "SELECT COUNT(*) FROM quote";
using var cmd = new NpgsqlCommand(sql, con);
// Exécution de la commande et récupération du résultat
var result = await cmd.ExecuteScalarAsync();
// Convertir le résultat en entier (si le résultat est null, renvoyer 0)
return result != DBNull.Value ? Convert.ToInt32(result) : 0;
}
public async Task<Quote> getOnequote(int id)
{
using var con = new NpgsqlConnection(connectionString);
await con.OpenAsync();
var sql = @"
SELECT
id_quote AS Id,
content AS Content,
likes AS ""Like"",
langue AS Langue,
'' AS Charac,
'' AS ImgPath,
'' AS TitleSrc,
now() AS DateSrc,
'' AS UserProposition
FROM quote
WHERE id_quote = @Id
";
using var cmd = new NpgsqlCommand(sql, con);
cmd.Parameters.AddWithValue("@Id", id);
using var reader = await cmd.ExecuteReaderAsync();
if (await reader.ReadAsync())
{
return 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.IsDBNull(reader.GetOrdinal("Charac")) ? null : reader.GetString(reader.GetOrdinal("Charac")),
ImgPath = reader.IsDBNull(reader.GetOrdinal("ImgPath")) ? null : reader.GetString(reader.GetOrdinal("ImgPath")),
TitleSrc = reader.IsDBNull(reader.GetOrdinal("TitleSrc")) ? null : reader.GetString(reader.GetOrdinal("TitleSrc")),
DateSrc = reader.GetDateTime(reader.GetOrdinal("DateSrc")),
UserProposition = reader.IsDBNull(reader.GetOrdinal("UserProposition")) ? null : reader.GetString(reader.GetOrdinal("UserProposition"))
};
}
return null;
}
public async Task<List<Quote>> getSomeQuote(int nb, int page)
{
var quotes = await getAllQuote();
if ((page - 1) * nb + nb > quotes.Count())
{
return quotes.GetRange(quotes.Count() - nb, nb);
}
return quotes.GetRange((page - 1) * nb, nb);
}
public Task<List<Quote>> getSomeQuoteInvalid(int nb, int page)
{
throw new NotImplementedException();
}
public async Task removeQuote(Quote 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", quote.Id);
await cmd.ExecuteNonQueryAsync();
}
public Task<List<Quote>> reserchQuote(string reserch, List<string> argument)
{
throw new NotImplementedException();
}
public async Task updateQuote(Quote quote)
{
using var con = new NpgsqlConnection(connectionString);
await con.OpenAsync();
var sql = @"
UPDATE quote
SET
content = @Content,
likes = @Likes,
langue = @Langue,
WHERE id_quote = @Id
";
using var cmd = new NpgsqlCommand(sql, con);
cmd.Parameters.AddWithValue("@Id", quote.Id);
cmd.Parameters.AddWithValue("@Content", quote.Content);
cmd.Parameters.AddWithValue("@Likes", quote.Like);
cmd.Parameters.AddWithValue("@Langue", quote.Langue);
await cmd.ExecuteNonQueryAsync();
}
public async Task validQuote(Quote quote)
{
using var con = new NpgsqlConnection(connectionString);
await con.OpenAsync();
var sql = @"
UPDATE quote
SET
isValid = 'true'
WHERE id_quote = @Id
";
using var cmd = new NpgsqlCommand(sql, con);
cmd.Parameters.AddWithValue("@Id", quote.Id);
}
}
}
Loading…
Cancel
Save