From 46be9eb7f4394faa5f7f85bbc224b0d342bd04b3 Mon Sep 17 00:00:00 2001 From: xoxel Date: Thu, 13 Dec 2018 06:42:08 +0100 Subject: [PATCH] routing --- .htaccess | 5 ++ index.php | 4 +- src/API/APIUser.php | 166 ++++++++++++++++++++++++++++++++++++++++---- src/Model/Model.php | 36 +++++++++- 4 files changed, 195 insertions(+), 16 deletions(-) diff --git a/.htaccess b/.htaccess index c7d96d4..848f1a5 100644 --- a/.htaccess +++ b/.htaccess @@ -20,6 +20,11 @@ RewriteCond %{HTTP_HOST} ^fitness.sanchez-mathieu.test$ RewriteCond %{DOCUMENT_ROOT}/static%{REQUEST_URI} -f RewriteRule ^(.*)$ static/$1 [L,E=END:1] +# Redirection des fichiers static pour cauweb.fr +RewriteCond %{HTTP_HOST} ^cauweb.fr$ +RewriteCond %{DOCUMENT_ROOT}/static%{REQUEST_URI} -f +RewriteRule ^(.*)$ static/$1 [L,E=END:1] + # RewriteRule ^haute-garonne/toulouse/installation-entretien-climatisation /haute-garonne/toulouse/climatisation.php [L] # Redirection de toutes les requĂȘtes vers Index.php diff --git a/index.php b/index.php index 74d89a6..d5cb3a6 100644 --- a/index.php +++ b/index.php @@ -35,7 +35,9 @@ if ( count( $pages ) > 1 && $pages[ count( $pages ) - 1 ] == '' ) { if ( $pages[ 0 ] == 'api' && isset( $pages[ 1 ] ) && preg_match( '#^([a-z]+)$#', $pages[ 1 ], $api1 ) && isset( $pages[ 2 ] ) && preg_match( '#^([a-z-]+)$#', $pages[ 2 ], $api2 ) ) { new APIRouter( $api1[ 0 ], $api2[ 0 ] ); -} else if ( preg_match( '#^fitness.sanchez-mathieu\.test$#', $_SERVER[ 'SERVER_NAME' ] ) || preg_match( '#^fitness.sanchez-mathieu\.fr$#', $_SERVER[ 'SERVER_NAME' ] ) ) { +} else if ( preg_match( '#^fitness.sanchez-mathieu\.test$#', $_SERVER[ 'SERVER_NAME' ] ) + || preg_match( '#^fitness.sanchez-mathieu\.fr$#', $_SERVER[ 'SERVER_NAME' ] ) + || preg_match( '#^cauweb\.fr$#', $_SERVER[ 'SERVER_NAME' ] )) { new SiteRouter( $pages ); } else { new Error( 404 ); diff --git a/src/API/APIUser.php b/src/API/APIUser.php index e85a0b4..16894d9 100644 --- a/src/API/APIUser.php +++ b/src/API/APIUser.php @@ -5,30 +5,168 @@ namespace WebProjectFitness\API; use WebProjectFitness\Model\BDTables; use WebProjectFitness\Model\Model; -class APIUser extends API { +class APIUser extends API +{ private $declaredFunctions = [ 'create' => [ - 'method' => 'GET', + 'method' => 'POST', 'params' => [ -// 'id' => [ -// 'required' => true, -// 'type' => 'string' -// ] + ] + ], + 'modify' => [ + 'method' => 'POST', + 'params' => [ + 'user_id' => [ + 'required' => true, + 'type' => 'string' + ], + 'name' => [ + 'required' => true, + 'type' => 'string' + ] + ] + ], + 'favorite-delete' => [ + 'method' => 'POST', + 'params' => [ + 'id_user' => [ + 'required' => true, + 'type' => 'string' + ], + 'id_exercise' => [ + 'required' => true, + 'type' => 'int' + ] + ] + ], + 'favorite-add' => [ + 'method' => 'POST', + 'params' => [ + 'id_user' => [ + 'required' => true, + 'type' => 'string' + ], + 'id_exercise' => [ + 'required' => true, + 'type' => 'int' + ] + ] + ], + 'training-add' => [ + 'method' => 'POST', + 'params' => [ + 'id_user' => [ + 'required' => true, + 'type' => 'string' + ], + 'id_exercise' => [ + 'required' => true, + 'type' => 'int' + ], 'id_order' => [ + 'required' => true, + 'type' => 'int' + ] + ] + ], + 'training-delete' => [ + 'method' => 'POST', + 'params' => [ + 'id_user' => [ + 'required' => true, + 'type' => 'string' + ], + 'id_exercise' => [ + 'required' => true, + 'type' => 'int' + ], 'id_order' => [ + 'required' => true, + 'type' => 'int' + ] + ] + ], + 'training-change-order' => [ + 'method' => 'POST', + 'params' => [ + 'id_user' => [ + 'required' => true, + 'type' => 'string' + ], + 'id_exercise' => [ + 'required' => true, + 'type' => 'int' + ], 'id_order' => [ + 'required' => true, + 'type' => 'int' + ], + 'new_order' => [ + 'required' => true, + 'type' => 'int' + ] ] ] + + ]; - public function __construct() { - parent::__construct( $this->declaredFunctions ); + public function __construct() + { + parent::__construct($this->declaredFunctions); } - public function create( $data ) { - Model::insert( BDTables::USER, [ - "name" => "Mathi", - "user_id" => "bite" - ] ); - $this->returnJson( [ 'body' => 'truc' ] ); + + //functions for user table. + public function create($data) + { + $id = uniqid(); + $id = substr($id, 0, 6); + Model::insert(BDTables::USER, [ + "name" => "", + "user_id" => $id + ]); + $this->returnJson(['id' => $id]); + } + + public function modify($data) + { + Model::update(BDTables::USER, ['name' => $data['name']], "user_id", $data['user_id']); + $this->returnJson(['name' => $data['name'], 'id' => $data['user_id']]); + } + + + //Functions for favorite table. + public function favoriteAdd($data) + { + Model::insert(BDTables::FAVORITE, ['id_user' => $data['id_user'], 'id_exercise' => $data['id_exercise']]); + $this->returnJson(['id_user' => $data['id_user'], 'id_exercise' => $data['id_exercise']]); + } + + public function favoriteDelete($data) + { + Model::delete(BDTables::FAVORITE, ['id_user' => $data['id_user'], 'id_exercise' => $data['id_exercise']]); + $this->returnJson(['id_user' => $data['id_user'], 'id_exercise' => $data['id_exercise']]); + + } + + //functions for training table + public function trainingAdd($data) + { + Model::insert(BDTables::TRAINING, ['id_user' => $data['id_user'], 'id_exercise' => $data['id_exercise'], 'id_order' => $data['id_order']]); + $this->returnJson(['id_user' => $data['id_user'], 'id_exercise' => $data['id_exercise'], 'id_order' => $data['id_order']]); + } + + public function trainingDelete($data) + { + Model::delete(BDTables::TRAINING, ['id_user' => $data['id_user'], 'id_exercise' => $data['id_exercise'], 'id_order' => $data['id_order']]); + + $this->returnJson(['id_user' => $data['id_user'], 'id_exercise' => $data['id_exercise'], 'id_order' => $data['id_order']]); + } + + public function trainingChangeOrder($data) + { + Model::update_order(BDTables::TRAINING, ['id_user' => $data['id_user'], 'id_order' => $data['id_order'], 'id_exercise' => $data['id_exercise'] ], $data['new_order']); + + $this->returnJson(['id_user' => $data['id_user'], 'id_exercise' => $data['id_exercise'], 'id_order' => $data['id_order'], 'new_order' => $data['new_order']]); } } \ No newline at end of file diff --git a/src/Model/Model.php b/src/Model/Model.php index d0bae79..98f8f35 100644 --- a/src/Model/Model.php +++ b/src/Model/Model.php @@ -28,7 +28,7 @@ class Model { * @param string $idColumn * @param int $idValue */ - public static function update( string $tableName, array $data, string $idColumn, int $idValue ) { + public static function update( string $tableName, array $data, string $idColumn, string $idValue ) { $reqStr = 'UPDATE ' . $tableName . ' SET '; $lastKey = endKey( $data ); foreach ( $data as $key => $value ) { @@ -45,6 +45,40 @@ class Model { $req = BDD::instance()->prepare( $reqStr ); $req->execute( $data ); } + + public static function delete (string $tableName, array $data){ + $reqStr = 'DELETE FROM ' . $tableName . ' WHERE '; + $lastKey = endKey( $data ); + foreach ( $data as $key => $value ) { + $reqStr .= $key . ' = :' . $key; + if ( $key != $lastKey ) { + $reqStr .= ' AND '; + } + } + + //echo $reqStr; exit(); + + $req = BDD::instance()->prepare( $reqStr ); + $req->execute( $data ); + + } + + public static function update_order( string $tableName, array $data, string $newOrder) { + $reqStr = 'UPDATE ' . $tableName . ' SET id_order = :newOrder WHERE '; + $lastKey = endKey( $data ); + foreach ( $data as $key => $value ) { + $reqStr .= $key . ' = :' . $key; + if ( $key != $lastKey ) { + $reqStr .= ' AND '; + } + } + $data[ 'newOrder' ] = $newOrder; + + //echo $reqStr; exit(); + + $req = BDD::instance()->prepare( $reqStr ); + $req->execute( $data ); + } } ?> \ No newline at end of file