diff --git a/.DS_Store b/.DS_Store index 1163199..880e6e1 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/src/Entity/favoriteEntity.php b/src/Entity/FavoriteEntity.php similarity index 99% rename from src/Entity/favoriteEntity.php rename to src/Entity/FavoriteEntity.php index 14dae95..cfefc2e 100644 --- a/src/Entity/favoriteEntity.php +++ b/src/Entity/FavoriteEntity.php @@ -1,7 +1,6 @@ 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) + ]); + } + +} \ No newline at end of file diff --git a/src/Model/FavoriteModel.php b/src/Model/FavoriteModel.php new file mode 100644 index 0000000..f9c2214 --- /dev/null +++ b/src/Model/FavoriteModel.php @@ -0,0 +1,71 @@ + 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); + } +} \ No newline at end of file