diff --git a/Sources/Data/ClientAPI.cs b/Sources/Data/ClientAPI.cs new file mode 100644 index 0000000..d78f859 --- /dev/null +++ b/Sources/Data/ClientAPI.cs @@ -0,0 +1,93 @@ +using Model; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Net.Http.Json; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace Data +{ + public static class ClientAPI + { + private const string ROOT_URL = "http://82.64.164.20:8888/"; + + //routes inscrit + private const string GET_INSCRITS_DATA_URL = ROOT_URL+"Inscrit/"; + private const string POST_EMAIL_INSCRIT_DATA_URL = ROOT_URL+"Inscrit/FromMail/"; + private const string PUT_PASSWORD_INSCRIT_DATA_URL = ROOT_URL+"Inscrit/UpdatePassword/"; + private const string POST_ADD_INSCRIT_DATA_URL = ROOT_URL + "Inscrit/add/"; + + //add all routes + + + private static HttpClient cli = new HttpClient(); + + public static async Task> GetInscritsAsync() + { + HttpResponseMessage reponse = await cli.GetAsync(GET_INSCRITS_DATA_URL); + if(reponse.IsSuccessStatusCode) + { + return JsonConvert.DeserializeObject>(await reponse.Content.ReadAsStringAsync()); + } + else + { + throw new HttpRequestException(reponse.StatusCode.ToString()); + } + } + + public static async Task> GetInscritAsync(string email) + { + var dataBody = new Dictionary { { "email", email } }; + HttpResponseMessage reponse = await cli.PostAsJsonAsync(POST_EMAIL_INSCRIT_DATA_URL, dataBody); + + if (reponse.IsSuccessStatusCode) + { + return JsonConvert.DeserializeObject>(await reponse.Content.ReadAsStringAsync()); + } + else + { + throw new HttpRequestException(reponse.StatusCode.ToString()); + } + + } + + public static async Task PutPasswordInscritAsync(string email, string password) + { + var dataBody = new Dictionary { { "email", email }, { "password", password } }; + HttpResponseMessage reponse = await cli.PutAsJsonAsync(PUT_PASSWORD_INSCRIT_DATA_URL, dataBody); + + if (reponse.IsSuccessStatusCode) + { + return true; + } + else + { + throw new HttpRequestException(reponse.StatusCode.ToString()); + } + + } + + public static async Task PostAddInscritAsync(string nom, string prenom, string email, string password) + { + var dataBody = new Dictionary { { "nom", nom }, { "prenom", prenom }, { "email", email }, { "password", password } }; + HttpResponseMessage reponse = await cli.PostAsJsonAsync(POST_ADD_INSCRIT_DATA_URL, dataBody); + + if (reponse.IsSuccessStatusCode) + { + return true; + } + else + { + throw new HttpRequestException(reponse.StatusCode.ToString()); + } + + } + + } +} diff --git a/Sources/Data/Data.csproj b/Sources/Data/Data.csproj index 27fb33c..8ed68db 100644 --- a/Sources/Data/Data.csproj +++ b/Sources/Data/Data.csproj @@ -18,6 +18,7 @@ + diff --git a/Sources/Data/PersLinqToPgSQL.cs b/Sources/Data/PersLinqToPgSQL.cs index 591609e..1a86bbe 100644 --- a/Sources/Data/PersLinqToPgSQL.cs +++ b/Sources/Data/PersLinqToPgSQL.cs @@ -296,11 +296,11 @@ namespace LinqToPgSQL return ListeCompte; } - public List LoadBanqueId(string id) + public List LoadBanqueId(int id) { - int idnombre = Int16.Parse(id); + ; List ListeBanque = new List(); - Debug.WriteLine(idnombre); + Debug.WriteLine(id); var conn = new NpgsqlConnection(connexionBDD); Console.Out.WriteLine("Ouverture de la connection"); try @@ -314,7 +314,7 @@ namespace LinqToPgSQL Environment.Exit(-1); } NpgsqlCommand cmd = new NpgsqlCommand("select b.nom,b.urlsite,b.urllogo from banque b, inscrbanque ib, Inscrit i where ib.idinscrit =(@p) AND ib.nombanque = b.nom AND ib.idinscrit = i.id;", conn); - cmd.Parameters.AddWithValue("p", idnombre); + cmd.Parameters.AddWithValue("p", id); NpgsqlDataReader dataReader = cmd.ExecuteReader(); while (dataReader.Read()) { diff --git a/Sources/Data/Stub.cs b/Sources/Data/Stub.cs index b4973f6..f77505f 100644 --- a/Sources/Data/Stub.cs +++ b/Sources/Data/Stub.cs @@ -9,14 +9,14 @@ namespace Data public Stub() { lesInscrits.Add(new Inscrit( - "1", + 1, "LIVET", "livet.hugo2003@gmail.com", "Hugo", "Bonjour63." )); } - public string GetId(string mail) + public int GetId(string mail) { foreach(Inscrit i in lesInscrits) { @@ -25,7 +25,7 @@ namespace Data return i.Id; } } - return null; + return -1; } public void SupprimerInscritBdd(Inscrit inscrit) { @@ -121,6 +121,16 @@ namespace Data { return LoadOperation.LoadOperationsFromOFX(ofx); } + + string IPersistanceManager.GetId(string mail) + { + throw new NotImplementedException(); + } + + public List LoadBanqueId(int id) + { + throw new NotImplementedException(); + } } } diff --git a/Sources/IHM/IHM.csproj b/Sources/IHM/IHM.csproj index 2121a48..b512e9e 100644 --- a/Sources/IHM/IHM.csproj +++ b/Sources/IHM/IHM.csproj @@ -100,6 +100,10 @@ + + + + diff --git a/Sources/Modele/IPersistanceManager.cs b/Sources/Modele/IPersistanceManager.cs index 47be43b..1585cc6 100644 --- a/Sources/Modele/IPersistanceManager.cs +++ b/Sources/Modele/IPersistanceManager.cs @@ -18,7 +18,7 @@ namespace Model void ChangePasswordBdd(string mail, string newMdp); string RecupMdpBdd(string mail); int CalculTotalSoldeComtpe(Inscrit user); - List LoadBanqueId(string id); + List LoadBanqueId(int id); public bool TestConnexionAsDatabase(); public List ImportBanques(); public Inscrit GetInscrit(string mail); diff --git a/Sources/Modele/Inscrit.cs b/Sources/Modele/Inscrit.cs index af5aef0..b35b993 100644 --- a/Sources/Modele/Inscrit.cs +++ b/Sources/Modele/Inscrit.cs @@ -6,6 +6,7 @@ using System.Text.RegularExpressions; using Model; using System.Threading.Tasks; using System.ComponentModel; +using Newtonsoft.Json; namespace Model { @@ -15,8 +16,9 @@ namespace Model public event PropertyChangedEventHandler PropertyChanged; - public string Id { get; private set; } - public string Nom { get; private set; } + public int Id { get; set; } + public string Nom { get; set; } + public string Prenom { get; set; } public string Mail { @@ -36,7 +38,7 @@ namespace Model } private string mail; - public string Prenom { get; set; } + public string Mdp { @@ -78,7 +80,9 @@ namespace Model } private List lesBanques; - public Inscrit(string id, string nom, string mail, string prenom, string mdp, double soldeTotal = 0) + + [JsonConstructor] + public Inscrit(int id, string nom, string mail, string prenom, string mdp, double soldeTotal = 0) { Id = id; Nom = nom; @@ -87,13 +91,13 @@ namespace Model Mdp = mdp; SoldeTotal = soldeTotal; } - public Inscrit(string id, string nom, string mail, string prenom, string mdp, double soldeTotal, List lesbanques) + public Inscrit(int id, string nom, string mail, string prenom, string mdp, double soldeTotal, List lesbanques) : this(id, nom, mail, prenom, mdp, soldeTotal) { LesBanques = lesbanques; } - public Inscrit(string mail, string id) + public Inscrit(string mail, int id) { Prenom = "Lucas"; Mail = mail; @@ -104,7 +108,6 @@ namespace Model { LesBanques = lesbanques; } - public void ajouterBanque(Banque banque) { @@ -122,5 +125,9 @@ namespace Model Dev = devise; } + public override string ToString() + { + return Id + " " + Nom + " " + Prenom + " " + Mail + " " + Mdp; + } } } diff --git a/Sources/Modele/Model.csproj b/Sources/Modele/Model.csproj index 5e4631e..b25d8ac 100644 --- a/Sources/Modele/Model.csproj +++ b/Sources/Modele/Model.csproj @@ -17,4 +17,8 @@ 6.5 + + + + \ No newline at end of file diff --git a/Sources/TestFonctionnel/Program.cs b/Sources/TestFonctionnel/Program.cs index b035a56..6db5c86 100644 --- a/Sources/TestFonctionnel/Program.cs +++ b/Sources/TestFonctionnel/Program.cs @@ -1,6 +1,13 @@ using Data; using Model; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System.Net.Http.Headers; +using System.Security.Principal; +using System.Xml.Linq; +//test OFX +/* Console.WriteLine("Test Deserializer OFX - simplifié"); IList comptes= new List(); @@ -16,4 +23,40 @@ foreach (Compte compte in comptes) { Console.WriteLine("\t\t"+operation); } +} +*/ + +//test APIClient + + +Console.WriteLine("Test ClientAPI"); + +IList res = ClientAPI.GetInscritsAsync().GetAwaiter().GetResult(); +foreach(Inscrit i in res) +{ + Console.WriteLine(i); +} + +Console.WriteLine("\n--------\n"); + +IList inscrit = ClientAPI.GetInscritAsync("renaudtoutnu@gmail.com").GetAwaiter().GetResult(); +foreach (Inscrit i in inscrit) +{ + Console.WriteLine(i); +} + +Console.WriteLine("\n----Modifs----\n"); + +bool r = ClientAPI.PutPasswordInscritAsync("lucasevard@gmail.com", "CeciEstUnNouveauMdp123456789!").GetAwaiter().GetResult(); +Console.WriteLine("Changement de mdp : "+r+"\n"); + +bool rr = ClientAPI.PostAddInscritAsync("LIVET", "Hugo", "livet.hugo2003@gmail.com", "EnAvantOuiOui!0").GetAwaiter().GetResult(); +Console.WriteLine("Ajout user : " + rr + "\n"); + +Console.WriteLine("\n----Resultats----\n"); + +IList modif = ClientAPI.GetInscritsAsync().GetAwaiter().GetResult(); +foreach (Inscrit i in modif) +{ + Console.WriteLine(i); } \ No newline at end of file diff --git a/Sources/TestFonctionnel/TestFonctionnel.csproj b/Sources/TestFonctionnel/TestFonctionnel.csproj index 3c5a859..b828247 100644 --- a/Sources/TestFonctionnel/TestFonctionnel.csproj +++ b/Sources/TestFonctionnel/TestFonctionnel.csproj @@ -7,6 +7,10 @@ enable + + + + diff --git a/Sources/TestsUnitaires/TestsUnitaires.csproj b/Sources/TestsUnitaires/TestsUnitaires.csproj index 290d390..581f679 100644 --- a/Sources/TestsUnitaires/TestsUnitaires.csproj +++ b/Sources/TestsUnitaires/TestsUnitaires.csproj @@ -10,6 +10,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive