nouvelle branche connexion
continuous-integration/drone/push Build is passing Details

connexion
Lucas EVARD 2 years ago
parent 1c39f4b402
commit 9c2913d709

@ -15,7 +15,7 @@ namespace IHM
/// </summary> /// </summary>
public partial class App : Application 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()); //public Manager AllInscrits { get; private set; } = new Manager(new Stub());
} }

@ -22,40 +22,21 @@ namespace IHM
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
public Manager ListedesInscrits => ((App)Application.Current).AllInscrits; public Manager Manager => ((App)Application.Current).Manager;
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
DataContext = Manager.SelectedInscrits;
ListedesInscrits.LoadInscrit();
DataContext = ListedesInscrits;
} }
public void testSelect() public void testSelect()
{ {
foreach (Inscrit i in ListedesInscrits.ListedesInscrits) MessageBox.Show($"{Manager.SelectedInscrits.Id} + {Manager.SelectedInscrits.Nom} + {Manager.SelectedInscrits.Prenom} + {Manager.SelectedInscrits.Mail} + {Manager.SelectedInscrits.Mdp}");
{
MessageBox.Show($"{i.Id} + {i.Nom} + {i.Prenom} + {i.Mail} + {i.Mdp}");
}
} }
public void testSuppression() public void testSuppression()
{ {
foreach (Inscrit i in ListedesInscrits.ListedesInscrits)
{
if (i.Nom == "YOUVOI")
{
ListedesInscrits.supprimerInscritBdd(i);
}
}
MessageBox.Show("Suppression ok"); MessageBox.Show("Suppression ok");
} }

@ -18,21 +18,12 @@ namespace LinqToPgSQL
private static string Password = "lulu"; private static string Password = "lulu";
private static string Port = "5432"; private static string Port = "5432";
string connString = string connexionBDD = String.Format("Server={0};Username{1};Database{2};Port{3};Password{4};SSLMode=Prefer",Host,User,DBname,Port,Password);
String.Format( public Inscrit LoadInscrit()
"Server={0};Username={1};Database={2};Port={3};Password={4};SSLMode=Prefer",
Host,
User,
DBname,
Port,
Password);
public PersLinqToPgSQL() { }
public IEnumerable<Inscrit> LoadInscrit()
{ {
List<Inscrit> ListeInscrits = new List<Inscrit>(); int t = 0;
Inscrit i=null;
var conn = new NpgsqlConnection(connString); var conn = new NpgsqlConnection(connexionBDD);
Console.Out.WriteLine("Ouverture de la connection"); try Console.Out.WriteLine("Ouverture de la connection"); try
{ {
conn.Open(); conn.Open();
@ -41,23 +32,15 @@ namespace LinqToPgSQL
{ {
conn.Close(); conn.Close();
Environment.Exit(0); Environment.Exit(0);
} }
NpgsqlDataReader dbReader = new NpgsqlCommand("SELECT * FROM Inscrit", conn).ExecuteReader(); NpgsqlDataReader dbReader = new NpgsqlCommand("SELECT * FROM Inscrit", conn).ExecuteReader();
while (dbReader.Read()) while (dbReader.Read())
{ {
t++;
ListeInscrits.Add(new Inscrit(dbReader.GetString(0), dbReader.GetString(1), dbReader.GetString(2), dbReader.GetString(3), dbReader.GetString(4))); i=new Inscrit(dbReader.GetString(0), dbReader.GetString(1), dbReader.GetString(2), dbReader.GetString(3), dbReader.GetString(4));
} }
dbReader.Close(); dbReader.Close();
return i;
return ListeInscrits;
} }
@ -66,28 +49,21 @@ namespace LinqToPgSQL
{ {
/*List<Inscrit> ListeInscrits = new List<Inscrit>(LoadInscrit());*/ /*List<Inscrit> ListeInscrits = new List<Inscrit>(LoadInscrit());*/
var conn = new NpgsqlConnection(connString); var conn = new NpgsqlConnection(connexionBDD);
Console.Out.WriteLine("Ouverture de la connection"); Console.Out.WriteLine("Ouverture de la connection");
conn.Open(); conn.Open();
string requete = $"DELETE FROM INSCRIT WHERE id=(@p)"; string requete = $"DELETE FROM INSCRIT WHERE id=(@p)";
string requeteFKey = $"DELETE FROM DEVISEINSCRIT WHERE idInscrit=(@p2)"; string requeteFKey = $"DELETE FROM DEVISEINSCRIT WHERE idInscrit=(@p2)";
using (var command1 = new NpgsqlCommand(requeteFKey, conn)) using (var command1 = new NpgsqlCommand(requeteFKey, conn))
{ {
command1.Parameters.AddWithValue("p2", i.Id); command1.Parameters.AddWithValue("p2", i.Id);
await command1.ExecuteNonQueryAsync(); await command1.ExecuteNonQueryAsync();
} }
using (var command = new NpgsqlCommand(requete, conn)) using (var command = new NpgsqlCommand(requete, conn))
{ {
command.Parameters.AddWithValue("p", i.Id); command.Parameters.AddWithValue("p", i.Id);
await command.ExecuteNonQueryAsync(); await command.ExecuteNonQueryAsync();
} }
} }
} }
} }

@ -8,7 +8,7 @@ namespace Model
{ {
public interface IPersistanceManager public interface IPersistanceManager
{ {
IEnumerable<Inscrit> LoadInscrit(); Inscrit LoadInscrit();
void SupprimerInscritBdd(Inscrit inscrit); void SupprimerInscritBdd(Inscrit inscrit);
} }
} }

@ -10,16 +10,8 @@ namespace Model
{ {
public class Manager : INotifyPropertyChanged public class Manager : INotifyPropertyChanged
{ {
public Manager() { }
public event PropertyChangedEventHandler? PropertyChanged; public event PropertyChangedEventHandler? PropertyChanged;
public IReadOnlyCollection<Inscrit> ListedesInscrits { get; private set; }
private List<Inscrit> TousLesInscrits { get; set; } = new List<Inscrit>();
public IPersistanceManager Pers { get; private set; } public IPersistanceManager Pers { get; private set; }
public Inscrit SelectedInscrits public Inscrit SelectedInscrits
{ {
get => selectedInscrits; get => selectedInscrits;
@ -33,22 +25,11 @@ namespace Model
} }
} }
private Inscrit selectedInscrits; private Inscrit selectedInscrits;
void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 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) public Manager(IPersistanceManager persistance)
{ {
ListedesInscrits = new ReadOnlyCollection<Inscrit>(TousLesInscrits); SelectedInscrits = persistance.LoadInscrit();
Pers = persistance; Pers = persistance;
} }

Loading…
Cancel
Save