From 95ad2e1d6afbeec1846d4c3bbf9dc67f35b684c5 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Thu, 12 Jan 2023 17:15:19 +0100 Subject: [PATCH 1/4] Example implementation of a controller --- Controllers/HelloworldController.php | 19 -------- Controllers/RecipeController.php | 46 +++++++++++++++++++ Models/DifficultyModel.php | 14 ++++++ Models/Helloworld.php | 12 ----- Models/IngredientModel.php | 21 +++++++++ Models/RecipeModel.php | 21 +++++++++ Models/UserModel.php | 14 ++++++ Views/helloworld/view.php | 3 -- .../testform.php => recipe/edit.php} | 0 Views/recipe/view.php | 10 ++++ 10 files changed, 126 insertions(+), 34 deletions(-) delete mode 100644 Controllers/HelloworldController.php create mode 100644 Controllers/RecipeController.php create mode 100644 Models/DifficultyModel.php delete mode 100644 Models/Helloworld.php create mode 100644 Models/IngredientModel.php create mode 100644 Models/RecipeModel.php create mode 100644 Models/UserModel.php delete mode 100644 Views/helloworld/view.php rename Views/{helloworld/testform.php => recipe/edit.php} (100%) create mode 100644 Views/recipe/view.php diff --git a/Controllers/HelloworldController.php b/Controllers/HelloworldController.php deleted file mode 100644 index 4215141..0000000 --- a/Controllers/HelloworldController.php +++ /dev/null @@ -1,19 +0,0 @@ - $O_helloworld->getMessage())); - - } - - public function testformAction(Array $A_urlParams = null, Array $A_postParams = null) - { - - View::show('helloworld/testform', array('formData' => $A_postParams)); - - } - -} \ No newline at end of file diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php new file mode 100644 index 0000000..cb09912 --- /dev/null +++ b/Controllers/RecipeController.php @@ -0,0 +1,46 @@ +getByID($A_urlParams[0]); + if($O_recipe === null){ + echo "404"; + 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); + + // print_r($A_urlParams); + // $O_recetteModel = new RecipeIngredientsModel(); + // $O_recetteModel->getByID(""); + // View::show('helloworld/testform', array('formData' => $A_postParams)); + + } + +} \ No newline at end of file diff --git a/Models/DifficultyModel.php b/Models/DifficultyModel.php new file mode 100644 index 0000000..874c235 --- /dev/null +++ b/Models/DifficultyModel.php @@ -0,0 +1,14 @@ +_S_message ; - } - -} \ No newline at end of file diff --git a/Models/IngredientModel.php b/Models/IngredientModel.php new file mode 100644 index 0000000..b1d11b3 --- /dev/null +++ b/Models/IngredientModel.php @@ -0,0 +1,21 @@ + 1, + "name" => "oeuf", + "quantity" => "6", + ), + array( + "id" => 2, + "name" => "lait", + "quantity" => "1/2L", + ), + ); + } +} diff --git a/Models/RecipeModel.php b/Models/RecipeModel.php new file mode 100644 index 0000000..7f367a9 --- /dev/null +++ b/Models/RecipeModel.php @@ -0,0 +1,21 @@ + 36, + "name" => "Ma recette num 36", + "desc" => "Une brève description de la recette 36", + "recipe" => "Etape 1\nEtape 2\nEtape 3", + "difficulty_id" => 1, + "author_id" => 1, + ); + } else { + return null; + } + } +} diff --git a/Models/UserModel.php b/Models/UserModel.php new file mode 100644 index 0000000..4add48a --- /dev/null +++ b/Models/UserModel.php @@ -0,0 +1,14 @@ +" . $A_view['helloworld'] . "

"; diff --git a/Views/helloworld/testform.php b/Views/recipe/edit.php similarity index 100% rename from Views/helloworld/testform.php rename to Views/recipe/edit.php diff --git a/Views/recipe/view.php b/Views/recipe/view.php new file mode 100644 index 0000000..8cb32d2 --- /dev/null +++ b/Views/recipe/view.php @@ -0,0 +1,10 @@ +

+

Auteur:

+

Difficulté:

+

Ingrédients:

+ {$i['name']}: {$i['quantity']}

"; +} +?> \ No newline at end of file From 788f2246295601d24155afa63538d7b015d0a4d3 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Mon, 16 Jan 2023 19:55:40 +0100 Subject: [PATCH 2/4] fix primary Model --- Kernel/Model.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Kernel/Model.php b/Kernel/Model.php index 5c45e3d..4ce8fb8 100644 --- a/Kernel/Model.php +++ b/Kernel/Model.php @@ -5,17 +5,17 @@ final class Model private static $conn = null; public static function get(){ - if($conn === null){ - init(); + if(self::$conn === null){ + self::init(); } - return $conn; + return self::$conn; } private static function init(){ $PDO_URI = sprintf("mysql:host=%s;dbname=%s", $_ENV["DB_HOST"], $_ENV["DB_DBNAME"]); try{ - $conn = new PDO($PDO_URI, $_ENV["DB_USER"], $_ENV["DB_PASSWORD"]); + self::$conn = new PDO($PDO_URI, $_ENV["DB_USERNAME"], $_ENV["DB_PASSWORD"]); }catch(PDOException $e){ die("Connection to the database failed"); } From 87e8f9247e02b0d5578a255723c15fc0098c99e7 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Mon, 16 Jan 2023 20:29:18 +0100 Subject: [PATCH 3/4] move recipe data creation to RecipeModel and use real SQL database --- Controllers/RecipeController.php | 23 +++--------------- Models/RecipeModel.php | 40 +++++++++++++++++++++----------- Views/recipe/view.php | 8 +++---- 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php index cb09912..5b22ce6 100644 --- a/Controllers/RecipeController.php +++ b/Controllers/RecipeController.php @@ -9,31 +9,14 @@ final class RecipeController echo "404"; return; } - + $O_recipeModel = new RecipeModel(); - $O_recipe = $O_recipeModel->getByID($A_urlParams[0]); - if($O_recipe === null){ + $A_returnArray = $O_recipeModel->getFullRecipeWithComments($A_urlParams[0]); + if ($A_returnArray === null) { echo "404"; 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); // print_r($A_urlParams); diff --git a/Models/RecipeModel.php b/Models/RecipeModel.php index 7f367a9..1b0ca1f 100644 --- a/Models/RecipeModel.php +++ b/Models/RecipeModel.php @@ -3,19 +3,33 @@ final class RecipeModel { - public function getByID($I_id) + public function getRecipeByID($I_id) { - if ($I_id == 36) { - return array( - "id" => 36, - "name" => "Ma recette num 36", - "desc" => "Une brève description de la recette 36", - "recipe" => "Etape 1\nEtape 2\nEtape 3", - "difficulty_id" => 1, - "author_id" => 1, - ); - } else { - return null; - } + $O_model = Model::get(); + $stmt = $O_model->prepare("SELECT * FROM RECIPE WHERE ID=:id"); + $stmt->bindParam("id", $I_id); + $stmt->execute(); + + $row = $stmt->fetch(); + if ($row === false) return null; + return $row; + } + + 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; } } diff --git a/Views/recipe/view.php b/Views/recipe/view.php index 8cb32d2..ec33823 100644 --- a/Views/recipe/view.php +++ b/Views/recipe/view.php @@ -1,10 +1,10 @@ -

-

Auteur:

-

Difficulté:

+

+

Auteur:

+

Difficulté:

Ingrédients:

{$i['name']}: {$i['quantity']}

"; } ?> \ No newline at end of file From 3da99a3ac7d85ebda5597be41dbacd2ea194cdee Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Mon, 16 Jan 2023 20:53:32 +0100 Subject: [PATCH 4/4] Change all models to use the SQL database --- Models/DifficultyModel.php | 16 ++++++++++------ Models/IngredientModel.php | 22 ++++++++++------------ Models/UserModel.php | 13 ++++++++----- Views/recipe/view.php | 2 +- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/Models/DifficultyModel.php b/Models/DifficultyModel.php index 874c235..5b6293f 100644 --- a/Models/DifficultyModel.php +++ b/Models/DifficultyModel.php @@ -3,12 +3,16 @@ final class DifficultyModel { - public function getByID($I_difficuly_id) + public function getByID($I_id) { - if ($I_difficuly_id == 1) { - return "Facile"; - } else { - return null; - } + + $O_model = Model::get(); + $stmt = $O_model->prepare("SELECT * FROM DIFFICULTY WHERE ID=:id"); + $stmt->bindParam("id", $I_id); + $stmt->execute(); + + $row = $stmt->fetch(); + if ($row === false) return null; + return $row["NAME"]; } } diff --git a/Models/IngredientModel.php b/Models/IngredientModel.php index b1d11b3..1f28d9a 100644 --- a/Models/IngredientModel.php +++ b/Models/IngredientModel.php @@ -5,17 +5,15 @@ final class IngredientModel public function searchByRecipe($I_recipe_id) { - return array( - array( - "id" => 1, - "name" => "oeuf", - "quantity" => "6", - ), - array( - "id" => 2, - "name" => "lait", - "quantity" => "1/2L", - ), - ); + $O_model = Model::get(); + $stmt = $O_model->prepare(" + SELECT * FROM INGREDIENT + JOIN RECIPE_INGREDIENT ON RECIPE_INGREDIENT.INGREDIENT_ID=INGREDIENT.ID + WHERE RECIPE_INGREDIENT.RECIPE_ID = :recipe_id + "); + $stmt->bindParam("recipe_id", $I_recipe_id); + $stmt->execute(); + + return $stmt->fetchAll(); } } diff --git a/Models/UserModel.php b/Models/UserModel.php index 4add48a..4919f55 100644 --- a/Models/UserModel.php +++ b/Models/UserModel.php @@ -5,10 +5,13 @@ final class UserModel public function getNameByID($I_id) { - if ($I_id == 1) { - return "Thomas"; - } else { - return null; - } + $O_model = Model::get(); + $stmt = $O_model->prepare("SELECT NAME FROM USER WHERE ID=:id"); + $stmt->bindParam("id", $I_id); + $stmt->execute(); + + $row = $stmt->fetch(); + if ($row === false) return null; + return $row["NAME"]; } } diff --git a/Views/recipe/view.php b/Views/recipe/view.php index ec33823..3f49e0e 100644 --- a/Views/recipe/view.php +++ b/Views/recipe/view.php @@ -5,6 +5,6 @@ {$i['name']}: {$i['quantity']}

"; + echo "

{$i['NAME']}: {$i['QUANTITY']}

"; } ?> \ No newline at end of file