Add Favorite Gateway & Model

pull/17/head
tomivt 6 months ago
parent 2788450c04
commit cb8db98aac

BIN
.DS_Store vendored

Binary file not shown.

@ -1,7 +1,6 @@
<?php
namespace Entity;
class FavoriteEntity
{
private int $id_user;

@ -0,0 +1,79 @@
<?php
namespace Gateway;
use Entity\FavoriteEntity;
use Gateway\Connection;
use PDO;
class FavoriteGateway
{
private Connection $co;
public function __construct(Connection $co)
{
$this -> co = $co;
}
public function createFavoriteGateway(int $idUser, int $idQuote) : bool
{
$query = "
INSERT INTO Favorite
VALUES (:user, :quote)
";
return $this -> co -> executeQuery($query, [
'user' => array($idUser, PDO::PARAM_INT),
'quote' => array($idQuote, PDO::PARAM_INT)
]);
}
public function findFavoriteById(int $idUser, int $idQuote) : array
{
$query = "
SELECT * FROM Favorite
WHERE user_f = :user AND quote_f = :quote
";
$this -> co -> executeQuery($query, [
'user' => array($idUser, PDO::PARAM_INT),
'quote' => array($idQuote, PDO::PARAM_INT)
]);
return $this -> co -> getResults();
}
public function findFavoriteByUser(int $idUser) : array
{
$query = "
SELECT * FROM Favorite
WHERE user_f = :user
";
$this -> co -> executeQuery($query, ['user' => array($idUser, PDO::PARAM_INT)]);
return $this -> co -> getResults();
}
public function findAllFavorite() : array
{
$query = "SELECT * FROM Favorite";
$this -> co -> executeQuery($query);
return $this -> co -> getResults();
}
public function deleteFavoriteGateway(int $idUser, int $idQuote) : bool
{
$query = "
DELETE FROM Favorite
WHERE user_f = :user AND quote_f = :quote
";
return $this -> co -> executeQuery($query, [
'user' => array($idUser, PDO::PARAM_INT),
'quote' => array($idQuote, PDO::PARAM_INT)
]);
}
}

@ -0,0 +1,71 @@
<?php
namespace Model;
use Gateway\FavoriteGateway;
use Entity\FavoriteEntity;
class FavoriteModel
{
private FavoriteGateway $gw;
public function __construct(FavoriteGateway $gw)
{
$this -> gw = $gw;
}
public function createFavoriteModel(int $idUser, int $idQuote) : bool
{
return $this -> gw -> createFavoriteGateway($idUser, $idQuote);
}
public function getFavoriteById(int $idUser, int $idQuote) : ?FavoriteEntity
{
$res = $this -> gw -> findFavoriteById($idUser, $idQuote);
if ($res)
{
return new FavoriteEntity (
$res[0]['user_f'],
$res[0]['quote_f'],
);
}
return null;
}
public function getFavoriteByUser(int $idUser) : array
{
$res = $this -> gw -> findFavoriteByUser($idUser);
$favorites = [];
foreach ($res as $favorite)
{
$favorites = new FavoriteEntity (
$favorite['user_f'],
$favorite['quote_f']
);
}
return $favorites;
}
public function getAllFavorite() : array
{
$res = $this -> gw -> findAllFavorite();
$favorites = [];
foreach ($res as $favorite)
{
$favorites = new FavoriteEntity (
$favorite['user_f'],
$favorite['quote_f']
);
}
return $favorites;
}
public function removeFavorite(int $idUser, int $idQuote) : bool
{
return $this -> gw -> deleteFavoriteGateway($idUser, $idQuote);
}
}
Loading…
Cancel
Save