Merge pull request #48 from ThomasRubini/appreciation

This commit is contained in:
Thomas Rubini 2023-01-22 21:33:09 +01:00 committed by GitHub
commit 39c3c67155
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 2 deletions

View File

@ -0,0 +1,44 @@
<?php
final class ApprController
{
public function createAction(Array $A_urlParams = null, Array $A_postParams = null)
{
Session::login_or_die();
$I_recipe_id = Utils::getOrDie($A_postParams, "recipe_id");
$S_comment = Utils::getOrDie($A_postParams, "comment");
$I_score = Utils::getOrDie($A_postParams, "score");
$O_apprModel = new ApprModel();
$O_apprModel->createAppr($_SESSION["ID"], $I_recipe_id, $S_comment, $I_score);
}
public function deleteAction(Array $A_urlParams = null, Array $A_postParams = null)
{
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);
echo "Appreciation $I_appr_id supprimée avec succès";
}
}

View File

@ -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");
}

53
Models/ApprModel.php Normal file
View File

@ -0,0 +1,53 @@
<?php
final class ApprModel {
public function getRecipeApprs($I_recipe_id)
{
$O_model = Model::get();
$stmt = $O_model->prepare("SELECT * FROM APPRECIATION WHERE ID = :recipe_id");
$stmt->bindParam("recipe_id",$I_recipe_id);
$stmt->execute();
return $stmt->fetch();
}
public function createAppr($I_user_id, $I_recipe_id, $S_Comment, $I_score)
{
$O_model = Model::get();
$stmt = $O_model->prepare("
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);
$_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 deleteAppr($I_appr_id)
{
$O_model = Model::get();
$stmt = $O_model->prepare("DELETE FROM APPRECIATION WHERE ID = :appr_id");
$stmt->bindParam("appr_id", $I_appr_id);
$stmt->execute();
}
public function getApprByID($I_appr_id)
{
$O_model = Model::get();
$stmt = $O_model->prepare("SELECT * FROM APPRECIATION 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)
{
}
}

View File

@ -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;