Ajout des routes des banques mais bug sur l'api

pull/138/head
Hugo LIVET 2 years ago
parent 12fefde1d7
commit 5099cf76a0

@ -14,7 +14,12 @@ $app->get('/', function (Request $request, Response $response, $args) {
return $response; return $response;
}); });
$app->get('/Inscrit/', function(Request $request, Response $response, $args){
print('TEEEEST');
});
require __DIR__.'/../routes/Inscrit.php'; require __DIR__.'/../routes/Inscrit.php';
require __DIR__.'/../routes/Banque.php';
$app->run(); $app->run();
?> ?>

@ -0,0 +1,142 @@
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use OpenApi\Annotations as OA;
/**
* @OA\Info(title="My First API", version="0.1")
*/
$app = AppFactory::create();
$app->addBodyParsingMiddleware();
$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);
/**
* @OA\Get(path="/api/Banque",
* @OA\Response(response="200", description="Succes")
* @OA\Response(response="500", description="Bdd Error")
* )
*/
$app->get('/Banque/', function(Request $request, Response $response){
$query = "SELECT * FROM Banque";
try{
$db = new Database();
$conn = $db->connect();
$stmt = $conn->query($query);
$inscrits = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
$response->getBody()->write(json_encode($inscrits));
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('/Banque/FromId/', function(Request $request, Response $response,array $args){
$id = $request->getParsedBody()["id"];
$query = 'SELECT * FROM Banque WHERE nom IN (SELECT nomBanque FROM InscrBanque WHERE idInscrit=:id)';
try{
$db = new Database();
$conn = $db->connect();
$stmt = $conn->prepare($query);
$stmt->bindValue(':id', $id, PDO::PARAM_STR);
$stmt->execute();
$inscrit = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
$response->getBody()->write(json_encode($inscrit));
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('/Banque/add/', function(Request $request, Response $response, array $args){
$nom = $request->getParsedBody()["nom"];
$idInscrit = $request->getParsedBody()["idIscrit"];
$query = "INSERT INTO InscrBanque (nomBanque, idInscrit) VALUES (:nom, :idInscrit) WHERE EXISTS (SELECT nom FROM Banque WHERE nom=:nom)";
try{
$db = new Database();
$conn = $db->connect();
$stmt = $conn->prepare($query);
$stmt->bindValue(':nom', $nom, PDO::PARAM_STR);
$stmt->bindValue(':idInscrit', $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('/Banque/delete/', function (Request $request, Response $response, array $args) {
$nom = $request->getParsedBody()["nom"];
$idInscrit = $request->getParsedBody()["idIscrit"];
$query = "DELETE FROM InscrBanque WHERE nom=:nom AND idInscrit=: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);
}
});
?>

@ -24,11 +24,20 @@ namespace Data
private const string POST_ADD_INSCRIT_DATA_URL = ROOT_URL + "Inscrit/add/"; private const string POST_ADD_INSCRIT_DATA_URL = ROOT_URL + "Inscrit/add/";
private const string DELETE_INSCRIT_DATA_URL = ROOT_URL + "Inscrit/delete/"; private const string DELETE_INSCRIT_DATA_URL = ROOT_URL + "Inscrit/delete/";
//routes banque
private const string GET_BANQUES_DATA_URL = ROOT_URL + "Banque/";
private const string POST_BANQUES_INSCRIT_DATA_URL = ROOT_URL + "Banque/FromId/";
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 //add all routes
private static HttpClient cli = new HttpClient(); private static HttpClient cli = new HttpClient();
//Inscrit
public static async Task<List<Inscrit>> GetInscritsAsync() public static async Task<List<Inscrit>> GetInscritsAsync()
{ {
HttpResponseMessage reponse = await cli.GetAsync(GET_INSCRITS_DATA_URL); HttpResponseMessage reponse = await cli.GetAsync(GET_INSCRITS_DATA_URL);
@ -113,5 +122,22 @@ namespace Data
} }
//Banque
public static async Task<List<Banque>> GetBanquesAsync()
{
HttpResponseMessage reponse = await cli.GetAsync(GET_BANQUES_DATA_URL);
if (reponse.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<List<Banque>>(await reponse.Content.ReadAsStringAsync());
}
else
{
throw new HttpRequestException(reponse.StatusCode.ToString());
}
}
} }
} }

@ -46,15 +46,11 @@ namespace Data
//actions sur les banques //actions sur les banques
public bool AjouterBanque(Banque banque) public bool AjouterBanque(Banque banque, Inscrit inscrit)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public bool SupprimerBanque(Banque banque) public bool SupprimerBanque(Banque banque, Inscrit inscrit)
{
throw new NotImplementedException();
}
public bool ModifierBanque(Banque banque)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

@ -92,15 +92,11 @@ namespace Data
//actions sur les banques //actions sur les banques
public bool AjouterBanque(Banque banque) public bool AjouterBanque(Banque banque, Inscrit inscrit)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public bool SupprimerBanque(Banque banque) public bool SupprimerBanque(Banque banque, Inscrit inscrit)
{
throw new NotImplementedException();
}
public bool ModifierBanque(Banque banque)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

@ -1,4 +1,5 @@
using System; using Newtonsoft.Json;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
@ -27,6 +28,7 @@ namespace Model
} }
private List<Compte> listeDesComptes = new List<Compte>(); private List<Compte> listeDesComptes = new List<Compte>();
[JsonConstructor]
public Banque(string nom, string urlSite, string urlLogo) public Banque(string nom, string urlSite, string urlLogo)
{ {
Nom = nom; Nom = nom;
@ -72,5 +74,10 @@ namespace Model
throw new KeyNotFoundException(); throw new KeyNotFoundException();
} }
public override string ToString()
{
return Nom + " " + UrlSite + " " + UrlLogo;
}
} }
} }

@ -20,9 +20,8 @@ namespace Model
//actions sur les banques //actions sur les banques
bool AjouterBanque(Banque banque); bool AjouterBanque(Banque banque, Inscrit inscrit);
bool SupprimerBanque(Banque banque); bool SupprimerBanque(Banque banque, Inscrit inscrit);
bool ModifierBanque(Banque banque);
IList<Banque> RecupererBanques(Inscrit inscrit); IList<Banque> RecupererBanques(Inscrit inscrit);
IList<Banque> RecupererBanquesDisponible(); IList<Banque> RecupererBanquesDisponible();

@ -30,13 +30,13 @@ foreach (Compte compte in comptes)
Console.WriteLine("Test ClientAPI"); Console.WriteLine("Test ClientAPI");
/*
IList<Inscrit> res = ClientAPI.GetInscritsAsync().GetAwaiter().GetResult(); IList<Inscrit> res = ClientAPI.GetInscritsAsync().GetAwaiter().GetResult();
foreach(Inscrit i in res) foreach(Inscrit i in res)
{ {
Console.WriteLine(i); Console.WriteLine(i);
} }
*/
Console.WriteLine("\n--------\n"); Console.WriteLine("\n--------\n");
IList<Inscrit> inscrit = ClientAPI.GetInscritAsync("renaudtoutnu@gmail.com").GetAwaiter().GetResult(); IList<Inscrit> inscrit = ClientAPI.GetInscritAsync("renaudtoutnu@gmail.com").GetAwaiter().GetResult();
@ -71,4 +71,12 @@ modif = ClientAPI.GetInscritsAsync().GetAwaiter().GetResult();
foreach (Inscrit i in modif) foreach (Inscrit i in modif)
{ {
Console.WriteLine(i); Console.WriteLine(i);
}
Console.WriteLine("\n\n\n----Banques----\n");
IList<Banque> banques = ClientAPI.GetBanquesAsync().GetAwaiter().GetResult();
foreach (Banque b in banques)
{
Console.WriteLine(b);
} }
Loading…
Cancel
Save