340 lines
12 KiB
C#
340 lines
12 KiB
C#
using MySqlConnector;
|
|
using TravelMateAdmin.Models;
|
|
using TravelMateAdmin.Configuration;
|
|
|
|
namespace TravelMateAdmin.Services;
|
|
|
|
public class DatabaseService : IDatabaseService
|
|
{
|
|
private readonly string _connectionString;
|
|
public string LastError { get; private set; } = string.Empty;
|
|
|
|
public DatabaseService()
|
|
{
|
|
// Utilise la configuration depuis AppSettings
|
|
_connectionString = AppSettings.GetConnectionString();
|
|
}
|
|
|
|
public DatabaseService(string connectionString)
|
|
{
|
|
_connectionString = connectionString;
|
|
}
|
|
|
|
#region Connection
|
|
|
|
public async Task<bool> TestConnectionAsync()
|
|
{
|
|
try
|
|
{
|
|
System.Diagnostics.Debug.WriteLine("=== TEST CONNEXION DB ===");
|
|
System.Diagnostics.Debug.WriteLine($"Connection String complet: {_connectionString}");
|
|
|
|
using var connection = new MySqlConnection(_connectionString);
|
|
await connection.OpenAsync();
|
|
|
|
System.Diagnostics.Debug.WriteLine("✓ Connexion réussie !");
|
|
LastError = string.Empty;
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LastError = $"{ex.Message}\n\nConnection: {_connectionString}";
|
|
System.Diagnostics.Debug.WriteLine($"❌ Erreur de connexion: {ex.Message}");
|
|
System.Diagnostics.Debug.WriteLine($"Connection String: {_connectionString}");
|
|
System.Diagnostics.Debug.WriteLine($"Type: {ex.GetType().Name}");
|
|
if (ex.InnerException != null)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Inner Exception: {ex.InnerException.Message}");
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Messages
|
|
|
|
public async Task<List<Message>> GetAllMessagesAsync()
|
|
{
|
|
var messages = new List<Message>();
|
|
|
|
try
|
|
{
|
|
using var connection = new MySqlConnection(_connectionString);
|
|
await connection.OpenAsync();
|
|
|
|
var query = "SELECT id, nom, prenom, email, message, done, date_envoi FROM messages ORDER BY date_envoi DESC";
|
|
using var command = new MySqlCommand(query, connection);
|
|
using var reader = await command.ExecuteReaderAsync();
|
|
|
|
while (await reader.ReadAsync())
|
|
{
|
|
messages.Add(new Message
|
|
{
|
|
Id = reader.GetInt32("id"),
|
|
Nom = reader.GetString("nom"),
|
|
Prenom = reader.GetString("prenom"),
|
|
Email = reader.GetString("email"),
|
|
MessageText = reader.GetString("message"),
|
|
Done = reader.GetBoolean("done"),
|
|
CreatedAt = reader.GetDateTime("date_envoi")
|
|
});
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Error getting messages: {ex.Message}");
|
|
}
|
|
|
|
return messages;
|
|
}
|
|
|
|
public async Task<List<Message>> GetMessagesFilteredAsync(bool? isDone)
|
|
{
|
|
if (isDone == null)
|
|
return await GetAllMessagesAsync();
|
|
|
|
var messages = new List<Message>();
|
|
|
|
try
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"=== GetMessagesFilteredAsync ===");
|
|
System.Diagnostics.Debug.WriteLine($"isDone: {isDone}");
|
|
|
|
using var connection = new MySqlConnection(_connectionString);
|
|
await connection.OpenAsync();
|
|
|
|
var query = "SELECT id, nom, prenom, email, message, done, date_envoi FROM messages WHERE done = @done ORDER BY date_envoi DESC";
|
|
System.Diagnostics.Debug.WriteLine($"Query: {query}");
|
|
|
|
using var command = new MySqlCommand(query, connection);
|
|
command.Parameters.AddWithValue("@done", isDone.Value);
|
|
using var reader = await command.ExecuteReaderAsync();
|
|
|
|
while (await reader.ReadAsync())
|
|
{
|
|
messages.Add(new Message
|
|
{
|
|
Id = reader.GetInt32("id"),
|
|
Nom = reader.GetString("nom"),
|
|
Prenom = reader.GetString("prenom"),
|
|
Email = reader.GetString("email"),
|
|
MessageText = reader.GetString("message"),
|
|
Done = reader.GetBoolean("done"),
|
|
CreatedAt = reader.GetDateTime("date_envoi")
|
|
});
|
|
}
|
|
|
|
System.Diagnostics.Debug.WriteLine($"Messages trouvés: {messages.Count}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Error getting filtered messages: {ex.Message}");
|
|
}
|
|
|
|
return messages;
|
|
}
|
|
|
|
public async Task<int> GetMessagesPendingCountAsync()
|
|
{
|
|
try
|
|
{
|
|
using var connection = new MySqlConnection(_connectionString);
|
|
await connection.OpenAsync();
|
|
|
|
var query = "SELECT COUNT(*) FROM messages WHERE done = 0";
|
|
using var command = new MySqlCommand(query, connection);
|
|
var result = await command.ExecuteScalarAsync();
|
|
return Convert.ToInt32(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Error getting pending messages count: {ex.Message}");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public async Task<int> GetMessagesDoneCountAsync()
|
|
{
|
|
try
|
|
{
|
|
using var connection = new MySqlConnection(_connectionString);
|
|
await connection.OpenAsync();
|
|
|
|
var query = "SELECT COUNT(*) FROM messages WHERE done = 1";
|
|
using var command = new MySqlCommand(query, connection);
|
|
var result = await command.ExecuteScalarAsync();
|
|
return Convert.ToInt32(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Error getting done messages count: {ex.Message}");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> UpdateMessageStatusAsync(int id, bool done)
|
|
{
|
|
try
|
|
{
|
|
using var connection = new MySqlConnection(_connectionString);
|
|
await connection.OpenAsync();
|
|
|
|
var query = "UPDATE messages SET done = @done WHERE id = @id";
|
|
using var command = new MySqlCommand(query, connection);
|
|
command.Parameters.AddWithValue("@done", done);
|
|
command.Parameters.AddWithValue("@id", id);
|
|
|
|
var rowsAffected = await command.ExecuteNonQueryAsync();
|
|
return rowsAffected > 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Error updating message status: {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Support Requests
|
|
|
|
public async Task<List<SupportRequest>> GetAllSupportRequestsAsync()
|
|
{
|
|
var requests = new List<SupportRequest>();
|
|
|
|
try
|
|
{
|
|
using var connection = new MySqlConnection(_connectionString);
|
|
await connection.OpenAsync();
|
|
|
|
var query = "SELECT id, nom, prenom, account_email, contact_email, message, done, created_at FROM support_requests ORDER BY created_at DESC";
|
|
using var command = new MySqlCommand(query, connection);
|
|
using var reader = await command.ExecuteReaderAsync();
|
|
|
|
while (await reader.ReadAsync())
|
|
{
|
|
requests.Add(new SupportRequest
|
|
{
|
|
Id = reader.GetInt32("id"),
|
|
Nom = reader.GetString("nom"),
|
|
Prenom = reader.GetString("prenom"),
|
|
AccountEmail = reader.GetString("account_email"),
|
|
ContactEmail = reader.GetString("contact_email"),
|
|
MessageText = reader.GetString("message"),
|
|
Done = reader.GetBoolean("done"),
|
|
CreatedAt = reader.GetDateTime("created_at")
|
|
});
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Error getting support requests: {ex.Message}");
|
|
}
|
|
|
|
return requests;
|
|
}
|
|
|
|
public async Task<List<SupportRequest>> GetSupportRequestsFilteredAsync(bool? isDone)
|
|
{
|
|
if (isDone == null)
|
|
return await GetAllSupportRequestsAsync();
|
|
|
|
var requests = new List<SupportRequest>();
|
|
|
|
try
|
|
{
|
|
using var connection = new MySqlConnection(_connectionString);
|
|
await connection.OpenAsync();
|
|
|
|
var query = "SELECT id, nom, prenom, account_email, contact_email, message, done, created_at FROM support_requests WHERE done = @done ORDER BY created_at DESC";
|
|
using var command = new MySqlCommand(query, connection);
|
|
command.Parameters.AddWithValue("@done", isDone.Value);
|
|
using var reader = await command.ExecuteReaderAsync();
|
|
|
|
while (await reader.ReadAsync())
|
|
{
|
|
requests.Add(new SupportRequest
|
|
{
|
|
Id = reader.GetInt32("id"),
|
|
Nom = reader.GetString("nom"),
|
|
Prenom = reader.GetString("prenom"),
|
|
AccountEmail = reader.GetString("account_email"),
|
|
ContactEmail = reader.GetString("contact_email"),
|
|
MessageText = reader.GetString("message"),
|
|
Done = reader.GetBoolean("done"),
|
|
CreatedAt = reader.GetDateTime("created_at")
|
|
});
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Error getting filtered support requests: {ex.Message}");
|
|
}
|
|
|
|
return requests;
|
|
}
|
|
|
|
public async Task<int> GetSupportRequestsPendingCountAsync()
|
|
{
|
|
try
|
|
{
|
|
using var connection = new MySqlConnection(_connectionString);
|
|
await connection.OpenAsync();
|
|
|
|
var query = "SELECT COUNT(*) FROM support_requests WHERE done = 0";
|
|
using var command = new MySqlCommand(query, connection);
|
|
var result = await command.ExecuteScalarAsync();
|
|
return Convert.ToInt32(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Error getting pending support requests count: {ex.Message}");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public async Task<int> GetSupportRequestsDoneCountAsync()
|
|
{
|
|
try
|
|
{
|
|
using var connection = new MySqlConnection(_connectionString);
|
|
await connection.OpenAsync();
|
|
|
|
var query = "SELECT COUNT(*) FROM support_requests WHERE done = 1";
|
|
using var command = new MySqlCommand(query, connection);
|
|
var result = await command.ExecuteScalarAsync();
|
|
return Convert.ToInt32(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Error getting done support requests count: {ex.Message}");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> UpdateSupportRequestStatusAsync(int id, bool done)
|
|
{
|
|
try
|
|
{
|
|
using var connection = new MySqlConnection(_connectionString);
|
|
await connection.OpenAsync();
|
|
|
|
var query = "UPDATE support_requests SET done = @done WHERE id = @id";
|
|
using var command = new MySqlCommand(query, connection);
|
|
command.Parameters.AddWithValue("@done", done);
|
|
command.Parameters.AddWithValue("@id", id);
|
|
|
|
var rowsAffected = await command.ExecuteNonQueryAsync();
|
|
return rowsAffected > 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Error updating support request status: {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|