From 00f80b9094443a036b85ca5da62d189aae9c16a2 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Fri, 20 Jan 2023 11:08:46 +0100 Subject: [PATCH 1/8] refactor getFullRecipeWithComments to getFullRecipeWithApprs --- Controllers/RecipeController.php | 2 +- Models/RecipeModel.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php index a453f75..00118e7 100644 --- a/Controllers/RecipeController.php +++ b/Controllers/RecipeController.php @@ -10,7 +10,7 @@ final class RecipeController } $O_recipeModel = new RecipeModel(); - $A_returnArray = $O_recipeModel->getFullRecipeWithComments($A_urlParams[0]); + $A_returnArray = $O_recipeModel->getFullRecipeWithApprs($A_urlParams[0]); if ($A_returnArray === null) { return View::show("errors/404"); } diff --git a/Models/RecipeModel.php b/Models/RecipeModel.php index 978fa70..0a2010b 100644 --- a/Models/RecipeModel.php +++ b/Models/RecipeModel.php @@ -15,7 +15,7 @@ final class RecipeModel return $row; } - public function getFullRecipeWithComments($I_id) + public function getFullRecipeWithApprs($I_id) { $A_recipe = self::getRecipeByID($I_id); if ($A_recipe === null)return null; From abb259bbd12e69b88908533089e88da9d5a782f4 Mon Sep 17 00:00:00 2001 From: SIMAILA Djalim Date: Sat, 21 Jan 2023 18:16:45 +0100 Subject: [PATCH 2/8] appreciation model --- Models/AppreciationModel.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Models/AppreciationModel.php diff --git a/Models/AppreciationModel.php b/Models/AppreciationModel.php new file mode 100644 index 0000000..bfa73cb --- /dev/null +++ b/Models/AppreciationModel.php @@ -0,0 +1,32 @@ +prepare("SELECT * FROM APPPRECIATION WHERE ID = :recipe_id"); + $stmt->bindParam("recipe_id",$I_recipe_id); + $stmt->execute(); + return $stmt->fetch(); + } + public function createAppreciation($I_recipe_id,$I_user_id,$S_Comment,$I_score){ + $O_model = Model::get(); + $stmt = $O_model->prepare("INSERT INTO APPPRECIATION(COMMENT,SCORE,DATE,AUTHOR_ID,RECIPE_ID) VALUES(:comment,:score,:date,:author_id,:recipe_id)"); + $stmt->bindParam("comment",$S_Comment); + $stmt->bindParam("score",$I_score); + $_date = date("Y-m-d"); + $stmt->bindParam("date",$_date); + $stmt->bindParam("author_id",$I_user_id); + $stmt->bindParam("recipe_id",$I_recipe_id); + $stmt->execute(); + return $stmt->fetch(); + } + public function deleteAppreciation($I_recipe_id){ + $O_model = Model::get(); + $stmt = $O_model->prepare("DELETE FROM APPPRECIATION WHERE ID = :recipe_id"); + $stmt->bindParam("recipe_id",$I_recipe_id); + $stmt->execute(); + } + public function updateAppreciation($I_recipe_id){ + + } +} From e6f735c3aff330f79f43668279026d1acc6955e3 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 22 Jan 2023 20:57:22 +0100 Subject: [PATCH 3/8] update ApprModel --- .../{AppreciationModel.php => ApprModel.php} | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) rename Models/{AppreciationModel.php => ApprModel.php} (58%) diff --git a/Models/AppreciationModel.php b/Models/ApprModel.php similarity index 58% rename from Models/AppreciationModel.php rename to Models/ApprModel.php index bfa73cb..e054c36 100644 --- a/Models/AppreciationModel.php +++ b/Models/ApprModel.php @@ -1,16 +1,21 @@ prepare("SELECT * FROM APPPRECIATION WHERE ID = :recipe_id"); $stmt->bindParam("recipe_id",$I_recipe_id); $stmt->execute(); return $stmt->fetch(); } - public function createAppreciation($I_recipe_id,$I_user_id,$S_Comment,$I_score){ + + public function createAppr($I_recipe_id, $I_user_id, $S_Comment, $I_score) + { $O_model = Model::get(); - $stmt = $O_model->prepare("INSERT INTO APPPRECIATION(COMMENT,SCORE,DATE,AUTHOR_ID,RECIPE_ID) VALUES(:comment,:score,:date,:author_id,:recipe_id)"); + $stmt = $O_model->prepare(" + INSERT INTO APPPRECIATION (COMMENT,SCORE,DATE,AUTHOR_ID,RECIPE_ID) VALUES (:comment, :score, :date, :author_id, :recipe_id) + "); $stmt->bindParam("comment",$S_Comment); $stmt->bindParam("score",$I_score); $_date = date("Y-m-d"); @@ -20,13 +25,17 @@ final class AppreciationsModel { $stmt->execute(); return $stmt->fetch(); } - public function deleteAppreciation($I_recipe_id){ + + public function deleteAppr($I_appr_id) + { $O_model = Model::get(); - $stmt = $O_model->prepare("DELETE FROM APPPRECIATION WHERE ID = :recipe_id"); - $stmt->bindParam("recipe_id",$I_recipe_id); + $stmt = $O_model->prepare("DELETE FROM APPPRECIATION WHERE ID = :appr_id"); + $stmt->bindParam("appr_id", $I_appr_id); $stmt->execute(); } - public function updateAppreciation($I_recipe_id){ + + public function updateAppreciation($I_appr_id) + { } } From 90fd668546138c527255a7a4600ce46745779d17 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 22 Jan 2023 21:00:38 +0100 Subject: [PATCH 4/8] add ApprModel::getApprByID --- Models/ApprModel.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Models/ApprModel.php b/Models/ApprModel.php index e054c36..8c46c27 100644 --- a/Models/ApprModel.php +++ b/Models/ApprModel.php @@ -34,6 +34,18 @@ final class ApprModel { $stmt->execute(); } + public function getApprByID($I_appr_id) + { + $O_model = Model::get(); + $stmt = $O_model->prepare("SELECT * FROM APPPRECIATION WHERE ID = :appr_id"); + $stmt->bindParam("appr_id", $I_appr_id); + $stmt->execute(); + + $row = $stmt->fetch(); + if ($row === false) return null; + return $row; + } + public function updateAppreciation($I_appr_id) { From 24b2c1622cc726afef4c15bfc942864fedb87f49 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 22 Jan 2023 21:02:50 +0100 Subject: [PATCH 5/8] switch arguments order --- Models/ApprModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Models/ApprModel.php b/Models/ApprModel.php index 8c46c27..708c044 100644 --- a/Models/ApprModel.php +++ b/Models/ApprModel.php @@ -10,7 +10,7 @@ final class ApprModel { return $stmt->fetch(); } - public function createAppr($I_recipe_id, $I_user_id, $S_Comment, $I_score) + public function createAppr($I_user_id, $I_recipe_id, $S_Comment, $I_score) { $O_model = Model::get(); $stmt = $O_model->prepare(" From ba9bb6bac41045a78a704c3844c7402d1d5b1a49 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 22 Jan 2023 21:13:10 +0100 Subject: [PATCH 6/8] fix table name --- Models/ApprModel.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Models/ApprModel.php b/Models/ApprModel.php index 708c044..d1bc917 100644 --- a/Models/ApprModel.php +++ b/Models/ApprModel.php @@ -4,7 +4,7 @@ final class ApprModel { public function getRecipeApprs($I_recipe_id) { $O_model = Model::get(); - $stmt = $O_model->prepare("SELECT * FROM APPPRECIATION WHERE ID = :recipe_id"); + $stmt = $O_model->prepare("SELECT * FROM APPRECIATION WHERE ID = :recipe_id"); $stmt->bindParam("recipe_id",$I_recipe_id); $stmt->execute(); return $stmt->fetch(); @@ -14,7 +14,7 @@ final class ApprModel { { $O_model = Model::get(); $stmt = $O_model->prepare(" - INSERT INTO APPPRECIATION (COMMENT,SCORE,DATE,AUTHOR_ID,RECIPE_ID) VALUES (:comment, :score, :date, :author_id, :recipe_id) + INSERT INTO APPRECIATION (COMMENT,SCORE,DATE,AUTHOR_ID,RECIPE_ID) VALUES (:comment, :score, :date, :author_id, :recipe_id) "); $stmt->bindParam("comment",$S_Comment); $stmt->bindParam("score",$I_score); @@ -29,7 +29,7 @@ final class ApprModel { public function deleteAppr($I_appr_id) { $O_model = Model::get(); - $stmt = $O_model->prepare("DELETE FROM APPPRECIATION WHERE ID = :appr_id"); + $stmt = $O_model->prepare("DELETE FROM APPRECIATION WHERE ID = :appr_id"); $stmt->bindParam("appr_id", $I_appr_id); $stmt->execute(); } @@ -37,7 +37,7 @@ final class ApprModel { public function getApprByID($I_appr_id) { $O_model = Model::get(); - $stmt = $O_model->prepare("SELECT * FROM APPPRECIATION WHERE ID = :appr_id"); + $stmt = $O_model->prepare("SELECT * FROM APPRECIATION WHERE ID = :appr_id"); $stmt->bindParam("appr_id", $I_appr_id); $stmt->execute(); From ee18ee7619d45defbbbedc7b10e65d09ff2d7e5c Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 22 Jan 2023 21:18:13 +0100 Subject: [PATCH 7/8] add AprController with create and delete action (for admins) --- Controllers/ApprController.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Controllers/ApprController.php diff --git a/Controllers/ApprController.php b/Controllers/ApprController.php new file mode 100644 index 0000000..94ca408 --- /dev/null +++ b/Controllers/ApprController.php @@ -0,0 +1,34 @@ +createAppr($_SESSION["ID"], $I_recipe_id, $S_comment, $I_score); + + } + + public function deleteAction(Array $A_urlParams = null, Array $A_postParams = null) + { + Session::admin_or_die(); + + $I_appr_id = $A_urlParams[0]; + + $O_apprModel = new ApprModel(); + + $O_apprModel->deleteAppr($I_appr_id); + + echo "Appreciation $I_appr_id supprimée avec succès"; + } + + +} \ No newline at end of file From 98ac177ed098987e58e5009884407de899fd2964 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 22 Jan 2023 21:32:27 +0100 Subject: [PATCH 8/8] add possibility for users to delete their own appreciations --- Controllers/ApprController.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Controllers/ApprController.php b/Controllers/ApprController.php index 94ca408..ffc1453 100644 --- a/Controllers/ApprController.php +++ b/Controllers/ApprController.php @@ -19,11 +19,21 @@ final class ApprController public function deleteAction(Array $A_urlParams = null, Array $A_postParams = null) { - Session::admin_or_die(); + Session::login_or_die(); $I_appr_id = $A_urlParams[0]; $O_apprModel = new ApprModel(); + $A_appr = $O_apprModel->getApprById($I_appr_id); + + if ($A_appr === null) { + echo "404"; + return; + } + + if ($A_appr["AUTHOR_ID"] !== $_SESSION["ID"]) { + Session::admin_or_die(); + } $O_apprModel->deleteAppr($I_appr_id);