move recipe data creation to RecipeModel and use real SQL database

This commit is contained in:
Thomas Rubini 2023-01-16 20:29:18 +01:00
parent 788f224629
commit 87e8f9247e
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373
3 changed files with 34 additions and 37 deletions

View File

@ -9,31 +9,14 @@ final class RecipeController
echo "404"; echo "404";
return; return;
} }
$O_recipeModel = new RecipeModel(); $O_recipeModel = new RecipeModel();
$O_recipe = $O_recipeModel->getByID($A_urlParams[0]); $A_returnArray = $O_recipeModel->getFullRecipeWithComments($A_urlParams[0]);
if($O_recipe === null){ if ($A_returnArray === null) {
echo "404"; echo "404";
return; return;
} }
$O_ingredientModel = new IngredientModel();
$A_ingredients = $O_ingredientModel->searchByRecipe($O_recipe["id"]);
$O_userModel = new UserModel();
$A_authorName = $O_userModel->getNameByID($O_recipe["author_id"]);
$O_userModel = new DifficultyModel();
$A_difficultyName = $O_userModel->getByID($O_recipe["difficulty_id"]);
$A_returnArray = array(
"recipe_name" => $O_recipe["name"],
"recipe_desc" => $O_recipe["desc"],
"author_name" => $A_authorName,
"recipe_ingredients" => $A_ingredients,
"difficulty_name" => $A_difficultyName,
);
View::show("recipe/view", $A_returnArray); View::show("recipe/view", $A_returnArray);
// print_r($A_urlParams); // print_r($A_urlParams);

View File

@ -3,19 +3,33 @@
final class RecipeModel final class RecipeModel
{ {
public function getByID($I_id) public function getRecipeByID($I_id)
{ {
if ($I_id == 36) { $O_model = Model::get();
return array( $stmt = $O_model->prepare("SELECT * FROM RECIPE WHERE ID=:id");
"id" => 36, $stmt->bindParam("id", $I_id);
"name" => "Ma recette num 36", $stmt->execute();
"desc" => "Une brève description de la recette 36",
"recipe" => "Etape 1\nEtape 2\nEtape 3", $row = $stmt->fetch();
"difficulty_id" => 1, if ($row === false) return null;
"author_id" => 1, return $row;
); }
} else {
return null; public function getFullRecipeWithComments($I_id)
} {
$A_recipe = self::getRecipeByID($I_id);
if ($A_recipe === null)return null;
$O_ingredientModel = new IngredientModel();
$A_recipe["INGREDIENTS"] = $O_ingredientModel->searchByRecipe($A_recipe["ID"]);
$O_userModel = new UserModel();
$A_recipe["AUTHOR_NAME"] = $O_userModel->getNameByID($A_recipe["AUTHOR_ID"]);
$O_userModel = new DifficultyModel();
$A_recipe["DIFFICULTY_NAME"] = $O_userModel->getByID($A_recipe["DIFFICULTY_ID"]);
return $A_recipe;
} }
} }

View File

@ -1,10 +1,10 @@
<p> <?= $A_view["recipe_name"] ?> </p> <p> <?= $A_view["NAME"] ?> </p>
<p> Auteur: <?= $A_view["author_name"] ?> </p> <p> Auteur: <?= $A_view["AUTHOR_NAME"] ?> </p>
<p> Difficulté: <?= $A_view["difficulty_name"] ?> </p> <p> Difficulté: <?= $A_view["DIFFICULTY_NAME"] ?> </p>
<p> Ingrédients: </p> <p> Ingrédients: </p>
<?php <?php
foreach($A_view["recipe_ingredients"] as $i){ foreach($A_view["INGREDIENTS"] as $i){
echo "<p> {$i['name']}: {$i['quantity']} </p>"; echo "<p> {$i['name']}: {$i['quantity']} </p>";
} }
?> ?>