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/
--
-- 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
@ -23,66 +23,37 @@ 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` (
CREATE TABLE `users`
(
`idDafl` varchar(25) NOT NULL,
`idSpotify` 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`
--
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;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT = @OLD_CHARACTER_SET_CLIENT */;

@ -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
$config=[
'determineRouteBeforeAppMiddleware' => 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';

@ -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
use \Psr\Http\Message\ServerRequestInterface as Request;
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) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
@ -10,22 +14,23 @@ $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);
$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);
$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);
@ -33,6 +38,7 @@ $app->put('/users/{id}', function (Request $request, Response $response, array $
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);
@ -40,6 +46,7 @@ $app->delete('/users/{id}', function (Request $request, Response $response, arra
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);
@ -47,6 +54,7 @@ $app->post('/user/{id}/like', function (Request $request, Response $response, ar
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);

@ -1,7 +1,8 @@
{
"require": {
"slim/slim": "3.*",
"illuminate/database": "~5.1"
"illuminate/database": "~5.1",
"ext-json": "*"
},
"config": {
"allow-plugins": {

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

Loading…
Cancel
Save