Lundi
This commit is contained in:
13
src/Domain/Ingredients/Ingredient.php
Normal file
13
src/Domain/Ingredients/Ingredient.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Ingredients;
|
||||
|
||||
use App\Domain\Model;
|
||||
|
||||
class Ingredient extends Model {
|
||||
|
||||
public int $num_ingredient;
|
||||
public string $nom_ingredient;
|
||||
|
||||
|
||||
}
|
||||
29
src/Domain/Ingredients/IngredientRepository.php
Normal file
29
src/Domain/Ingredients/IngredientRepository.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Ingredients;
|
||||
|
||||
use App\Domain\Repository;
|
||||
use App\Domain\Model;
|
||||
|
||||
class IngredientRepository extends Repository {
|
||||
|
||||
public static function getEntity(): string
|
||||
{
|
||||
return Ingredient::class;
|
||||
}
|
||||
|
||||
public static function getStructure(): array
|
||||
{
|
||||
return [
|
||||
'table' => 'Ingredient',
|
||||
'columns' => [
|
||||
'num_ingredient', 'nom_ingredient'
|
||||
]
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
public function add( Model $ingredient ): bool {
|
||||
|
||||
}
|
||||
}
|
||||
8
src/Domain/Model.php
Normal file
8
src/Domain/Model.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain;
|
||||
|
||||
abstract class Model {
|
||||
|
||||
|
||||
}
|
||||
22
src/Domain/Recettes/Recette.php
Normal file
22
src/Domain/Recettes/Recette.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Recettes;
|
||||
|
||||
use App\Domain\Model;
|
||||
use App\Helpers\Markdown;
|
||||
|
||||
class Recette extends Model {
|
||||
|
||||
public int $num_recette;
|
||||
public string $titre_recette;
|
||||
public string $slug;
|
||||
public string $description_recette;
|
||||
public string $photo;
|
||||
public string $publication_date;
|
||||
public int $temps_de_preparation;
|
||||
|
||||
public function getHTMLDescription(): string {
|
||||
return Markdown::convertToHTML( $this->description_recette );
|
||||
}
|
||||
|
||||
}
|
||||
66
src/Domain/Recettes/RecetteRepository.php
Normal file
66
src/Domain/Recettes/RecetteRepository.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Recettes;
|
||||
|
||||
use App\Domain\Model;
|
||||
use App\Domain\Repository;
|
||||
|
||||
class RecetteRepository extends Repository {
|
||||
|
||||
public static function getEntity(): string
|
||||
{
|
||||
return Recette::class;
|
||||
}
|
||||
|
||||
public static function getStructure(): array
|
||||
{
|
||||
return [
|
||||
'table' => 'Recette',
|
||||
'columns' => [
|
||||
'num_recette', 'titre_recette', 'slug', 'description_recette', 'photo', 'publication_date', 'temps_de_preparation'
|
||||
]
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet d'avoir une recette par un ID.
|
||||
*
|
||||
* @param int $id
|
||||
* @return Recette|null
|
||||
*/
|
||||
public function getByID( int $id ): ?Recette {
|
||||
$sqlQuery = "SELECT * FROM {$this->tableName} WHERE num_recette = {$id}";
|
||||
$results = $this->selectGetAll($sqlQuery);
|
||||
if( $results === null || count( $results ) > 1 )
|
||||
return null;
|
||||
return $results[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet d'avoir une recette par un slug.
|
||||
*
|
||||
* @param string $slug
|
||||
* @return Recette|null
|
||||
*/
|
||||
public function getBySlug( string $slug ): ?Recette {
|
||||
$sqlQuery = "SELECT * FROM {$this->tableName} WHERE slug = {$slug}";
|
||||
$results = $this->selectGetAll($sqlQuery);
|
||||
if( $results === null || count( $results ) > 1 )
|
||||
return null;
|
||||
return $results[0];
|
||||
}
|
||||
|
||||
public function add( Model $recette ): bool {
|
||||
return $this->addEntity( $recette );
|
||||
}
|
||||
|
||||
public function update( Model $recette ): bool {
|
||||
return $this->updateEntity( $recette, 'num_recette' );
|
||||
}
|
||||
|
||||
public function delete( Model $recette ): bool {
|
||||
return $this->deleteEntity( $recette, 'num_recette' );
|
||||
}
|
||||
|
||||
}
|
||||
99
src/Domain/Repository.php
Normal file
99
src/Domain/Repository.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain;
|
||||
|
||||
use App\Domain\Recettes\Recette;
|
||||
use App\Kernel;
|
||||
use PDO;
|
||||
|
||||
abstract class Repository {
|
||||
|
||||
/**
|
||||
* Instancie des éléments de cette classe par rapport au repository.
|
||||
* @return class-string
|
||||
*/
|
||||
abstract public static function getEntity(): string;
|
||||
|
||||
/**
|
||||
* Doit retourner une liste du type :
|
||||
* ['table' => '', 'columns' => [ ...Liste des colonnes dans la BDD ] ].
|
||||
* @return array
|
||||
*/
|
||||
abstract public static function getStructure(): array;
|
||||
|
||||
final public string $tableName;
|
||||
final public array $tableColumns;
|
||||
|
||||
public function __construct(){
|
||||
$structure = static::getStructure();
|
||||
|
||||
$this->tableName = $structure['table'];
|
||||
$this->tableColumns = $structure['columns'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Permet d'avoir tous les éléments correspondant à la requête passée en paramètre.
|
||||
*
|
||||
* @param string $sqlQuery
|
||||
* @return array|null
|
||||
*/
|
||||
public function selectGetAll( string $sqlQuery ): ?array {
|
||||
$statement = Kernel::$DB->pdo->prepare( $sqlQuery );
|
||||
if( !$statement->execute() )
|
||||
return null;
|
||||
|
||||
$results = $statement->fetchAll( PDO::FETCH_CLASS, static::getEntity() );
|
||||
if( empty( $results ) )
|
||||
return null;
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function addEntity( Model $entity ): bool {
|
||||
|
||||
$query = "INSERT INTO {$this->tableName} (";
|
||||
$values = "(";
|
||||
foreach( $this->tableColumns as $column ) {
|
||||
$query .= "`{$column}`,";
|
||||
$values .= ":{$column},";
|
||||
}
|
||||
$query = substr( $query, 0, -1 ) . ")";
|
||||
$values = substr( $values, 0, -1 ) . ")";
|
||||
$query .= " VALUES " . $values . ";";
|
||||
|
||||
$statement = Kernel::$DB->pdo->prepare( $query );
|
||||
foreach( $this->tableColumns as $column ) {
|
||||
$statement->bindValue(":{$column}", $entity->{$column} );
|
||||
}
|
||||
|
||||
return $statement->execute();
|
||||
}
|
||||
|
||||
public function updateEntity( Model $entity, string $identifier ): bool {
|
||||
$query = "UPDATE {$this->tableName} SET ";
|
||||
foreach( $this->tableColumns as $column ) {
|
||||
$query .= "`{$column}` = :{$column},";
|
||||
}
|
||||
$query = substr( $query, 0, -1 ) . " WHERE {$identifier} = :{$identifier};";
|
||||
|
||||
$statement = Kernel::$DB->pdo->prepare( $query );
|
||||
foreach( $this->tableColumns as $column ) {
|
||||
$statement->bindValue(":{$column}", $entity->{$column} );
|
||||
}
|
||||
$statement->bindValue( ":{$identifier}", $entity->{$identifier} );
|
||||
return $statement->execute();
|
||||
}
|
||||
|
||||
public function deleteEntity( Model $entity, string $identifier ): bool {
|
||||
$query = "DELETE FROM {$this->tableName} WHERE {$identifier} = :{$identifier};";
|
||||
$statement = Kernel::$DB->pdo->prepare( $query );
|
||||
$statement->bindValue( ":{$identifier}", $entity->{$identifier} );
|
||||
return $statement->execute();
|
||||
}
|
||||
|
||||
abstract public function add( Model $entity ): bool;
|
||||
abstract public function update( Model $entity ): bool;
|
||||
abstract public function delete( Model $entity ): bool;
|
||||
|
||||
|
||||
}
|
||||
@@ -6,6 +6,7 @@ use App\Domain\Controller;
|
||||
use App\Helpers\Authentification;
|
||||
use App\Http\JSONResponse;
|
||||
use App\Http\Request;
|
||||
use App\Infrastructure\View;
|
||||
|
||||
class AuthentificationController extends Controller {
|
||||
|
||||
@@ -15,19 +16,29 @@ class AuthentificationController extends Controller {
|
||||
|
||||
// Public routes.
|
||||
self::Route( routeUrl: '/login', routeName: 'login', routeAction: 'loginForm', pageHeadTitle: 'Connexion' ),
|
||||
self::Route( routeUrl: '/logout', routeName: 'logout', routeAction: 'logoutPage', pageHeadTitle: 'Déconnexion' ),
|
||||
|
||||
// API Routes.
|
||||
self::Route( routeUrl: '/api/auth', routeName: 'api->auth', routeAction: 'auth', routeMethods: ['POST'] ),
|
||||
self::Route( routeUrl: '/api/auth/logout', routeName: 'api->auth->logout', routeAction: 'logout', routeMethods: ['POST'] ),
|
||||
// self::Route( routeUrl: '/api/auth/logout', routeName: 'api->auth->logout', routeAction: 'logout', routeMethods: ['POST'] ),
|
||||
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
public function login(): View {
|
||||
public function loginForm(): View {
|
||||
return new View( 'login' );
|
||||
}
|
||||
|
||||
public function logoutPage(){
|
||||
if( !Authentification::isLoggedIn() ) {
|
||||
Request::redirectTo( 'home' );
|
||||
}
|
||||
|
||||
Authentification::destroySession();
|
||||
Request::redirectTo( 'home' );
|
||||
}
|
||||
|
||||
public function auth(): JSONResponse {
|
||||
|
||||
Request::setCORS();
|
||||
@@ -39,14 +50,14 @@ class AuthentificationController extends Controller {
|
||||
|
||||
$userId = 1;
|
||||
Authentification::loginUser( $userId );
|
||||
JSONResponse::sendSuccess( [ 'user_id' => $userId ] );
|
||||
return JSONResponse::sendSuccess( [ 'user_id' => $userId ] );
|
||||
|
||||
}
|
||||
|
||||
public function logout(): JSONResponse {
|
||||
|
||||
if( !Authentification::isLoggedIn() ) {
|
||||
return JSONResponse::sendError( [ 'message' => 'Alrady disconnected' ] );
|
||||
return JSONResponse::sendError( [ 'message' => 'Already disconnected' ] );
|
||||
}
|
||||
|
||||
Authentification::destroySession();
|
||||
|
||||
Reference in New Issue
Block a user