From f1b7bb2df53ce8e7aff4093ea1d59cd3518c60f6 Mon Sep 17 00:00:00 2001 From: felixmielcarek Date: Sun, 25 Dec 2022 22:11:39 +0100 Subject: [PATCH] =?UTF-8?q?Modification=20de=20l'API=20pour=20ajout=20d'un?= =?UTF-8?q?=20user=20et=20r=C3=A9cup=C3=A9ration=20d'informations=20sur=20?= =?UTF-8?q?un=20user?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/api_database/.idea/.gitignore | 8 +++ Sources/api_database/.idea/api_database.iml | 29 ++++++++ Sources/api_database/.idea/modules.xml | 8 +++ Sources/api_database/.idea/php.xml | 28 ++++++++ Sources/api_database/.idea/sqldialects.xml | 6 ++ Sources/api_database/.idea/vcs.xml | 6 ++ Sources/api_database/database/dafl_music.sql | 69 ++++++------------- .../api_database/sources/app/Connection.php | 36 ++++++++++ Sources/api_database/sources/app/Model.php | 32 +++++++++ Sources/api_database/sources/app/User.php | 6 -- .../sources/app/business/User.php | 35 ++++++++++ Sources/api_database/sources/app/config.php | 20 ++---- .../api_database/sources/app/dependencies.php | 12 ---- .../sources/app/gateways/UserGateway.php | 24 +++++++ Sources/api_database/sources/app/routes.php | 56 ++++++++------- Sources/api_database/sources/composer.json | 17 ++--- Sources/api_database/sources/public/index.php | 5 +- 17 files changed, 282 insertions(+), 115 deletions(-) create mode 100644 Sources/api_database/.idea/.gitignore create mode 100644 Sources/api_database/.idea/api_database.iml create mode 100644 Sources/api_database/.idea/modules.xml create mode 100644 Sources/api_database/.idea/php.xml create mode 100644 Sources/api_database/.idea/sqldialects.xml create mode 100644 Sources/api_database/.idea/vcs.xml create mode 100644 Sources/api_database/sources/app/Connection.php create mode 100644 Sources/api_database/sources/app/Model.php delete mode 100644 Sources/api_database/sources/app/User.php create mode 100644 Sources/api_database/sources/app/business/User.php delete mode 100644 Sources/api_database/sources/app/dependencies.php create mode 100644 Sources/api_database/sources/app/gateways/UserGateway.php diff --git a/Sources/api_database/.idea/.gitignore b/Sources/api_database/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/Sources/api_database/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Sources/api_database/.idea/api_database.iml b/Sources/api_database/.idea/api_database.iml new file mode 100644 index 0000000..bce5ede --- /dev/null +++ b/Sources/api_database/.idea/api_database.iml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Sources/api_database/.idea/modules.xml b/Sources/api_database/.idea/modules.xml new file mode 100644 index 0000000..a56b752 --- /dev/null +++ b/Sources/api_database/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Sources/api_database/.idea/php.xml b/Sources/api_database/.idea/php.xml new file mode 100644 index 0000000..53ef6a6 --- /dev/null +++ b/Sources/api_database/.idea/php.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Sources/api_database/.idea/sqldialects.xml b/Sources/api_database/.idea/sqldialects.xml new file mode 100644 index 0000000..5682560 --- /dev/null +++ b/Sources/api_database/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Sources/api_database/.idea/vcs.xml b/Sources/api_database/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/Sources/api_database/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Sources/api_database/database/dafl_music.sql b/Sources/api_database/database/dafl_music.sql index 726cf50..870fcbc 100644 --- a/Sources/api_database/database/dafl_music.sql +++ b/Sources/api_database/database/dafl_music.sql @@ -3,7 +3,7 @@ -- https://www.phpmyadmin.net/ -- -- Host: localhost --- Generation Time: Dec 21, 2022 at 04:45 PM +-- Generation Time: Dec 25, 2022 at 09:09 PM -- Server version: 10.9.4-MariaDB -- PHP Version: 8.1.13 @@ -12,9 +12,9 @@ START TRANSACTION; SET time_zone = "+00:00"; -/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; -/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; -/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET @OLD_CHARACTER_SET_CLIENT = @@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS = @@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION = @@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- @@ -23,68 +23,39 @@ SET time_zone = "+00:00"; -- -------------------------------------------------------- --- --- Table structure for table `passwords` --- - -CREATE TABLE `passwords` ( - `password` varchar(250) NOT NULL, - `userId` varchar(25) NOT NULL -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; - --- --- Dumping data for table `passwords` --- - -INSERT INTO `passwords` (`password`, `userId`) VALUES -('mdp', 'felix'); - --- -------------------------------------------------------- - -- -- Table structure for table `users` -- -CREATE TABLE `users` ( - `idDafl` varchar(25) NOT NULL, - `idSpotify` varchar(25) NOT NULL -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +CREATE TABLE `users` +( + `idDafl` varchar(25) NOT NULL, + `idSpotify` varchar(25) NOT NULL, + `password` varchar(250) NOT NULL +) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 + COLLATE = utf8mb4_unicode_ci; -- -- Dumping data for table `users` -- -INSERT INTO `users` (`idDafl`, `idSpotify`) VALUES -('felix', 'idspotfelix'); +INSERT INTO `users` (`idDafl`, `idSpotify`, `password`) +VALUES ('felix', 'idspotfelix', 'mdp'), + ('lucas', 'spottest', 'pwdtest'); -- -- Indexes for dumped tables -- --- --- Indexes for table `passwords` --- -ALTER TABLE `passwords` - ADD UNIQUE KEY `userId` (`userId`); - -- -- Indexes for table `users` -- ALTER TABLE `users` - ADD PRIMARY KEY (`idDafl`), - ADD UNIQUE KEY `idSpotify` (`idSpotify`); - --- --- Constraints for dumped tables --- - --- --- Constraints for table `passwords` --- -ALTER TABLE `passwords` - ADD CONSTRAINT `passwords_ibfk_1` FOREIGN KEY (`userId`) REFERENCES `users` (`idDafl`) ON DELETE CASCADE; + ADD PRIMARY KEY (`idDafl`), + ADD UNIQUE KEY `idSpotify` (`idSpotify`); COMMIT; -/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; -/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; -/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40101 SET CHARACTER_SET_CLIENT = @OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS = @OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION = @OLD_COLLATION_CONNECTION */; diff --git a/Sources/api_database/sources/app/Connection.php b/Sources/api_database/sources/app/Connection.php new file mode 100644 index 0000000..ce10fd7 --- /dev/null +++ b/Sources/api_database/sources/app/Connection.php @@ -0,0 +1,36 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } + + + /** * @param string $query + * @param array $parameters * + * @return bool Returns `true` on success, `false` otherwise + */ + public function executeQuery(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->stmt->execute(); + } + + public function getResults(): array + { + return $this->stmt->fetchall(); + } +} + +?> \ No newline at end of file diff --git a/Sources/api_database/sources/app/Model.php b/Sources/api_database/sources/app/Model.php new file mode 100644 index 0000000..1e5c569 --- /dev/null +++ b/Sources/api_database/sources/app/Model.php @@ -0,0 +1,32 @@ +getContainer()['settings']['db']; + $gw = new UserGateway(new Connection($db['dsn'], $db['user'], $db['pass'])); + $id = filter_var($id, FILTER_SANITIZE_STRING); + $userDb = $gw->getInformations($id); + $user = new User($userDb[0][0], $userDb[0][1]); + return $user->getInformations(); + } + + public function addUser($idDafl, $idSpotify, $passw): void + { + global $app; + $db = $app->getContainer()['settings']['db']; + + $data = []; + $data['idDafl'] = filter_var($idDafl, FILTER_SANITIZE_STRING); + $data['idSpotify'] = filter_var($idSpotify, FILTER_SANITIZE_STRING); + $data['passw'] = filter_var($passw, FILTER_SANITIZE_STRING); + + $gw = new UserGateway(new Connection($db['dsn'], $db['user'], $db['pass'])); + $gw->addUser($data['idDafl'], $data['idSpotify'], $data['passw']); + } +} \ No newline at end of file diff --git a/Sources/api_database/sources/app/User.php b/Sources/api_database/sources/app/User.php deleted file mode 100644 index fcf128e..0000000 --- a/Sources/api_database/sources/app/User.php +++ /dev/null @@ -1,6 +0,0 @@ -idDafl = $idDafl; + $this->idSpotify = $idSpotify; + } + + /** + * @return string + */ + public function getIdDafl(): string + { + return $this->idDafl; + } + + /** + * @return string + */ + public function getIdSpotify(): string + { + return $this->idSpotify; + } + + public function getInformations(): array + { + return array($this->idDafl, $this->idSpotify); + } + +} diff --git a/Sources/api_database/sources/app/config.php b/Sources/api_database/sources/app/config.php index c0a798f..f025fc7 100644 --- a/Sources/api_database/sources/app/config.php +++ b/Sources/api_database/sources/app/config.php @@ -1,16 +1,8 @@ false, - 'displayErrorDetails' => true, - 'db' => [ - 'driver' => 'mysql', - 'host' => 'localhost', - 'database' => 'dafl_music', - 'username' => 'root', - 'password' => 'root', - 'charset' => 'utf8', - 'collation' => 'utf8_unicode_ci', - 'prefix' => '' - ] -]; +$config['displayErrorDetails'] = true; +$config['addContentLengthHeader'] = false; + +$config['db']['dsn'] = 'mysql:host=localhost;dbname=dafl_music'; +$config['db']['user'] = 'root'; +$config['db']['pass'] = 'root'; diff --git a/Sources/api_database/sources/app/dependencies.php b/Sources/api_database/sources/app/dependencies.php deleted file mode 100644 index 24ee3c0..0000000 --- a/Sources/api_database/sources/app/dependencies.php +++ /dev/null @@ -1,12 +0,0 @@ -getContainer(); - -$container['db'] = function ($container) { - $capsule = new \Illuminate\Database\Capsule\Manager; - $capsule->addConnection($container['settings']['db']); - - $capsule->setAsGlobal(); - $capsule->bootEloquent(); - - return $capsule; -}; diff --git a/Sources/api_database/sources/app/gateways/UserGateway.php b/Sources/api_database/sources/app/gateways/UserGateway.php new file mode 100644 index 0000000..2ecd10d --- /dev/null +++ b/Sources/api_database/sources/app/gateways/UserGateway.php @@ -0,0 +1,24 @@ +con = $con; + } + + public function getInformations($id): array + { + $query = 'SELECT idDafl,idSpotify FROM users WHERE idDafl=:id'; + $this->con->executeQuery($query, array(':id' => array($id, PDO::PARAM_STR))); + return $this->con->getResults(); + } + + public function addUser($idDafl, $idSpotify, $passw) + { + $query = 'INSERT INTO users VALUES (:idDafl,:idSpotify,:passw)'; + $this->con->executeQuery($query, array(':idDafl' => array($idDafl, PDO::PARAM_STR), ':idSpotify' => array($idSpotify, PDO::PARAM_STR), ':passw' => array($passw, PDO::PARAM_STR))); + } +} \ No newline at end of file diff --git a/Sources/api_database/sources/app/routes.php b/Sources/api_database/sources/app/routes.php index a6c792f..8d70347 100644 --- a/Sources/api_database/sources/app/routes.php +++ b/Sources/api_database/sources/app/routes.php @@ -1,8 +1,12 @@ get('/hello/{name}', function (Request $request, Response $response, array $args) { $name = $args['name']; $response->getBody()->write("Hello, $name"); @@ -10,46 +14,50 @@ $app->get('/hello/{name}', function (Request $request, Response $response, array return $response; }); +// Get information of a user $app->get('/users/{id}', function (Request $request, Response $response, array $args) { - global $container; - - $user = $container->db->table('users')->where('idDafl',$args['id'])->get(); - $res=json_encode($user); - $response->getBody()->write($res); - return $response; + $mdl = new Model(); + $response->getBody()->write(json_encode($mdl->getInformationsUser($args['id']))); + return $response; }); +// Add a user $app->post('/users/{id}', function (Request $request, Response $response, array $args) { - $res="Add user ".$args['id']; - $response->getBody()->write($res); - - return $response; + $mdl = new Model(); + $data = $request->getParsedBody(); + $mdl->addUser($args['id'], $data['idSpotify'], $data['passw']); + return $response; }); + +// Update information of a user $app->put('/users/{id}', function (Request $request, Response $response, array $args) { - $res="Update infos of user ".$args['id']; - $response->getBody()->write($res); + $res = "Update infos of user " . $args['id']; + $response->getBody()->write($res); - return $response; + return $response; }); +// Delete a user $app->delete('/users/{id}', function (Request $request, Response $response, array $args) { - $res="Delete user ".$args['id']; - $response->getBody()->write($res); + $res = "Delete user " . $args['id']; + $response->getBody()->write($res); - return $response; + return $response; }); +// Like someone $app->post('/user/{id}/like', function (Request $request, Response $response, array $args) { - $res="User ".$args['id']." liked ".$args['liked']; - $response->getBody()->write($res); - - return $response; + $res = "User " . $args['id'] . " liked " . $args['liked']; + $response->getBody()->write($res); + + return $response; }); +// Add a new song as a preference for a situation $app->post('/users/{id}/preferences', function (Request $request, Response $response, array $args) { - $res="User ".$args['id']." add music ".$args['music']." to his preferences for category ".$args['categ']; - $response->getBody()->write($res); + $res = "User " . $args['id'] . " add music " . $args['music'] . " to his preferences for category " . $args['categ']; + $response->getBody()->write($res); - return $response; + return $response; }); diff --git a/Sources/api_database/sources/composer.json b/Sources/api_database/sources/composer.json index 9d2a970..48e2c4f 100644 --- a/Sources/api_database/sources/composer.json +++ b/Sources/api_database/sources/composer.json @@ -1,11 +1,12 @@ { - "require": { - "slim/slim": "3.*", - "illuminate/database": "~5.1" - }, - "config": { - "allow-plugins": { - "kylekatarnls/update-helper": true - } + "require": { + "slim/slim": "3.*", + "illuminate/database": "~5.1", + "ext-json": "*" + }, + "config": { + "allow-plugins": { + "kylekatarnls/update-helper": true } + } } diff --git a/Sources/api_database/sources/public/index.php b/Sources/api_database/sources/public/index.php index 1980371..9dedd64 100644 --- a/Sources/api_database/sources/public/index.php +++ b/Sources/api_database/sources/public/index.php @@ -1,11 +1,12 @@ $config]); -require '../app/dependencies.php'; // Routes require '../app/routes.php';