UPDATE: code php remis à zéro pour la partie user, routing toujours incomplet mais les requetes marchent (testées et approuvées) :)
continuous-integration/drone/push Build is failing Details

stub-api
Lucie Bedouret 2 years ago
parent 8a7eb760e0
commit 7291994ad0

@ -1,57 +1,24 @@
<?php
/*
$db_dsn="mysql:dbname=bobParty;host=127.0.0.1;port=8889";
//$db_host = '127.0.0.1';
$db_user = 'root';
$db_password = 'root';
//$db_db = 'bobParty';
//$db_port = 8889;
$mysqli = new mysqli(
$db_host,
$db_user,
$db_password,
$db_db,
$db_port
);
if ($mysqli->connect_error) {
echo 'Errno: '.$mysqli->connect_errno;
echo '<br>';
echo 'Error: '.$mysqli->connect_error;
exit();
}
echo '<br>';
echo 'Host information: '.$mysqli->host_info;
echo '<br>';
echo 'Protocol version: '.$mysqli->protocol_version;
$mysqli->close();
try{
$dbh = new PDO($db_dsn,$db_user,$db_password);
$dbh->exec("set names utf8");
echo 'Success: A proper connection to MySQL was made.';
}catch(PDOException $exception){
echo "Connection error : " . $exception->getMessage();
}
*/
class Database{
public $connection;
public function establishConnection(){
$this->connection=null;
class DatabaseConnection extends PDO{
private $stmt;
try{
$this->connection = new PDO("mysql:dbname=bobParty;host=127.0.0.1;port=8889", "root", "root");
$this->connection->exec("set names utf8");
}catch(PDOException $exception){
echo "Connection error : " . $exception->getMessage();
public function __construct(string $dsn, string $username, string $password){
parent::__construct($dsn,$username,$password);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function execQuery(string $query, array $parameters=[]) :bool{
$this->stmt = parent::prepare($query);
foreach($parameters as $name => $value){
$this->stmt->bindValue($name, $value[0], $value[1]);
}
return $this->connection;
return $this->stmt->execute();
}
public function getRes():array{
return $this->stmt->fetchall();
}
}
?>

@ -0,0 +1,90 @@
<?php
require_once('model/user.php');
class UserGateway{
private $connection;
public function __construct(DatabaseConnection $con){
$this->connection=$con;
}
// execute get method to find all users in database
public function getUsers(){
$query= "SELECT * FROM User";
$this->connection->execQuery($query,[]);
$res=$this->connection->getRes();
foreach($res as $row){
$tabUser[] = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed']);
}
return $tabUser;
}
// execute get method to find one user by his id in database
public function getUserById(string $id):array{
$query= "SELECT * FROM User U WHERE id = :id ";
$arg= array('id'=> array($id,PDO::PARAM_STR));
$this->connection->execQuery($query,$arg);
$res=$this->connection->getRes();
foreach($res as $row){
$usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed']);
}
return $usr;
}
// execute get method to find one user by his username in database
public function getUserByUsername(string $username):?User{
$query= "SELECT * FROM User U WHERE username = :username ";
$arg = array('username'=>array($username,PDO::PARAM_STR));
$this->connection->execQuery($query,$arg);
$res=$this->connection->getRes();
foreach($res as $row){
$usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed']);
}
return $usr;
}
// execute get method to find one user by his username and password for his connection in database
public function getUserForConnection(string $username, string $password):?User{
$query= "SELECT * FROM User U WHERE username = :username AND password = :password";
$arg = array('username'=>array($username,PDO::PARAM_STR),'password'=>array($password,PDO::PARAM_STR));
$this->connection->execQuery($query,$arg);
$res=$this->connection->getRes();
foreach($res as $row){
$usr = new User ($row['id'],$row['username'],$row['password'],$row['nationality'],$row['sex'],$row['dateOfBirth'],$row['currentBobCoins'],$row['totalBobCoins'],$row['nbGamesPlayed']);
}
return $usr;
}
// execute put method to create a new user in database
public function postUser(User $u): void{
if ($u->currentBobCoins != 0 | $u->totalBobCoins != 0| $u->nbGamesPlayed !=0){
echo "new user, can't have any coin or games played";
return;
}
$query = "INSERT INTO User VALUES (:id, :username, :password, :nationality, :sex, :dateOfBirth, 0, 0, 0)";
$arg=array('id' => array($u->id, PDO::PARAM_STR), 'username' => array($u->username, PDO::PARAM_STR), 'password' => array($u->password, PDO::PARAM_STR),'nationality' => array($u->nationality, PDO::PARAM_STR), 'sex' => array($u->sex, PDO::PARAM_STR),'dateOfBirth' => array($u->dateOfBirth, PDO::PARAM_STR));
$this->connection->execQuery($query, $arg);
}
// executing put method to update an user (by his id) in database
public function putUser(User $u){
$query="UPDATE User SET username = :username, password=:password, sex=:sex, nationality=:nationality, currentBobCoins=:currentBobCoins, totalBobCoins=:totalBobCoins, nbGamesPlayed=:nbGamesPlayed WHERE id=:id";
$arg=array(':id' => array($u->id, PDO::PARAM_STR), ':username' => array($u->username, PDO::PARAM_STR), ':password' => array($u->password, PDO::PARAM_STR),':nationality' => array($u->nationality, PDO::PARAM_STR), ':sex' => array($u->sex, PDO::PARAM_STR),':currentBobCoins' => array($u->currentBobCoins, PDO::PARAM_INT),':totalBobCoins' => array($u->totalBobCoins, PDO::PARAM_INT), ':nbGamesPlayed' => array($u->nbGamesPlayed, PDO::PARAM_INT));
$this->connection->execQuery($query, $arg);
}
// exectuing delete method to delete an user in database
public function deleteUser(User $u): void{
$query = "DELETE from User WHERE id = :id";
$arg=array(':id' => array($u->id, PDO::PARAM_STR));
$this->connection->execQuery($query,$arg);
}
}
?>

@ -0,0 +1,124 @@
<?php
include ("./dbConnection.php");
include ("./gateways/userGateway.php");
// Connection to database
// A changer quand la base de données sera hébergée, comment masquer les var?
$dsn ="mysql:dbname=bobParty;host=127.0.0.1;port=8889";
$username="root";
$password="root";
$database = new DatabaseConnection($dsn,$username,$password);
$usergw = new UserGateway($database);
// test postUser
$usr = new User("U0004","billy","bulbe","francais","F","2023-03-12",10,10,2);
$usergw->deleteUser($usr);
$res=$usergw->getUsers();
echo json_encode($res);
// Managing request and routing
$requestMethod = $_SERVER['REQUEST_METHOD'];
$requestName = $_REQUEST['fname'];
if(empty($requestName)){
header("HTTP/1.0 400 Request Name Empty");
http_response_code(400);
}
else{
switch ($requestMethod){
case 'GET':
switch ($requestName){
case 'getUser':
if (!empty($_GET["id"])){
//read an user by his id
$id = intval($_GET["id"]);
$res=$usergw->getUserById($id);
}
elseif (!empty($_GET["username"])){
// read an user by his username
$username = intval($_GET["username"]);
$res=$usergw->getUserByUsername($username);
}
else{
// read all users
$res= $usergw->getUsers();
echo json_encode($res);
}
break;
case 'getMatch':
break;
case 'getMessage':
break;
case 'getConversation ':
break;
}
break;
case 'POST':
switch ($requestName){
case 'postUser':
// rcreate a new user
$res= $usergw->postUser();
echo json_encode($res);
break;
case 'postMatch':
break;
case 'postMessage':
break;
case 'postConversation ':
break;
}
break;
case 'PUT':
switch ($requestName){
case 'putUser':
break;
case 'putMatch':
break;
/* case 'putMessage':
break; */
case 'putConversation ':
break;
}
break;
case 'DELETE':
switch ($requestName){
case 'delUser':
break;
case 'delMatch':
break;
/* case 'delMessage':
break; */
case 'delConversation ':
break;
}
break;
default :
// Invalid request
header("HTTP/1.0 405 Request Name Empty");
http_response_code(405);
break;
}
}
?>

@ -0,0 +1,31 @@
<?php
class User {
// Object attributes
public string $id;
public string $username;
public string $password;
public string $nationality;
public string $sex;
public string $dateOfBirth;
public int $currentBobCoins;
public int $totalBobCoins;
public int $nbGamesPlayed;
public function __construct(string $_id,string $_username,string $_password, string $_nationality,string $_sex, string $_dateOfBirth, int $_currentBobCoins, int $_totalBobCoins, int $_nbGamesPlayed){
$this->id=$_id;
$this->username=$_username;
$this->password=$_password;
$this->nationality=$_nationality;
$this->sex=$_sex;
$this->dateOfBirth=$_dateOfBirth;
$this->currentBobCoins=$_currentBobCoins;
$this->totalBobCoins=$_totalBobCoins;
$this->nbGamesPlayed=$_nbGamesPlayed;
}
}
?>

@ -1,65 +0,0 @@
<?php
class User{
private $connection;
private $table= "User";
// object properties
public $id;
public $username;
public $password;
public $nationality;
public $sex;
public $dateOfBirth;
public $currentBobCoins;
public $totalBobCoins;
public $nbGamesPlayed;
public function __construct($db){
$this->connection=$db;
}
public function read(){
$sqlQuery= "SELECT U.id, U.username, U.password, U.nationality, U.sex, U.dateOfBirth, U.currentBobCoins, U.totalBobCoins, U.nbGamesPlayed FROM User U";
$query = $this->connection->prepare($sqlQuery);
$query->execute();
return $query;
}
public function post(){
$sqlQuery= "INSERT INTO User VALUES(\"" . $this->id . "\",\"" . $this->username . "\",\"" . $this->password . "\",\"" . $this->nationality . "\",\"" . $this->sex . "\",\"" . $this->dateOfBirth . "\",0,0,0)";
$query = $this->connection->prepare($sqlQuery);
$query->execute();
return $query;
}
public function delete(){
$sqlQuery = "DELETE FROM User WHERE username=\"" . $this->username . "\"";
$query = $this->connection->prepare($sqlQuery);
$query->execute();
return $query;
}
public function readOneId(){
$sqlQuery = "SELECT * FROM User WHERE id=\"" . $this->id . "\"";
$query = $this->connection->prepare($sqlQuery);
$query->execute();
return $query;
}
public function readOneUsername(){
$sqlQuery = "SELECT * FROM User WHERE username=\"" . $this->username . "\"";
$query = $this->connection->prepare($sqlQuery);
$query->execute();
return $query;
}
public function put(){
$sqlQuery = "UPDATE User SET username='" . $this->username . "', nationality='" . $this->nationality . "', sex='" . $this->sex . "', dateOfBirth='" . $this->dateOfBirth . "', currentBobCoins='" . $this->currentBobCoins . "', totalBobCoins='" . $this->totalBobCoins . "', nbGamesPlayed='" . $this->nbGamesPlayed . "' WHERE id =\"" . $this->id . "\"";
$query=$this->connection->prepare($sqlQuery);
$query->execute();
return $query;
}
}
?>

@ -1,44 +0,0 @@
<?php
//Headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application.json; charset=UTF-8");
header("Access-Control-Allow-Method: GET");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// Checking that the method is correct and dealing with the error
if($_SERVER['REQUEST_METHOD'] != 'DELETE'){
http_response_code(405);
echo json_encode(["message" => "Unauthorized method"]);
}else{
include_once '../dbConnection.php';
include_once '../models/User.php';
$db= new Database();
$db= $db->establishConnection();
$user = new User($db);
$user->id="4";
$user->username="petitFilou";
$user->password="blblbl";
$user->nationality="Francaise";
$user->sex="M";
$user->dateOfBirth="2002-05-10";
$stmt = $user->delete();
if($stmt->rowCount() > 0){
echo "User deleted successfully";
http_response_code(200);
}
else{
echo "The user can't be deleted because not found in database";
http_response_code(200);
}
}
?>

@ -1,40 +0,0 @@
<?php
//Headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application.json; charset=UTF-8");
header("Access-Control-Allow-Method: GET");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// Checking that the method is correct and dealing with the error
if($_SERVER['REQUEST_METHOD'] != 'POST'){
http_response_code(405);
echo json_encode(["message" => "Unauthorized method"]);
}else{
include_once '../dbConnection.php';
include_once '../models/User.php';
$db= new Database();
$db= $db->establishConnection();
$user = new User($db);
$user->id="U0004";
$user->username="petitFilou";
$user->password="blblbl";
$user->nationality="Francaise";
$user->sex="M";
$user->dateOfBirth="2002-05-10";
$stmt = $user->post();
if($stmt != false){
echo "user created successfully :)";
http_response_code(200);
}
}
?>

@ -1,43 +0,0 @@
<?php
//Headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application.json; charset=UTF-8");
header("Access-Control-Allow-Method: GET");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// Checking that the method is correct and dealing with the error
if($_SERVER['REQUEST_METHOD'] != 'PUT'){
http_response_code(405);
echo json_encode(["message" => "Unauthorized method"]);
}else{
include_once '../dbConnection.php';
include_once '../models/User.php';
$db= new Database();
$db= $db->establishConnection();
$user = new User($db);
$user->id="U0004";
$user->username="petitFilou";
$user->password="blblbl";
$user->nationality="Francaise";
$user->sex="M";
$user->dateOfBirth="2002-05-10";
$user->currentBobCoins=10;
$user->totalBobCoins=10;
$user->nbGamesPlayed=1;
$stmt = $user->put();
if($stmt != false){
http_response_code(200);
echo "User updated successfully :)";
}
}
?>

@ -1,55 +0,0 @@
<?php
//Headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application.json; charset=UTF-8");
header("Access-Control-Allow-Method: GET");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// Checking that the method is correct and dealing with the error
if($_SERVER['REQUEST_METHOD'] != 'GET'){
http_response_code(405);
echo json_encode(["message" => "Unauthorized method"]);
}else{
include_once '../dbConnection.php';
include_once '../models/User.php';
$db= new Database();
$db= $db->establishConnection();
$user = new User($db);
$stmt = $user->read();
if($stmt->rowCount() >= 0){
$arrayUser=[];
$arrayUser['users']=[];
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$user= [
"id" => $id,
"username" => $username,
"password" => $password,
"nationality" => $nationality,
"sex" => $sex,
"dateofBirth" => $dateOfBirth,
"currentBobCoins"=>$currentBobCoins,
"totalBobCoins" => $totalBobCoins,
"nbGamesPlayed" => $nbGamesPlayed,
];
$arrayUser['users'][]=$user;
}
http_response_code(200);
echo json_encode($arrayUser);
}
}
?>

@ -1,50 +0,0 @@
<?php
//Headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application.json; charset=UTF-8");
header("Access-Control-Allow-Method: GET");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// Checking that the method is correct and dealing with the error
if($_SERVER['REQUEST_METHOD'] != 'GET'){
http_response_code(405);
echo json_encode(["message" => "Unauthorized method"]);
}else{
include_once '../dbConnection.php';
include_once '../models/User.php';
$db= new Database();
$db= $db->establishConnection();
$user = new User($db);
$user->id="U0004";
$stmt = $user->readOneId();
if($stmt != false){
$row = $stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
$user= [
"id" => $id,
"username" => $username,
"password" => $password,
"nationality" => $nationality,
"sex" => $sex,
"dateofBirth" => $dateOfBirth,
"currentBobCoins"=>$currentBobCoins,
"totalBobCoins" => $totalBobCoins,
"nbGamesPlayed" => $nbGamesPlayed,
];
http_response_code(200);
echo json_encode($user);
}
}
?>

@ -1,50 +0,0 @@
<?php
//Headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application.json; charset=UTF-8");
header("Access-Control-Allow-Method: GET");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// Checking that the method is correct and dealing with the error
if($_SERVER['REQUEST_METHOD'] != 'GET'){
http_response_code(405);
echo json_encode(["message" => "Unauthorized method"]);
}else{
include_once '../dbConnection.php';
include_once '../models/User.php';
$db= new Database();
$db= $db->establishConnection();
$user = new User($db);
$user->username="lulu";
$stmt = $user->readOneUsername();
if($stmt != false){
$row = $stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
$user= [
"id" => $id,
"username" => $username,
"password" => $password,
"nationality" => $nationality,
"sex" => $sex,
"dateofBirth" => $dateOfBirth,
"currentBobCoins"=>$currentBobCoins,
"totalBobCoins" => $totalBobCoins,
"nbGamesPlayed" => $nbGamesPlayed,
];
http_response_code(200);
echo json_encode($user);
}
}
?>
Loading…
Cancel
Save