Merge pull request #99 from ThomasRubini/create_recipe

This commit is contained in:
Thomas Rubini 2023-01-25 21:52:00 +01:00 committed by GitHub
commit 81e67a0bf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 15 deletions

View File

@ -51,6 +51,24 @@ final class RecipeController
View::show("recipe/edit", array("POST_URI" => "/recipe/create", "RECIPE" => array()));
}
public function createAction(Array $A_urlParams = null, Array $A_postParams = null)
{
Session::login_or_die();
$O_difficulty = DifficultyModel::getByName($A_postParams["recipeDifficulty"]);
if($O_difficulty === null){
throw new HTTPSpecialCaseException(400, "Invalid difficulty");
}
$O_recipe = new RecipeModel(
$A_postParams["recipeName"], $A_postParams["recipeTime"], $A_postParams["recipeDescription"],
null, $O_difficulty->I_ID, $_SESSION["ID"]
);
$O_recipe->insert();
header("Location: /recipe/view/".$O_recipe->I_ID);
}
public function searchAction(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null)
{
if (isset($A_getParams["query"])) {

View File

@ -5,7 +5,6 @@ final class RecipeModel
public $I_ID = null;
public $S_NAME = null;
public $I_TIME = null;
public $I_COST = null;
public $S_DESCR = null;
public $S_RECIPE = null;
public $I_DIFFICULTY_ID = null;
@ -16,11 +15,10 @@ final class RecipeModel
public $A_APPRS = null;
public $A_INGREDIENTS = null;
public function __construct($S_NAME, $I_TIME, $I_COST, $S_DESCR, $S_RECIPE, $I_DIFFICULTY_ID, $I_AUTHOR_ID)
public function __construct($S_NAME, $I_TIME, $S_DESCR, $S_RECIPE, $I_DIFFICULTY_ID, $I_AUTHOR_ID)
{
$this->S_NAME = $S_NAME;
$this->I_TIME = $I_TIME;
$this->I_COST = $I_COST;
$this->S_DESCR = $S_DESCR;
$this->S_RECIPE = $S_RECIPE;
$this->I_DIFFICULTY_ID = $I_DIFFICULTY_ID;
@ -30,10 +28,9 @@ final class RecipeModel
public function insert()
{
$O_model = Model::get();
$stmt = $O_model->prepare("INSERT INTO RECIPE (NAME, TIME, COST, DESCR, RECIPE ,DIFFICULTY_ID, AUTHOR_ID) VALUES(:name, :time, :cost, :descr, :recipe, :difficulty_id, :author_id)");
$stmt = $O_model->prepare("INSERT INTO RECIPE (NAME, TIME, DESCR, RECIPE ,DIFFICULTY_ID, AUTHOR_ID) VALUES(:name, :time, :descr, :recipe, :difficulty_id, :author_id)");
$stmt->bindParam("name", $this->S_NAME);
$stmt->bindParam("time", $this->I_TIME);
$stmt->bindParam("cost", $this->I_COST);
$stmt->bindParam("descr", $this->S_DESCR);
$stmt->bindParam("recipe", $this->S_RECIPE);
$stmt->bindParam("difficulty_id", $this->I_DIFFICULTY_ID);
@ -44,11 +41,10 @@ final class RecipeModel
public function update()
{
$O_model = Model::get();
$stmt = $O_model->prepare("UPDATE RECIPE SET NAME=:name, TIME=:time, COST=:cost, DESCR=:descr, RECIPE:recipe, DIFFICULTY_ID=:difficulty_id, AUTHOR_ID=:author_id WHERE ID=:id");
$stmt = $O_model->prepare("UPDATE RECIPE SET NAME=:name, TIME=:time, DESCR=:descr, RECIPE:recipe, DIFFICULTY_ID=:difficulty_id, AUTHOR_ID=:author_id WHERE ID=:id");
$stmt->bindParam("id", $this->I_ID);
$stmt->bindParam("name", $this->S_NAME);
$stmt->bindParam("time", $this->I_TIME);
$stmt->bindParam("cost", $this->I_COST);
$stmt->bindParam("descr", $this->S_DESCR);
$stmt->bindParam("recipe", $this->S_RECIPE);
$stmt->bindParam("difficulty_id", $this->I_DIFFICULTY_ID);
@ -63,7 +59,7 @@ final class RecipeModel
}
private static function createFromRow($A_row, $I_ID){
$O_recipe = new RecipeModel($A_row["NAME"], $A_row["TIME"], $A_row["COST"], $A_row["DESCR"], $A_row["RECIPE"], $A_row["DIFFICULTY_ID"], $A_row["AUTHOR_ID"]);
$O_recipe = new RecipeModel($A_row["NAME"], $A_row["TIME"], $A_row["DESCR"], $A_row["RECIPE"], $A_row["DIFFICULTY_ID"], $A_row["AUTHOR_ID"]);
$O_recipe->I_ID = $I_ID;
return $O_recipe;
}

View File

@ -18,7 +18,7 @@ function getOrEmpty($A_Dict, $S_keyName) {
<form action="<?= $A_view["POST_URI"] ?>" method="post">
<label for="recipeImage">Entrez l'image de haut de page&nbsp;:</label>
<input type="file" name="recipeImage" id="recipeImage" required>
<input type="file" name="recipeImage" id="recipeImage">
<label for="recipeName">Nom de la recette&nbsp;:</label>
<input type="text" name="recipeName" id="recipeName" placeholder="Nom du plat" value="<?= getOrEmpty($A_recipe, "NAME") ?>" required>
@ -31,7 +31,7 @@ function getOrEmpty($A_Dict, $S_keyName) {
<h1>Informations alimentaires</h1>
<label for="recipeFifficulte">Niveau de difficulé&nbsp;:</label>
<select name="recipeDifficulte" id="recipeDifficulte" required>
<select name="recipeDifficulty" id="recipeDifficulte" required>
<option value="tresFacile" <?= getOrEmpty($A_recipe, "DIFFICULTY_NAME")=="Très facile"? 'selected="selected"' : "" ?> >Très facile</option>
<option value="facile" <?= getOrEmpty($A_recipe, "DIFFICULTY_NAME")=="Facile"? 'selected="selected"' : "" ?>>Facile</option>
<option value="moyen" <?= getOrEmpty($A_recipe, "DIFFICULTY_NAME")=="Moyen"? 'selected="selected"' : "" ?>>Moyen</option>
@ -39,12 +39,12 @@ function getOrEmpty($A_Dict, $S_keyName) {
</select>
<legend>Type de plat&nbsp;:</legend>
<input type="checkbox" name="recipeVegan" id="recipeVegan" <?= in_array("Végan", getOrEmpty($A_recipe, "TYPE"))? "checked":"" ?> >
<legend>Particularités du plat&nbsp;:</legend>
<input type="checkbox" name="part_Vegan" id="recipeVegan" <?= in_array("Végan", getOrEmpty($A_recipe, "TYPE"))? "checked":"" ?> >
<label for="recipeVegan">Végan</label>
<input type="checkbox" name="recipeLactoseFree" id="recipeLactoseFree" <?= in_array("Sans lactose", getOrEmpty($A_recipe, "TYPE"))? "checked":"" ?> >
<input type="checkbox" name="part_LactoseFree" id="recipeLactoseFree" <?= in_array("Sans lactose", getOrEmpty($A_recipe, "TYPE"))? "checked":"" ?> >
<label for="recipeLactoseFree">Sans lactose</label>
<input type="checkbox" name="recipeGlutenFree" id="recipeGlutenFree" <?= in_array("Sans gluten", getOrEmpty($A_recipe, "TYPE"))? "checked":"" ?> >
<input type="checkbox" name="part_GlutenFree" id="recipeGlutenFree" <?= in_array("Sans gluten", getOrEmpty($A_recipe, "TYPE"))? "checked":"" ?> >
<label for="recipeGlutenFree">Sans gluten</label>
</br>