From 0f3872e8f26e416cddef705c634ac12893a2ff03 Mon Sep 17 00:00:00 2001 From: hulivet1 Date: Wed, 4 Jan 2023 18:12:28 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20routes=20Op=C3=A9ration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/API/public/index.php | 1 + Sources/API/routes/Operation.php | 123 ++++++++++++++++++++++++++ Sources/Data/ClientAPI.cs | 69 +++++++++++++++ Sources/Data/PersAPI.cs | 14 ++- Sources/Data/PersSQL.cs | 8 +- Sources/Data/insert.sql | 16 ++-- Sources/Data/tables.sql | 40 ++++----- Sources/Modele/IPersistanceManager.cs | 5 +- Sources/Modele/Inscrit.cs | 7 -- Sources/Modele/MethodePayement.cs | 3 +- Sources/Modele/Operation.cs | 6 +- Sources/TestFonctionnel/Program.cs | 32 +++++++ 12 files changed, 262 insertions(+), 62 deletions(-) create mode 100644 Sources/API/routes/Operation.php diff --git a/Sources/API/public/index.php b/Sources/API/public/index.php index 8deb0ff..13e0dd3 100644 --- a/Sources/API/public/index.php +++ b/Sources/API/public/index.php @@ -17,6 +17,7 @@ $app->get('/', function (Request $request, Response $response, $args) { require __DIR__.'/../routes/Inscrit.php'; require __DIR__.'/../routes/Banque.php'; require __DIR__.'/../routes/Compte.php'; +require __DIR__.'/../routes/Operation.php'; $app->run(); ?> \ No newline at end of file diff --git a/Sources/API/routes/Operation.php b/Sources/API/routes/Operation.php new file mode 100644 index 0000000..f04585c --- /dev/null +++ b/Sources/API/routes/Operation.php @@ -0,0 +1,123 @@ +addBodyParsingMiddleware(); +$app->addRoutingMiddleware(); +$app->addErrorMiddleware(true, true, true); + +/** +* @OA\Get(path="/api/Operation", +* @OA\Response(response="200", description="Succes") +* @OA\Response(response="500", description="Bdd Error") +* ) +*/ + +$app->post('/Operation/FromIdCompte/', function(Request $request, Response $response,array $args){ + $idCompte = $request->getParsedBody()["id"]; + $query = 'SELECT * FROM Operation WHERE compte=:id'; + + try{ + $db = new Database(); + $conn = $db->connect(); + + $stmt = $conn->prepare($query); + $stmt->bindValue(':id', $idCompte, PDO::PARAM_STR); + + $stmt->execute(); + $ope = $stmt->fetchAll(PDO::FETCH_OBJ); + + $db = null; + $response->getBody()->write(json_encode($ope)); + 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('/Operation/add/', function(Request $request, Response $response, array $args){ + $compte = $request->getParsedBody()["compte"]; + $nom = $request->getParsedBody()["nom"]; + $montant = $request->getParsedBody()["montant"]; + $dateO = $request->getParsedBody()["dateO"]; + $methodePayement = $request->getParsedBody()["methodePayement"]; + $isDebit = $request->getParsedBody()["isDebit"]; + + $query = "INSERT INTO Operation (compte, nom, montant, dateO, methodePayement, isDebit) SELECT :compte,:nom,:montant, STR_TO_DATE(:dateO, '%d/%m/%Y %H:%i:%s' ), :methodePayement, :isD;"; + try{ + $db = new Database(); + $conn = $db->connect(); + + $stmt = $conn->prepare($query); + $stmt->bindValue(':compte', $compte, PDO::PARAM_STR); + $stmt->bindValue(':nom', $nom, PDO::PARAM_STR); + $stmt->bindValue(':montant', $montant, PDO::PARAM_STR); + $stmt->bindValue(':dateO', $dateO, PDO::PARAM_STR); + $stmt->bindValue(':methodePayement', $methodePayement, PDO::PARAM_STR); + $stmt->bindValue(':isD', $isDebit, PDO::PARAM_BOOL); + + $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('/Operation/delete/', function (Request $request, Response $response, array $args) { + $compte = $request->getParsedBody()["compte"]; + $nom = $request->getParsedBody()["nom"]; + + $query = "DELETE FROM Operation WHERE compte=:compte AND nom=:nom"; + + try{ + $db = new Database(); + $conn = $db->connect(); + + $stmt = $conn->prepare($query); + $stmt->bindValue(':compte', $compte, PDO::PARAM_STR); + $stmt->bindValue(':nom', $nom, 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 22b04e3..a6653f4 100644 --- a/Sources/Data/ClientAPI.cs +++ b/Sources/Data/ClientAPI.cs @@ -35,6 +35,10 @@ namespace Data 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/"; + //routes operation + private const string POST_OPERATION_COMPTE_DATA_URL = ROOT_URL + "Operation/FromIdCompte/"; + private const string POST_ADD_OPERATION_COMPTE_DATA_URL = ROOT_URL + "Operation/add/"; + private const string DELETE_OPERATION_COMPTE_DATA_URL = ROOT_URL + "Operation/delete/"; //add all routes @@ -253,5 +257,70 @@ namespace Data } + //Operations + public static async Task> GetOperationAsync(string id) + { + var dataBody = new Dictionary { { "id", id } }; + HttpResponseMessage reponse = await cli.PostAsJsonAsync(POST_OPERATION_COMPTE_DATA_URL, dataBody); + + if (reponse.IsSuccessStatusCode) + { + return JsonConvert.DeserializeObject>(await reponse.Content.ReadAsStringAsync()); + } + else + { + throw new HttpRequestException(reponse.StatusCode.ToString()); + } + + } + + public static async Task PostAddOperationInscritAsync(Compte compte, Operation operation) + { + var dataBody = new Dictionary + { + { "compte", compte.Identifiant }, + { "nom", operation.IntituleOperation }, + { "montant", operation.Montant.ToString() }, + { "dateO", operation.DateOperation.ToString() }, + { "methodePayement", operation.ModePayement.ToString() }, + { "isDebit", operation.IsDebit.ToString() } + }; + HttpResponseMessage reponse = await cli.PostAsJsonAsync(POST_ADD_OPERATION_COMPTE_DATA_URL, dataBody); + + if (reponse.IsSuccessStatusCode) + { + return true; + } + else + { + throw new HttpRequestException(reponse.StatusCode.ToString()); + } + + } + + public static async Task DeleteOperationInscritAsync(string nomCompte, string nomOpe) + { + var dataBody = new Dictionary { { "compte", nomCompte }, { "nom", nomOpe } }; + + var reponse = + cli.SendAsync( + new HttpRequestMessage(HttpMethod.Delete, DELETE_OPERATION_COMPTE_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 88bcd8f..6b92b15 100644 --- a/Sources/Data/PersAPI.cs +++ b/Sources/Data/PersAPI.cs @@ -67,28 +67,24 @@ namespace Data //actions sur les comptes public bool AjouterCompte(Compte compte, Inscrit inscrit) { - throw new NotImplementedException(); + return ClientAPI.PostAddCompteInscritAsync(compte.Nom, inscrit.Id.ToString()).GetAwaiter().GetResult(); } public bool SupprimerCompte(Compte compte, Inscrit inscrit) { - throw new NotImplementedException(); + return ClientAPI.DeleteCompteInscritAsync(compte.Nom, inscrit.Id.ToString()).GetAwaiter().GetResult(); } public IList RecupererCompte(Banque banque, Inscrit inscrit) { - throw new NotImplementedException(); + return ClientAPI.GetCompteAsync(inscrit.Id.ToString()).GetAwaiter().GetResult(); } //actions sur les Opérations - public bool AjouterOperation(Compte compte) - { - throw new NotImplementedException(); - } - public bool SupprimerOperation(Compte compte) + public bool AjouterOperation(Compte compte, Operation operation) { throw new NotImplementedException(); } - public bool ModifierOperation(Compte compte) + public bool SupprimerOperation(Compte compte, Operation operation) { throw new NotImplementedException(); } diff --git a/Sources/Data/PersSQL.cs b/Sources/Data/PersSQL.cs index bef6e50..cc9b9b9 100644 --- a/Sources/Data/PersSQL.cs +++ b/Sources/Data/PersSQL.cs @@ -126,15 +126,11 @@ namespace Data //actions sur les Opérations - public bool AjouterOperation(Compte compte) + public bool AjouterOperation(Compte compte, Operation operation) { throw new NotImplementedException(); } - public bool SupprimerOperation(Compte compte) - { - throw new NotImplementedException(); - } - public bool ModifierOperation(Compte compte) + public bool SupprimerOperation(Compte compte, Operation operation) { throw new NotImplementedException(); } diff --git a/Sources/Data/insert.sql b/Sources/Data/insert.sql index c296050..ef88998 100644 --- a/Sources/Data/insert.sql +++ b/Sources/Data/insert.sql @@ -7,11 +7,11 @@ INSERT INTO Devise VALUES('NZD','DOLLAR NEO-ZELANDAIS'); INSERT INTO Devise VALUES('ZAR','RANd'); -INSERT INTO Inscrit (nom,prenom,mail,mdp)VALUES('EVARD','LUCAS','lucasevard@gmail.com','test'); -INSERT INTO Inscrit (nom,prenom,mail,mdp)VALUES('MONCUL','STEPHANE','stef@gmail.com','teststef'); -INSERT INTO Inscrit (nom,prenom,mail,mdp)VALUES('MENFOUMETTOITOUTNU','RENAUD','renaudtoutnu@gmail.com','test000'); -INSERT INTO Inscrit (nom,prenom,mail,mdp)VALUES('YOUVOI','BENJAMIN','BENJAMIN@gmail.com','BENJAMIN'); -INSERT INTO Inscrit (nom,prenom,mail,mdp)VALUES('TUBEAU','RAOUL','raoullacouille@gmail.com','zizi'); +INSERT INTO Inscrit (nom,prenom,mail,mdp)VALUES('EVARD','LUCAS','lucasevard@gmail.com','Azerty12345678!'); +INSERT INTO Inscrit (nom,prenom,mail,mdp)VALUES('MONCUL','STEPHANE','stef@gmail.com','Azerty12345678!'); +INSERT INTO Inscrit (nom,prenom,mail,mdp)VALUES('MENFOUMETTOITOUTNU','RENAUD','renaudtoutnu@gmail.com','Azerty12345678!'); +INSERT INTO Inscrit (nom,prenom,mail,mdp)VALUES('YOUVOI','BENJAMIN','BENJAMIN@gmail.com','Azerty12345678!'); +INSERT INTO Inscrit (nom,prenom,mail,mdp)VALUES('TUBEAU','RAOUL','raoullacouille@gmail.com','Azerty12345678!'); INSERT INTO DeviseInscrit VALUES('EUR','1'); INSERT INTO DeviseInscrit VALUES('JPY','2'); @@ -35,9 +35,3 @@ INSERT INTO Compte (nom,idInscritBanque)VALUES('LIVRET A','1'); INSERT INTO Compte (nom,idInscritBanque)VALUES('LIVRET A','2'); INSERT INTO Compte (nom,idInscritBanque)VALUES('LIVRET A','3'); INSERT INTO Compte (nom,idInscritBanque)VALUES('LIVRET A','4'); - - -INSERT INTO Planification (nom,credit,compte,datep,datecrea,methodePayement) VALUES ('EDF','190','1',now(),now(),'CB'); -INSERT INTO Planification (nom,credit,compte,datep,datecrea,methodePayement) VALUES ('SPOTIFY','190','2',now(),now(),'Prélevement'); -INSERT INTO Planification (nom,credit,compte,datep,datecrea,methodePayement) VALUES ('NETFLIX','190','3',now(),now(),'Cheque'); -INSERT INTO Planification (nom,credit,compte,datep,datecrea,methodePayement) VALUES ('PLAYSTATION PLUS','190','4',now(),now(),'Espece'); \ No newline at end of file diff --git a/Sources/Data/tables.sql b/Sources/Data/tables.sql index 2bb239c..962c0e0 100644 --- a/Sources/Data/tables.sql +++ b/Sources/Data/tables.sql @@ -62,44 +62,38 @@ CREATE TABLE Compte CREATE TABLE Echeancier ( id MEDIUMINT PRIMARY KEY AUTO_INCREMENT, - nom varchar(40), - credit numeric, compte MEDIUMINT, - debit numeric, - dateE date, - datecrea date, + nom varchar(40), + montant numeric, + dateO date, methodePayement varchar(20), - CONSTRAINT ck_echan CHECK (methodePayement IN ('CB','Cheque','Espece','Prélevement')), - FOREIGN KEY(compte) REFERENCES Compte(id), - UNIQUE (datecrea,compte) + isDebit boolean, + CONSTRAINT ck_ech CHECK (methodePayement IN ('Cb','Esp','Chq','Vir','Pre')), + FOREIGN KEY(compte) REFERENCES Compte(id) ); CREATE TABLE Operation ( id MEDIUMINT PRIMARY KEY AUTO_INCREMENT, - nom varchar(40), - credit numeric, compte MEDIUMINT, - debit numeric, + nom varchar(40), + montant numeric, dateO date, - datecrea date, methodePayement varchar(20), - CONSTRAINT ck_methPaye CHECK (methodePayement IN ('CB','Cheque','Espece','Prélevement')), - FOREIGN KEY(compte) REFERENCES Compte(id), - UNIQUE (datecrea,compte) + isDebit boolean, + CONSTRAINT ck_methPaye CHECK (methodePayement IN ('Cb','Esp','Chq','Vir','Pre')), + FOREIGN KEY(compte) REFERENCES Compte(id) ); CREATE TABLE Planification ( id MEDIUMINT PRIMARY KEY AUTO_INCREMENT, - nom varchar(40), - credit numeric, compte MEDIUMINT, - debit numeric, - dateP date, - datecrea date, + nom varchar(40), + montant numeric, + dateO date, methodePayement varchar(20), - CONSTRAINT ck_planif CHECK (methodePayement IN ('CB','Cheque','Espece','Prélevement')), - FOREIGN KEY(compte) REFERENCES Compte(id), - UNIQUE (datecrea,compte) + isDebit boolean, + CONSTRAINT ck_pla CHECK (methodePayement IN ('Cb','Esp','Chq','Vir','Pre')), + FOREIGN KEY(compte) REFERENCES Compte(id) ); \ No newline at end of file diff --git a/Sources/Modele/IPersistanceManager.cs b/Sources/Modele/IPersistanceManager.cs index a9194cc..fa0c5d3 100644 --- a/Sources/Modele/IPersistanceManager.cs +++ b/Sources/Modele/IPersistanceManager.cs @@ -33,9 +33,8 @@ namespace Model //actions sur les Opérations - bool AjouterOperation(Compte compte); - bool SupprimerOperation(Compte compte); - bool ModifierOperation(Compte compte); + bool AjouterOperation(Compte compte, Operation operation); + bool SupprimerOperation(Compte compte, Operation operation); IList RecupererOperation(Compte compte); diff --git a/Sources/Modele/Inscrit.cs b/Sources/Modele/Inscrit.cs index b35b993..59c1163 100644 --- a/Sources/Modele/Inscrit.cs +++ b/Sources/Modele/Inscrit.cs @@ -97,13 +97,6 @@ namespace Model LesBanques = lesbanques; } - public Inscrit(string mail, int id) - { - Prenom = "Lucas"; - Mail = mail; - Id = id; - } - public Inscrit(List lesbanques) { LesBanques = lesbanques; diff --git a/Sources/Modele/MethodePayement.cs b/Sources/Modele/MethodePayement.cs index 7a7abb8..4aa0053 100644 --- a/Sources/Modele/MethodePayement.cs +++ b/Sources/Modele/MethodePayement.cs @@ -12,6 +12,7 @@ namespace Model Cb, Esp, Chq, - Vir + Vir, + Pre } } \ No newline at end of file diff --git a/Sources/Modele/Operation.cs b/Sources/Modele/Operation.cs index d8b229f..d847bfa 100644 --- a/Sources/Modele/Operation.cs +++ b/Sources/Modele/Operation.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -32,6 +33,7 @@ namespace Model public bool IsDebit { get; private set; } + [JsonConstructor] public Operation(string intituleOperation, double montant, DateTime dateOperation, MethodePayement modePayement, bool isDebit=true) { IntituleOperation = intituleOperation; @@ -45,7 +47,7 @@ namespace Model public override string ToString() { - return IntituleOperation + " " + DateOperation + " " + Montant + " " + ModePayement + " " + IsDebit + "\n"; + return IntituleOperation + " " + DateOperation + " " + Montant + " " + ModePayement + " " + IsDebit; } } } \ No newline at end of file diff --git a/Sources/TestFonctionnel/Program.cs b/Sources/TestFonctionnel/Program.cs index fe18523..315ba68 100644 --- a/Sources/TestFonctionnel/Program.cs +++ b/Sources/TestFonctionnel/Program.cs @@ -147,4 +147,36 @@ comptes = ClientAPI.GetCompteAsync("1").GetAwaiter().GetResult(); foreach (Compte c in comptes) { Console.WriteLine(c); +} + +Console.WriteLine("\n\n\n----Operations----\n"); + +IList operations = ClientAPI.GetOperationAsync("1").GetAwaiter().GetResult(); +foreach (Operation o in operations) +{ + Console.WriteLine(o); +} + +Console.WriteLine("\n----Modifs----\n"); + +rrrrrrr = ClientAPI.PostAddOperationInscritAsync(new Compte("1","PEL"), new Operation("test",100,DateTime.Now,MethodePayement.Cb,true)).GetAwaiter().GetResult(); +Console.WriteLine("Add Ope On Compte : " + rrrrrrr + "\n"); + +Console.WriteLine("\n----Verif----\n"); +operations = ClientAPI.GetOperationAsync("1").GetAwaiter().GetResult(); +foreach (Operation o in operations) +{ + Console.WriteLine(o); +} + +Console.WriteLine("\n----Modifs----\n"); + +rrrrrrr = ClientAPI.DeleteOperationInscritAsync("1", "test").GetAwaiter().GetResult(); +Console.WriteLine("Del Ope On Compte : " + rrrrrrr + "\n"); + +Console.WriteLine("\n----Verif----\n"); +operations = ClientAPI.GetOperationAsync("1").GetAwaiter().GetResult(); +foreach (Operation o in operations) +{ + Console.WriteLine(o); } \ No newline at end of file