From 4e150819bce5588a34a4add10f2590e7d1c852b5 Mon Sep 17 00:00:00 2001 From: hulivet1 Date: Wed, 4 Jan 2023 14:33:29 +0100 Subject: [PATCH] API Compte OK --- Sources/API/public/index.php | 1 + Sources/API/routes/Compte.php | 116 +++++++++++++++++++++ Sources/Data/ClientAPI.cs | 66 +++++++++++- Sources/Data/PersAPI.cs | 10 +- Sources/Data/PersSQL.cs | 10 +- Sources/IHM/App.xaml.cs | 2 +- Sources/IHM/Desktop/ChangePassword.xaml.cs | 2 +- Sources/IHM/Desktop/ForgetPassword.xaml.cs | 4 +- Sources/IHM/Desktop/MainPage.xaml.cs | 4 +- Sources/IHM/Mobile/AjoutBanques.xaml.cs | 8 +- Sources/IHM/Mobile/ChangePassword.xaml.cs | 2 +- Sources/IHM/Mobile/Dashboard.xaml.cs | 4 +- Sources/IHM/Mobile/ErrorPage.xaml.cs | 4 +- Sources/IHM/Mobile/ForgetPassword.xaml.cs | 4 +- Sources/IHM/Mobile/GestionBanques.xaml.cs | 2 +- Sources/IHM/Mobile/MainPage.xaml.cs | 6 +- Sources/Modele/Compte.cs | 15 ++- Sources/Modele/IPersistanceManager.cs | 7 +- Sources/TestFonctionnel/Program.cs | 32 ++++++ Sources/TestsUnitaires/TestUnitPgSQL.cs | 5 +- 20 files changed, 256 insertions(+), 48 deletions(-) create mode 100644 Sources/API/routes/Compte.php diff --git a/Sources/API/public/index.php b/Sources/API/public/index.php index 273acb5..8deb0ff 100644 --- a/Sources/API/public/index.php +++ b/Sources/API/public/index.php @@ -16,6 +16,7 @@ $app->get('/', function (Request $request, Response $response, $args) { require __DIR__.'/../routes/Inscrit.php'; require __DIR__.'/../routes/Banque.php'; +require __DIR__.'/../routes/Compte.php'; $app->run(); ?> \ No newline at end of file diff --git a/Sources/API/routes/Compte.php b/Sources/API/routes/Compte.php new file mode 100644 index 0000000..348c6cd --- /dev/null +++ b/Sources/API/routes/Compte.php @@ -0,0 +1,116 @@ +addBodyParsingMiddleware(); +$app->addRoutingMiddleware(); +$app->addErrorMiddleware(true, true, true); + +/** +* @OA\Get(path="/api/Compte", +* @OA\Response(response="200", description="Succes") +* @OA\Response(response="500", description="Bdd Error") +* ) +*/ + +$app->post('/Compte/FromIdInscrit/', function(Request $request, Response $response,array $args){ + $idInscrit = $request->getParsedBody()["id"]; + $query = 'SELECT * FROM Compte WHERE idInscritBanque=:id'; + + try{ + $db = new Database(); + $conn = $db->connect(); + + $stmt = $conn->prepare($query); + $stmt->bindValue(':id', $idInscrit, PDO::PARAM_STR); + + $stmt->execute(); + $compte = $stmt->fetchAll(PDO::FETCH_OBJ); + + $db = null; + $response->getBody()->write(json_encode($compte)); + return $response + ->withHeader('content-type', 'application/json') + ->withStatus(200); + } catch(PDOException $e){ + $error = array("message" => $e->getMessage()); + + $response->getBody()->write(json_encode($error)); + return $response + ->withHeader('content-type', 'application/json') + ->withStatus(500); + } +}); + +$app->post('/Compte/add/', function(Request $request, Response $response, array $args){ + $nom = $request->getParsedBody()["nom"]; + $idInscrit = $request->getParsedBody()["idInscrit"]; + + $query = "INSERT INTO Compte (nom, idInscritBanque) VALUES (:nom, :idI)"; + + try{ + $db = new Database(); + $conn = $db->connect(); + + $stmt = $conn->prepare($query); + $stmt->bindValue(':nom', $nom, PDO::PARAM_STR); + $stmt->bindValue(':idI', $idInscrit, PDO::PARAM_STR); + + $result = $stmt->execute(); + + $db = null; + $response->getBody()->write(json_encode($result)); + return $response + ->withHeader('content-type', 'application/json') + ->withStatus(200); + } catch(PDOException $e){ + $error = array("message" => $e->getMessage()); + + $response->getBody()->write(json_encode($error)); + return $response + ->withHeader('content-type', 'application/json') + ->withStatus(500); + } +}); + +$app->delete('/Compte/delete/', function (Request $request, Response $response, array $args) { + $nom = $request->getParsedBody()["nom"]; + $idInscrit = $request->getParsedBody()["idInscrit"]; + + $query = "DELETE FROM Compte WHERE nom=:nom AND idInscritBanque=:idI"; + + try{ + $db = new Database(); + $conn = $db->connect(); + + $stmt = $conn->prepare($query); + $stmt->bindValue(':nom', $nom, PDO::PARAM_STR); + $stmt->bindValue(':idI', $idInscrit, PDO::PARAM_STR); + + $result = $stmt->execute(); + + $db = null; + $response->getBody()->write(json_encode($result)); + return $response + ->withHeader('content-type', 'application/json') + ->withStatus(200); + + } catch(PDOException $e){ + $error = array("message" => $e->getMessage()); + + $response->getBody()->write(json_encode($error)); + return $response + ->withHeader('content-type', 'application/json') + ->withStatus(500); + } +}); + + + +?> \ No newline at end of file diff --git a/Sources/Data/ClientAPI.cs b/Sources/Data/ClientAPI.cs index bba968e..22b04e3 100644 --- a/Sources/Data/ClientAPI.cs +++ b/Sources/Data/ClientAPI.cs @@ -30,12 +30,15 @@ namespace Data private const string POST_ADD_BANQUE_INSCRIT_DATA_URL = ROOT_URL + "Banque/add/"; private const string DELETE_BANQUE_INSCRIT_DATA_URL = ROOT_URL + "Banque/delete/"; - //add all routes - + //routes compte + private const string POST_COMPTE_INSCRIT_DATA_URL = ROOT_URL + "Compte/FromIdInscrit/"; + private const string POST_ADD_COMPTE_INSCRIT_DATA_URL = ROOT_URL + "Compte/add/"; + private const string DELETE_COMPTE_INSCRIT_DATA_URL = ROOT_URL + "Compte/delete/"; - private static HttpClient cli = new HttpClient(); + //add all routes + private static HttpClient cli = new HttpClient(); //Inscrit public static async Task> GetInscritsAsync() @@ -193,5 +196,62 @@ namespace Data } + //Comptes + public static async Task> GetCompteAsync(string id) + { + var dataBody = new Dictionary { { "id", id } }; + HttpResponseMessage reponse = await cli.PostAsJsonAsync(POST_COMPTE_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 PostAddCompteInscritAsync(string nomCompte, string idInscrit) + { + var dataBody = new Dictionary { { "nom", nomCompte }, { "idInscrit", idInscrit } }; + HttpResponseMessage reponse = await cli.PostAsJsonAsync(POST_ADD_COMPTE_INSCRIT_DATA_URL, dataBody); + + if (reponse.IsSuccessStatusCode) + { + return true; + } + else + { + throw new HttpRequestException(reponse.StatusCode.ToString()); + } + + } + + public static async Task DeleteCompteInscritAsync(string nomCompte, string idInscrit) + { + var dataBody = new Dictionary { { "nom", nomCompte }, { "idInscrit", idInscrit } }; + + var reponse = + cli.SendAsync( + new HttpRequestMessage(HttpMethod.Delete, DELETE_COMPTE_INSCRIT_DATA_URL) + { + Content = new FormUrlEncodedContent(dataBody) + }) + .Result; + + if (reponse.IsSuccessStatusCode) + { + return true; + } + else + { + throw new HttpRequestException(reponse.StatusCode.ToString()); + } + + } + + } } diff --git a/Sources/Data/PersAPI.cs b/Sources/Data/PersAPI.cs index 435f922..88bcd8f 100644 --- a/Sources/Data/PersAPI.cs +++ b/Sources/Data/PersAPI.cs @@ -65,19 +65,15 @@ namespace Data //actions sur les comptes - public bool AjouterCompte(Compte compte) + public bool AjouterCompte(Compte compte, Inscrit inscrit) { throw new NotImplementedException(); } - public bool SupprimerCompte(Compte compte) + public bool SupprimerCompte(Compte compte, Inscrit inscrit) { throw new NotImplementedException(); } - public bool ModifierCompte(Compte compte) - { - throw new NotImplementedException(); - } - public IList RecupererCompte(Banque banque) + public IList RecupererCompte(Banque banque, Inscrit inscrit) { throw new NotImplementedException(); } diff --git a/Sources/Data/PersSQL.cs b/Sources/Data/PersSQL.cs index 5ea58be..bef6e50 100644 --- a/Sources/Data/PersSQL.cs +++ b/Sources/Data/PersSQL.cs @@ -111,19 +111,15 @@ namespace Data //actions sur les comptes - public bool AjouterCompte(Compte compte) + public bool AjouterCompte(Compte compte, Inscrit inscrit) { throw new NotImplementedException(); } - public bool SupprimerCompte(Compte compte) + public bool SupprimerCompte(Compte compte, Inscrit inscrit) { throw new NotImplementedException(); } - public bool ModifierCompte(Compte compte) - { - throw new NotImplementedException(); - } - public IList RecupererCompte(Banque banque) + public IList RecupererCompte(Banque banque, Inscrit inscrit) { throw new NotImplementedException(); } diff --git a/Sources/IHM/App.xaml.cs b/Sources/IHM/App.xaml.cs index ef859b9..3f5ea45 100644 --- a/Sources/IHM/App.xaml.cs +++ b/Sources/IHM/App.xaml.cs @@ -6,7 +6,7 @@ namespace IHM { public partial class App : Application { - public Manager Manager { get; set; } = new Manager(new Stub()); + public Manager Manager { get; set; } = new Manager(new PersAPI()); public App() { InitializeComponent(); diff --git a/Sources/IHM/Desktop/ChangePassword.xaml.cs b/Sources/IHM/Desktop/ChangePassword.xaml.cs index 4e98fc6..f12cc9c 100644 --- a/Sources/IHM/Desktop/ChangePassword.xaml.cs +++ b/Sources/IHM/Desktop/ChangePassword.xaml.cs @@ -26,7 +26,7 @@ public partial class ChangePassword : ContentPage } else { - Mgr.changePasswordBdd(MailUser, EntryNewMdp.Text); + //Mgr.changePasswordBdd(MailUser, EntryNewMdp.Text); AffichError("mdp changé", "mot de passe bien changé", "ok"); NavigateTo("../.."); } diff --git a/Sources/IHM/Desktop/ForgetPassword.xaml.cs b/Sources/IHM/Desktop/ForgetPassword.xaml.cs index 4cd409b..45b8ad1 100644 --- a/Sources/IHM/Desktop/ForgetPassword.xaml.cs +++ b/Sources/IHM/Desktop/ForgetPassword.xaml.cs @@ -20,7 +20,7 @@ public partial class ForgetPassword : ContentPage { AffichError("Email inconnue", "Aucun compte existant portant cette adresse mail", "OK"); } - if (Mgr.existEmail(EntryMail.Text)) + /*if (Mgr.existEmail(EntryMail.Text)) { Random generator = new Random(); code = generator.Next(0, 1000000).ToString("D6"); @@ -28,7 +28,7 @@ public partial class ForgetPassword : ContentPage ValidateReceptCode.IsVisible = true; ConnexionButton.IsEnabled = false; UpdateArc(); - } + }*/ } private async void AffichError(string s, string s1, string s2) { diff --git a/Sources/IHM/Desktop/MainPage.xaml.cs b/Sources/IHM/Desktop/MainPage.xaml.cs index f6a64b8..24ba442 100644 --- a/Sources/IHM/Desktop/MainPage.xaml.cs +++ b/Sources/IHM/Desktop/MainPage.xaml.cs @@ -20,7 +20,7 @@ public partial class MainPage : ContentPage } else { - if (Mgr.existEmail(EntryMail.Text)) + /*if (Mgr.existEmail(EntryMail.Text)) { if (Mgr.isEqualHash(Mgr.recupMdpBdd(EntryMail.Text), EntryPassworld.Text)) { @@ -35,7 +35,7 @@ public partial class MainPage : ContentPage else { AffichError("Compte inexistant", "Email ou mot de passe invalide", "OK"); - } + }*/ } } diff --git a/Sources/IHM/Mobile/AjoutBanques.xaml.cs b/Sources/IHM/Mobile/AjoutBanques.xaml.cs index e54e6fd..50011ff 100644 --- a/Sources/IHM/Mobile/AjoutBanques.xaml.cs +++ b/Sources/IHM/Mobile/AjoutBanques.xaml.cs @@ -11,7 +11,7 @@ public partial class AjoutBanques : ContentPage { InitializeComponent(); BindingContext = Mgr; - Mgr.importBanques(); + //Mgr.importBanques(); if (OperatingSystem.IsIOS()) { boutonRetour.IsVisible = true; @@ -29,12 +29,12 @@ public partial class AjoutBanques : ContentPage { if (result.FileName.EndsWith("ofx", StringComparison.OrdinalIgnoreCase)) { - IList lesComptes = Mgr.getCompteFromOFX(result.FullPath); - Debug.WriteLine(lesComptes.Count); + //IList lesComptes = Mgr.getCompteFromOFX(result.FullPath); + /*Debug.WriteLine(lesComptes.Count); foreach(Compte compte in lesComptes) { Mgr.User.LesBanques.First().AjouterCompte(compte); - } + }*/ } } diff --git a/Sources/IHM/Mobile/ChangePassword.xaml.cs b/Sources/IHM/Mobile/ChangePassword.xaml.cs index 9abed58..73dfd20 100644 --- a/Sources/IHM/Mobile/ChangePassword.xaml.cs +++ b/Sources/IHM/Mobile/ChangePassword.xaml.cs @@ -25,7 +25,7 @@ public partial class ChangePassword : ContentPage } else { - Mgr.changePasswordBdd(MailUser, EntryNewMdp.Text); + //Mgr.changePasswordBdd(MailUser, EntryNewMdp.Text); AffichError("mdp changé", "mot de passe bien changé", "ok"); NavigateTo("../.."); } diff --git a/Sources/IHM/Mobile/Dashboard.xaml.cs b/Sources/IHM/Mobile/Dashboard.xaml.cs index acd4c06..6be0d67 100644 --- a/Sources/IHM/Mobile/Dashboard.xaml.cs +++ b/Sources/IHM/Mobile/Dashboard.xaml.cs @@ -17,11 +17,11 @@ public partial class DashBoard : ContentPage } - if (!Mgr.testConnexionAsDatabase()) + /* if (!Mgr.testConnexionAsDatabase()) { loadPage(new ErrorPage()); - } + }*/ } diff --git a/Sources/IHM/Mobile/ErrorPage.xaml.cs b/Sources/IHM/Mobile/ErrorPage.xaml.cs index 711e406..7809cb6 100644 --- a/Sources/IHM/Mobile/ErrorPage.xaml.cs +++ b/Sources/IHM/Mobile/ErrorPage.xaml.cs @@ -22,10 +22,10 @@ public partial class ErrorPage : ContentPage public void conIsActive() { - while (!Mgr.testConnexionAsDatabase()) + /*while (!Mgr.testConnexionAsDatabase()) { Thread.Sleep(TIME_TEST_DB); - } + }*/ ConnexionValide(); return; diff --git a/Sources/IHM/Mobile/ForgetPassword.xaml.cs b/Sources/IHM/Mobile/ForgetPassword.xaml.cs index 4384043..69c7b15 100644 --- a/Sources/IHM/Mobile/ForgetPassword.xaml.cs +++ b/Sources/IHM/Mobile/ForgetPassword.xaml.cs @@ -20,7 +20,7 @@ public partial class ForgetPassword : ContentPage { AffichError("Email inconnue", "Aucun compte existant portant cette adresse mail", "OK"); } - if (Mgr.existEmail(EntryMail.Text)){ + /*if (Mgr.existEmail(EntryMail.Text)){ Random generator = new Random(); code = generator.Next(0, 1000000).ToString("D6"); Email.CreateMail(EntryMail.Text, code); @@ -31,7 +31,7 @@ public partial class ForgetPassword : ContentPage else { AffichError("Mail inexistant", "Aucun compte possédant cette adresse email trouvé", "OK"); - } + }*/ } private async void AffichError(string s, string s1, string s2) { diff --git a/Sources/IHM/Mobile/GestionBanques.xaml.cs b/Sources/IHM/Mobile/GestionBanques.xaml.cs index 6d1001a..04681a8 100644 --- a/Sources/IHM/Mobile/GestionBanques.xaml.cs +++ b/Sources/IHM/Mobile/GestionBanques.xaml.cs @@ -11,7 +11,7 @@ public partial class GestionBanques : ContentPage { InitializeComponent(); BindingContext= Mgr; - Mgr.LoadBanques(); + //Mgr.LoadBanques(); if (OperatingSystem.IsIOS()) { boutonRetour.IsVisible = true; diff --git a/Sources/IHM/Mobile/MainPage.xaml.cs b/Sources/IHM/Mobile/MainPage.xaml.cs index c3e55c6..8d020b6 100644 --- a/Sources/IHM/Mobile/MainPage.xaml.cs +++ b/Sources/IHM/Mobile/MainPage.xaml.cs @@ -21,7 +21,7 @@ namespace IHM.Mobile AffichError("Champ invalide", "Veuillez compléter tout les champs", "OK"); } else { - if (Mgr.existEmail(EntryMail.Text)) + /* if (Mgr.existEmail(EntryMail.Text)) { if (Mgr.isEqualHash(Mgr.recupMdpBdd(EntryMail.Text), EntryPassworld.Text)) { @@ -36,13 +36,13 @@ namespace IHM.Mobile else { AffichError("Compte inexistant", "Email ou mot de passe invalide", "OK"); - } + }*/ } } private async void ConnexionValide() { - Mgr.LoadBanques(); + //Mgr.LoadBanques(); await Navigation.PopModalAsync(); } diff --git a/Sources/Modele/Compte.cs b/Sources/Modele/Compte.cs index 53491e8..85a7bc6 100644 --- a/Sources/Modele/Compte.cs +++ b/Sources/Modele/Compte.cs @@ -1,4 +1,5 @@ -using System.ComponentModel; +using Newtonsoft.Json; +using System.ComponentModel; namespace Model { @@ -24,13 +25,19 @@ namespace Model private List lesOpe = new List(); public List LesPla { get; set; } = new List(); public List LesEch { get; set; } = new List(); - public Compte(string id,string nom, double solde) + + [JsonConstructor] + public Compte(string id, string nom) { Identifiant = id; Nom = nom; - Solde = solde; + Solde = 0; DerniereModification = DateTime.Now; } + public Compte(string id,string nom, double solde) : base() + { + Solde = solde; + } public Compte(string id, string nom, double solde, List lesOpe) : base() { LesOpe = lesOpe; @@ -108,7 +115,7 @@ namespace Model public override string ToString() { - return Identifiant + " " + Nom + " " + Solde + " " + DerniereModification + "\n"; + return Identifiant + " " + Nom + " " + Solde + " " + DerniereModification; } } diff --git a/Sources/Modele/IPersistanceManager.cs b/Sources/Modele/IPersistanceManager.cs index 772ce1a..a9194cc 100644 --- a/Sources/Modele/IPersistanceManager.cs +++ b/Sources/Modele/IPersistanceManager.cs @@ -27,10 +27,9 @@ namespace Model //actions sur les comptes - bool AjouterCompte(Compte compte); - bool SupprimerCompte(Compte compte); - bool ModifierCompte(Compte compte); - IList RecupererCompte(Banque banque); + bool AjouterCompte(Compte compte, Inscrit inscrit); + bool SupprimerCompte(Compte compte, Inscrit inscrit); + IList RecupererCompte(Banque banque, Inscrit inscrit); //actions sur les Opérations diff --git a/Sources/TestFonctionnel/Program.cs b/Sources/TestFonctionnel/Program.cs index ceeabb1..fe18523 100644 --- a/Sources/TestFonctionnel/Program.cs +++ b/Sources/TestFonctionnel/Program.cs @@ -115,4 +115,36 @@ banquesId1 = ClientAPI.GetBanqueAsync("1").GetAwaiter().GetResult(); foreach (Banque b in banquesId1) { Console.WriteLine(b); +} + +Console.WriteLine("\n\n\n----Comptes----\n"); + +IList comptes = ClientAPI.GetCompteAsync("1").GetAwaiter().GetResult(); +foreach (Compte c in comptes) +{ + Console.WriteLine(c); +} + +Console.WriteLine("\n----Modifs----\n"); + +bool rrrrrrr = ClientAPI.PostAddCompteInscritAsync("PEL","1").GetAwaiter().GetResult(); +Console.WriteLine("Add Compte for user : " + rrrrrrr + "\n"); + +Console.WriteLine("\n----Verif----\n"); + +comptes = ClientAPI.GetCompteAsync("1").GetAwaiter().GetResult(); +foreach (Compte c in comptes) +{ + Console.WriteLine(c); +} + +rrrrrrr = ClientAPI.DeleteCompteInscritAsync("PEL", "1").GetAwaiter().GetResult(); +Console.WriteLine("Del Compte for user : " + rrrrrrr + "\n"); + +Console.WriteLine("\n----Verif----\n"); + +comptes = ClientAPI.GetCompteAsync("1").GetAwaiter().GetResult(); +foreach (Compte c in comptes) +{ + Console.WriteLine(c); } \ No newline at end of file diff --git a/Sources/TestsUnitaires/TestUnitPgSQL.cs b/Sources/TestsUnitaires/TestUnitPgSQL.cs index 545f3a6..5f158c5 100644 --- a/Sources/TestsUnitaires/TestUnitPgSQL.cs +++ b/Sources/TestsUnitaires/TestUnitPgSQL.cs @@ -1,4 +1,5 @@ -using LinqToPgSQL; +using Data; +using LinqToPgSQL; using Model; using System; using System.Collections.Generic; @@ -13,7 +14,7 @@ namespace TestsUnitaires [Fact] public void testLoadInscrit() { - Manager m = new Manager(new PersLinqToPgSQL()); + Manager m = new Manager(new PersAPI()); //Assert.Null(m.SelectedBanque); //m.LoadInscrit("lucasevard@gmail.com", "test"); //Assert.Equal(m.SelectedInscrits, "00001");