Modification de l'API pour ajout d'un user et récupération d'informations sur un user
continuous-integration/drone/push Build is passing Details

database-api-implementation
Félix MIELCAREK 2 years ago
parent 437d16b25d
commit f1b7bb2df5

@ -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

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/symfony/translation" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/nesbot/carbon" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/pimple/pimple" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/psr/simple-cache" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/psr/http-message" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/illuminate/support" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/psr/container" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/doctrine/inflector" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/illuminate/contracts" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/slim/slim" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/illuminate/database" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/illuminate/container" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/nikic/fast-route" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/composer" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/symfony/polyfill-php80" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/kylekatarnls/update-helper" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/symfony/translation-contracts" />
<excludeFolder url="file://$MODULE_DIR$/sources/vendor/symfony/polyfill-mbstring" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/api_database.iml" filepath="$PROJECT_DIR$/.idea/api_database.iml" />
</modules>
</component>
</project>

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpIncludePathManager">
<include_path>
<path value="$PROJECT_DIR$/sources/vendor/symfony/translation" />
<path value="$PROJECT_DIR$/sources/vendor/nesbot/carbon" />
<path value="$PROJECT_DIR$/sources/vendor/pimple/pimple" />
<path value="$PROJECT_DIR$/sources/vendor/psr/simple-cache" />
<path value="$PROJECT_DIR$/sources/vendor/psr/http-message" />
<path value="$PROJECT_DIR$/sources/vendor/illuminate/support" />
<path value="$PROJECT_DIR$/sources/vendor/psr/container" />
<path value="$PROJECT_DIR$/sources/vendor/doctrine/inflector" />
<path value="$PROJECT_DIR$/sources/vendor/illuminate/contracts" />
<path value="$PROJECT_DIR$/sources/vendor/slim/slim" />
<path value="$PROJECT_DIR$/sources/vendor/illuminate/database" />
<path value="$PROJECT_DIR$/sources/vendor/illuminate/container" />
<path value="$PROJECT_DIR$/sources/vendor/nikic/fast-route" />
<path value="$PROJECT_DIR$/sources/vendor/composer" />
<path value="$PROJECT_DIR$/sources/vendor/symfony/polyfill-php80" />
<path value="$PROJECT_DIR$/sources/vendor/kylekatarnls/update-helper" />
<path value="$PROJECT_DIR$/sources/vendor/symfony/translation-contracts" />
<path value="$PROJECT_DIR$/sources/vendor/symfony/polyfill-mbstring" />
</include_path>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="7.4">
<option name="suggestChangeDefaultLanguageLevel" value="false" />
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/database/dafl_music.sql" dialect="MariaDB" />
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
</component>
</project>

@ -3,7 +3,7 @@
-- https://www.phpmyadmin.net/ -- https://www.phpmyadmin.net/
-- --
-- Host: localhost -- 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 -- Server version: 10.9.4-MariaDB
-- PHP Version: 8.1.13 -- PHP Version: 8.1.13
@ -12,9 +12,9 @@ START TRANSACTION;
SET time_zone = "+00:00"; SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_CLIENT = @@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS = @@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET @OLD_COLLATION_CONNECTION = @@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */; /*!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` -- Table structure for table `users`
-- --
CREATE TABLE `users` ( CREATE TABLE `users`
`idDafl` varchar(25) NOT NULL, (
`idSpotify` varchar(25) NOT NULL `idDafl` varchar(25) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; `idSpotify` varchar(25) NOT NULL,
`password` varchar(250) NOT NULL
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
-- --
-- Dumping data for table `users` -- Dumping data for table `users`
-- --
INSERT INTO `users` (`idDafl`, `idSpotify`) VALUES INSERT INTO `users` (`idDafl`, `idSpotify`, `password`)
('felix', 'idspotfelix'); VALUES ('felix', 'idspotfelix', 'mdp'),
('lucas', 'spottest', 'pwdtest');
-- --
-- Indexes for dumped tables -- Indexes for dumped tables
-- --
--
-- Indexes for table `passwords`
--
ALTER TABLE `passwords`
ADD UNIQUE KEY `userId` (`userId`);
-- --
-- Indexes for table `users` -- Indexes for table `users`
-- --
ALTER TABLE `users` ALTER TABLE `users`
ADD PRIMARY KEY (`idDafl`), ADD PRIMARY KEY (`idDafl`),
ADD UNIQUE KEY `idSpotify` (`idSpotify`); 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;
COMMIT; COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_CLIENT = @OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET CHARACTER_SET_RESULTS = @OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40101 SET COLLATION_CONNECTION = @OLD_COLLATION_CONNECTION */;

@ -0,0 +1,36 @@
<?php
class Connection extends PDO
{
private $stmt;
public function __construct(string $dsn, string $username, string $password)
{
parent::__construct($dsn, $username, $password);
$this->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();
}
}
?>

@ -0,0 +1,32 @@
<?php
require "gateways/UserGateway.php";
require "business/User.php";
class Model
{
public function getInformationsUser($id): array
{
global $app;
$db = $app->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']);
}
}

@ -1,6 +0,0 @@
<?php
use Illuminate\Database\Eloquent\Model as Model;
class User extends Model {
protected $table = 'users';
}

@ -0,0 +1,35 @@
<?php
class User
{
private string $idDafl;
private string $idSpotify;
public function __construct(string $idDafl, string $idSpotify)
{
$this->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);
}
}

@ -1,16 +1,8 @@
<?php <?php
$config=[ $config['displayErrorDetails'] = true;
'determineRouteBeforeAppMiddleware' => false, $config['addContentLengthHeader'] = false;
'displayErrorDetails' => true,
'db' => [ $config['db']['dsn'] = 'mysql:host=localhost;dbname=dafl_music';
'driver' => 'mysql', $config['db']['user'] = 'root';
'host' => 'localhost', $config['db']['pass'] = 'root';
'database' => 'dafl_music',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
]
];

@ -1,12 +0,0 @@
<?php
$container = $app->getContainer();
$container['db'] = function ($container) {
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($container['settings']['db']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
return $capsule;
};

@ -0,0 +1,24 @@
<?php
class UserGateway
{
private Connection $con;
public function __construct(Connection $con)
{
$this->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)));
}
}

@ -1,8 +1,12 @@
<?php <?php
use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response; use \Psr\Http\Message\ResponseInterface as Response;
require 'User.php';
require "Model.php";
require "Connection.php";
// Test route
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) { $app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name']; $name = $args['name'];
$response->getBody()->write("Hello, $name"); $response->getBody()->write("Hello, $name");
@ -10,46 +14,50 @@ $app->get('/hello/{name}', function (Request $request, Response $response, array
return $response; return $response;
}); });
// Get information of a user
$app->get('/users/{id}', function (Request $request, Response $response, array $args) { $app->get('/users/{id}', function (Request $request, Response $response, array $args) {
global $container; $mdl = new Model();
$response->getBody()->write(json_encode($mdl->getInformationsUser($args['id'])));
$user = $container->db->table('users')->where('idDafl',$args['id'])->get(); return $response;
$res=json_encode($user);
$response->getBody()->write($res);
return $response;
}); });
// Add a user
$app->post('/users/{id}', function (Request $request, Response $response, array $args) { $app->post('/users/{id}', function (Request $request, Response $response, array $args) {
$res="Add user ".$args['id']; $mdl = new Model();
$response->getBody()->write($res); $data = $request->getParsedBody();
$mdl->addUser($args['id'], $data['idSpotify'], $data['passw']);
return $response; return $response;
}); });
// Update information of a user
$app->put('/users/{id}', function (Request $request, Response $response, array $args) { $app->put('/users/{id}', function (Request $request, Response $response, array $args) {
$res="Update infos of user ".$args['id']; $res = "Update infos of user " . $args['id'];
$response->getBody()->write($res); $response->getBody()->write($res);
return $response; return $response;
}); });
// Delete a user
$app->delete('/users/{id}', function (Request $request, Response $response, array $args) { $app->delete('/users/{id}', function (Request $request, Response $response, array $args) {
$res="Delete user ".$args['id']; $res = "Delete user " . $args['id'];
$response->getBody()->write($res); $response->getBody()->write($res);
return $response; return $response;
}); });
// Like someone
$app->post('/user/{id}/like', function (Request $request, Response $response, array $args) { $app->post('/user/{id}/like', function (Request $request, Response $response, array $args) {
$res="User ".$args['id']." liked ".$args['liked']; $res = "User " . $args['id'] . " liked " . $args['liked'];
$response->getBody()->write($res); $response->getBody()->write($res);
return $response; return $response;
}); });
// Add a new song as a preference for a situation
$app->post('/users/{id}/preferences', function (Request $request, Response $response, array $args) { $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']; $res = "User " . $args['id'] . " add music " . $args['music'] . " to his preferences for category " . $args['categ'];
$response->getBody()->write($res); $response->getBody()->write($res);
return $response; return $response;
}); });

@ -1,11 +1,12 @@
{ {
"require": { "require": {
"slim/slim": "3.*", "slim/slim": "3.*",
"illuminate/database": "~5.1" "illuminate/database": "~5.1",
}, "ext-json": "*"
"config": { },
"allow-plugins": { "config": {
"kylekatarnls/update-helper": true "allow-plugins": {
} "kylekatarnls/update-helper": true
} }
}
} }

@ -1,11 +1,12 @@
<?php <?php
// Files auto-loading
require '../vendor/autoload.php'; require '../vendor/autoload.php';
// Configuration
require '../app/config.php'; require '../app/config.php';
// App instantiation // App instantiation
$app = new \Slim\App(['settings' => $config]); $app = new \Slim\App(['settings' => $config]);
require '../app/dependencies.php';
// Routes // Routes
require '../app/routes.php'; require '../app/routes.php';

Loading…
Cancel
Save