diff --git a/Code/IHM/App.xaml.cs b/Code/IHM/App.xaml.cs index c799256..87723ac 100644 --- a/Code/IHM/App.xaml.cs +++ b/Code/IHM/App.xaml.cs @@ -15,7 +15,7 @@ namespace IHM /// public partial class App : Application { - public Manager AllInscrits { get; private set; } = new Manager(new LinqToPgSQL.PersLinqToPgSQL()); + public Manager Manager { get; private set; } = new Manager(new LinqToPgSQL.PersLinqToPgSQL()); //public Manager AllInscrits { get; private set; } = new Manager(new Stub()); } diff --git a/Code/IHM/MainWindow.xaml.cs b/Code/IHM/MainWindow.xaml.cs index 09786c4..0de7bd9 100644 --- a/Code/IHM/MainWindow.xaml.cs +++ b/Code/IHM/MainWindow.xaml.cs @@ -22,40 +22,21 @@ namespace IHM /// public partial class MainWindow : Window { - public Manager ListedesInscrits => ((App)Application.Current).AllInscrits; + public Manager Manager => ((App)Application.Current).Manager; public MainWindow() { InitializeComponent(); - - ListedesInscrits.LoadInscrit(); - DataContext = ListedesInscrits; - + DataContext = Manager.SelectedInscrits; } public void testSelect() { - foreach (Inscrit i in ListedesInscrits.ListedesInscrits) - { - - - MessageBox.Show($"{i.Id} + {i.Nom} + {i.Prenom} + {i.Mail} + {i.Mdp}"); - - - } - + MessageBox.Show($"{Manager.SelectedInscrits.Id} + {Manager.SelectedInscrits.Nom} + {Manager.SelectedInscrits.Prenom} + {Manager.SelectedInscrits.Mail} + {Manager.SelectedInscrits.Mdp}"); } public void testSuppression() { - foreach (Inscrit i in ListedesInscrits.ListedesInscrits) - { - if (i.Nom == "YOUVOI") - { - ListedesInscrits.supprimerInscritBdd(i); - } - - } MessageBox.Show("Suppression ok"); } diff --git a/Code/LinqToPgSQL/PersLinqToPgSQL.cs b/Code/LinqToPgSQL/PersLinqToPgSQL.cs index 1eb575d..1d6b0ba 100644 --- a/Code/LinqToPgSQL/PersLinqToPgSQL.cs +++ b/Code/LinqToPgSQL/PersLinqToPgSQL.cs @@ -18,21 +18,12 @@ namespace LinqToPgSQL private static string Password = "lulu"; private static string Port = "5432"; - string connString = - String.Format( - "Server={0};Username={1};Database={2};Port={3};Password={4};SSLMode=Prefer", - Host, - User, - DBname, - Port, - Password); - - public PersLinqToPgSQL() { } - public IEnumerable LoadInscrit() + string connexionBDD = String.Format("Server={0};Username{1};Database{2};Port{3};Password{4};SSLMode=Prefer",Host,User,DBname,Port,Password); + public Inscrit LoadInscrit() { - List ListeInscrits = new List(); - - var conn = new NpgsqlConnection(connString); + int t = 0; + Inscrit i=null; + var conn = new NpgsqlConnection(connexionBDD); Console.Out.WriteLine("Ouverture de la connection"); try { conn.Open(); @@ -41,23 +32,15 @@ namespace LinqToPgSQL { conn.Close(); Environment.Exit(0); - } - - NpgsqlDataReader dbReader = new NpgsqlCommand("SELECT * FROM Inscrit", conn).ExecuteReader(); - while (dbReader.Read()) { - - ListeInscrits.Add(new Inscrit(dbReader.GetString(0), dbReader.GetString(1), dbReader.GetString(2), dbReader.GetString(3), dbReader.GetString(4))); - + t++; + i=new Inscrit(dbReader.GetString(0), dbReader.GetString(1), dbReader.GetString(2), dbReader.GetString(3), dbReader.GetString(4)); } - - dbReader.Close(); - - return ListeInscrits; + return i; } @@ -66,28 +49,21 @@ namespace LinqToPgSQL { /*List ListeInscrits = new List(LoadInscrit());*/ - var conn = new NpgsqlConnection(connString); + var conn = new NpgsqlConnection(connexionBDD); Console.Out.WriteLine("Ouverture de la connection"); conn.Open(); - - string requete = $"DELETE FROM INSCRIT WHERE id=(@p)"; string requeteFKey = $"DELETE FROM DEVISEINSCRIT WHERE idInscrit=(@p2)"; - using (var command1 = new NpgsqlCommand(requeteFKey, conn)) { command1.Parameters.AddWithValue("p2", i.Id); await command1.ExecuteNonQueryAsync(); } - using (var command = new NpgsqlCommand(requete, conn)) { command.Parameters.AddWithValue("p", i.Id); await command.ExecuteNonQueryAsync(); } - - - } } } diff --git a/Code/Model/IPersistanceManager.cs b/Code/Model/IPersistanceManager.cs index fe3121c..05e222f 100644 --- a/Code/Model/IPersistanceManager.cs +++ b/Code/Model/IPersistanceManager.cs @@ -8,7 +8,7 @@ namespace Model { public interface IPersistanceManager { - IEnumerable LoadInscrit(); + Inscrit LoadInscrit(); void SupprimerInscritBdd(Inscrit inscrit); } } diff --git a/Code/Model/Manager.cs b/Code/Model/Manager.cs index 7e86094..442672e 100644 --- a/Code/Model/Manager.cs +++ b/Code/Model/Manager.cs @@ -10,16 +10,8 @@ namespace Model { public class Manager : INotifyPropertyChanged { - public Manager() { } - public event PropertyChangedEventHandler? PropertyChanged; - - public IReadOnlyCollection ListedesInscrits { get; private set; } - private List TousLesInscrits { get; set; } = new List(); - public IPersistanceManager Pers { get; private set; } - - public Inscrit SelectedInscrits { get => selectedInscrits; @@ -33,22 +25,11 @@ namespace Model } } private Inscrit selectedInscrits; - void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - - public void LoadInscrit() - { - TousLesInscrits.Clear(); - TousLesInscrits.AddRange(Pers.LoadInscrit()); - if (TousLesInscrits.Count > 0) - SelectedInscrits = TousLesInscrits.First(); - - } - public Manager(IPersistanceManager persistance) { - ListedesInscrits = new ReadOnlyCollection(TousLesInscrits); + SelectedInscrits = persistance.LoadInscrit(); Pers = persistance; }