Merge pull request #3 from ThomasRubini/first_impl

This commit is contained in:
Thomas Rubini 2023-01-16 20:55:00 +01:00
commit b0079b47c3
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373
11 changed files with 132 additions and 38 deletions

View File

@ -1,19 +0,0 @@
<?php
final class HelloworldController
{
public function defaultAction()
{
$O_helloworld = new Helloworld();
View::show('helloworld/view', array('helloworld' => $O_helloworld->getMessage()));
}
public function testformAction(Array $A_urlParams = null, Array $A_postParams = null)
{
View::show('helloworld/testform', array('formData' => $A_postParams));
}
}

View File

@ -0,0 +1,29 @@
<?php
final class RecipeController
{
public function viewAction(Array $A_urlParams = null, Array $A_postParams = null)
{
if(count($A_urlParams)!=1){
echo "404";
return;
}
$O_recipeModel = new RecipeModel();
$A_returnArray = $O_recipeModel->getFullRecipeWithComments($A_urlParams[0]);
if ($A_returnArray === null) {
echo "404";
return;
}
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));
}
}

View File

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

View File

@ -0,0 +1,18 @@
<?php
final class DifficultyModel
{
public function getByID($I_id)
{
$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"];
}
}

View File

@ -1,12 +0,0 @@
<?php
final class Helloworld
{
private $_S_message = "Hello World";
public function getMessage()
{
return $this->_S_message ;
}
}

View File

@ -0,0 +1,19 @@
<?php
final class IngredientModel
{
public function searchByRecipe($I_recipe_id)
{
$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();
}
}

35
Models/RecipeModel.php Normal file
View File

@ -0,0 +1,35 @@
<?php
final class RecipeModel
{
public function getRecipeByID($I_id)
{
$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;
}
}

17
Models/UserModel.php Normal file
View File

@ -0,0 +1,17 @@
<?php
final class UserModel
{
public function getNameByID($I_id)
{
$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"];
}
}

View File

@ -1,3 +0,0 @@
<?php
echo "<p>" . $A_view['helloworld'] . "</p>";

10
Views/recipe/view.php Normal file
View File

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