From cb8db98aac786176d8095c8e7839269912b48f42 Mon Sep 17 00:00:00 2001 From: tomivt Date: Wed, 30 Oct 2024 22:50:55 +0100 Subject: [PATCH] Add Favorite Gateway & Model --- .DS_Store | Bin 6148 -> 6148 bytes ...{favoriteEntity.php => FavoriteEntity.php} | 1 - src/Gateway/FavoriteGateway.php | 79 ++++++++++++++++++ src/Model/FavoriteModel.php | 71 ++++++++++++++++ 4 files changed, 150 insertions(+), 1 deletion(-) rename src/Entity/{favoriteEntity.php => FavoriteEntity.php} (99%) create mode 100644 src/Gateway/FavoriteGateway.php create mode 100644 src/Model/FavoriteModel.php diff --git a/.DS_Store b/.DS_Store index 1163199908eb5715d3278b9d7370dab36c09a1c1..880e6e1331ba124f01d737bb382819d8e285da37 100644 GIT binary patch delta 82 zcmZoMXfc=|#>B`mu~2NHo+2aj#DLw5%#(Ro^e4Bn8clx8sB!ku~2NHo+2aX#DLw47ceq2a!%%9)UW4a$YjW4NMT52s9?}z$Y3a8 z$Ysd!%*jtq%E?b+U|UN)j{kQj5SEGE-84N@Bt@^HTE5o$^cbQi{QPgCP0%B;S_>7v<&T z=cNNh87EsXn#!^<6f-0<6amAufT08}Y<^CTV>DFm;-XYGf(Chu@nHh3+yx?!31*C<_M8B%m7U^eUJbE 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