refactored recipeModel
This commit is contained in:
parent
01ae39a025
commit
9633e59e5e
@ -9,8 +9,8 @@ final class RecipeController
|
||||
throw new HTTPSpecialCaseException(404);
|
||||
}
|
||||
|
||||
$O_recipeModel = new RecipeModel();
|
||||
$A_returnArray = $O_recipeModel->getFullRecipeWithApprs($A_urlParams[0]);
|
||||
//TODO MAKE THE VIEW USE THE NEW DATA FORMAT
|
||||
$A_returnArray = RecipeModel::getFullRecipeWithApprs($A_urlParams[0]);
|
||||
if ($A_returnArray === null) {
|
||||
throw new HTTPSpecialCaseException(404);
|
||||
}
|
||||
@ -28,19 +28,19 @@ final class RecipeController
|
||||
throw new HTTPSpecialCaseException(404);
|
||||
}
|
||||
|
||||
$O_recipeModel = new RecipeModel();
|
||||
$A_recipe = $O_recipeModel->getFullRecipe($A_urlParams[0]);
|
||||
if ($A_recipe === null) {
|
||||
|
||||
$O_recipe = RecipeModel::getFullRecipeById($A_urlParams[0]);
|
||||
if ($O_recipe === null) {
|
||||
throw new HTTPSpecialCaseException(404);
|
||||
}
|
||||
|
||||
if ($A_recipe["AUTHOR_ID"] !== $_SESSION["ID"]) {
|
||||
if ($O_recipe->I_AUTHOR_ID !== $_SESSION["ID"]) {
|
||||
if(!Session::is_admin()){
|
||||
throw new HTTPSpecialCaseException(400, "You are not the owner of this recipe");
|
||||
}
|
||||
}
|
||||
|
||||
View::show("recipe/edit", array("POST_URI" => "/recipe/update", "RECIPE" => $A_recipe));
|
||||
//TODO MAKE THE VIEW USE THE NEW DATA FORMAT
|
||||
View::show("recipe/edit", array("POST_URI" => "/recipe/update", "RECIPE" => $O_recipe));
|
||||
}
|
||||
|
||||
public function newAction(Array $A_urlParams = null, Array $A_postParams = null)
|
||||
@ -67,8 +67,8 @@ final class RecipeController
|
||||
private function searchQueryView(Array $A_urlParams = null, Array $A_postParams = null, Array $A_getParams = null)
|
||||
{
|
||||
|
||||
$O_recipeModel = new RecipeModel();
|
||||
$A_results = $O_recipeModel->searchRecipesByName($A_getParams["query"]);
|
||||
//TODO change this when the function will return object array
|
||||
$A_results = RecipeModel::searchRecipesByName($A_getParams["query"]);
|
||||
|
||||
View::show("recipe/search", array(
|
||||
"QUERY" => $A_getParams["query"],
|
||||
@ -80,12 +80,12 @@ final class RecipeController
|
||||
{
|
||||
if (count($A_urlParams) !== 1 ) throw new HTTPSpecialCaseException(404);
|
||||
|
||||
$O_recipeModel = new RecipeModel();
|
||||
$A_recipe = $O_recipeModel->getRecipeByID($A_urlParams[0]);
|
||||
|
||||
$O_recipe = RecipeModel::getRecipeByID($A_urlParams[0]);
|
||||
|
||||
header("Content-Type: image");
|
||||
if (isset($A_recipe) && $A_recipe["IMG"] !== null) {
|
||||
echo $A_recipe["IMG"];
|
||||
if (isset($O_recipe) && $O_recipe->getImage() !== null) {
|
||||
echo $O_recipe->getImage();
|
||||
} else {
|
||||
echo file_get_contents(Constants::rootDir()."/static/img/default_recipe.jpg");
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
final class IngredientModel
|
||||
{
|
||||
|
||||
public function searchByRecipe($I_recipe_id)
|
||||
public static function searchByRecipe($I_recipe_id)
|
||||
{
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("
|
||||
|
||||
@ -2,8 +2,67 @@
|
||||
|
||||
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;
|
||||
public $I_AUTHOR_ID = null;
|
||||
|
||||
public function getRecipeByID($I_id)
|
||||
public $O_DIFFICULTY = null;
|
||||
public $O_AUTHOR = null;
|
||||
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)
|
||||
{
|
||||
$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;
|
||||
$this->I_AUTHOR_ID = $I_AUTHOR_ID;
|
||||
}
|
||||
|
||||
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, :desc, :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("desc", $this->S_DESCR);
|
||||
$stmt->bindParam("recipe", $this->S_RECIPE);
|
||||
$stmt->bindParam("difficulty_id", $this->I_DIFFICULTY_ID);
|
||||
$stmt->bindParam("author_id", $this->I_AUTHOR_ID);
|
||||
$stmt->execute();
|
||||
$this->I_ID = Model::get()->lastInsertId();
|
||||
}
|
||||
public function update()
|
||||
{
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("UPDATE RECIPE SET NAME=:name, TIME=:time, COST=:cost, DESCR=:desc, 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("desc", $this->S_DESCR);
|
||||
$stmt->bindParam("recipe", $this->S_RECIPE);
|
||||
$stmt->bindParam("difficulty_id", $this->I_DIFFICULTY_ID);
|
||||
$stmt->bindParam("author_id", $this->I_AUTHOR_ID);
|
||||
$stmt->execute();
|
||||
}
|
||||
public function delete(){
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("DELETE FROM RECIPE WHERE ID=:id");
|
||||
$stmt->bindParam("id", $this->I_ID);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
public static function getRecipeByID($I_id)
|
||||
{
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("SELECT * FROM RECIPE WHERE ID=:id");
|
||||
@ -12,39 +71,76 @@ final class RecipeModel
|
||||
|
||||
$row = $stmt->fetch();
|
||||
if ($row === false) return null;
|
||||
return $row;
|
||||
|
||||
$O_recipe = new RecipeModel($row["NAME"], $row["TIME"], $row["COST"], $row["DESCR"], $row["RECIPE"], $row["DIFFICULTY_ID"], $row["AUTHOR_ID"]);
|
||||
|
||||
$O_recipe->I_ID = $I_id;
|
||||
return $O_recipe;
|
||||
}
|
||||
|
||||
public function getFullRecipe($I_id)
|
||||
{
|
||||
$A_recipe = self::getRecipeByID($I_id);
|
||||
if ($A_recipe === null)return null;
|
||||
|
||||
$A_recipe["IMG_LINK"] = "/static/img/recipes/".$A_recipe["ID"].".jpg";
|
||||
|
||||
$O_ingredientModel = new IngredientModel();
|
||||
$A_recipe["INGREDIENTS"] = $O_ingredientModel->searchByRecipe($A_recipe["ID"]);
|
||||
|
||||
$A_recipe["AUTHOR_USERNAME"] = UserModel::getByID($A_recipe["AUTHOR_ID"])->S_USERNAME;
|
||||
|
||||
$A_recipe["DIFFICULTY_NAME"] = DifficultyModel::getByID($A_recipe["DIFFICULTY_ID"])->S_NAME;
|
||||
|
||||
return $A_recipe;
|
||||
public function getImage(){
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("SELECT IMG FROM RECIPE WHERE ID=:id");
|
||||
$stmt->bindParam("id", $this->I_ID);
|
||||
$stmt->execute();
|
||||
$row = $stmt->fetch();
|
||||
if ($row === false) return null;
|
||||
return $row["IMG"];
|
||||
}
|
||||
|
||||
public function getFullRecipeWithApprs($I_id)
|
||||
{
|
||||
$A_recipe = self::getFullRecipe($I_id);
|
||||
if ($A_recipe === null)return null;
|
||||
public function getAuthor(){
|
||||
if($this->O_AUTHOR === null){
|
||||
$this->O_AUTHOR = UserModel::getByID($this->I_AUTHOR_ID);
|
||||
}
|
||||
return $this->O_AUTHOR;
|
||||
}
|
||||
|
||||
public function getIngredients(){
|
||||
if($this->A_INGREDIENTS === null){
|
||||
$this->A_INGREDIENTS = IngredientModel::searchByRecipe($this->I_ID);
|
||||
}
|
||||
return $this->A_INGREDIENTS;
|
||||
}
|
||||
public function getDifficulty(){
|
||||
if($this->O_AUTHOR === null){
|
||||
$this->O_AUTHOR = DifficultyModel::getByID($this->I_DIFFICULTY_ID);
|
||||
}
|
||||
return $this->O_AUTHOR;
|
||||
}
|
||||
|
||||
public function getApprs(){
|
||||
if ($this->A_APPRS === null) {
|
||||
$O_apprModel = new ApprModel();
|
||||
|
||||
$A_recipe["APPRS"] = $O_apprModel->searchRecipeApprsWithAuthors($I_id);
|
||||
|
||||
return $A_recipe;
|
||||
$this->A_APPRS = $O_apprModel->searchRecipeApprsWithAuthors($this->I_ID);
|
||||
}
|
||||
return $this->A_APPRS;
|
||||
}
|
||||
|
||||
public function searchRecipesByName($S_query)
|
||||
public function getFullRecipe()
|
||||
{
|
||||
$this->getAuthor();
|
||||
$this->getDifficulty();
|
||||
$this->getIngredients();
|
||||
}
|
||||
public static function getFullRecipeById($I_id)
|
||||
{
|
||||
$O_recipe = self::getRecipeByID($I_id);
|
||||
$O_recipe->getFullRecipe();
|
||||
return $O_recipe;
|
||||
}
|
||||
|
||||
public static function getFullRecipeWithApprs($I_id)
|
||||
{
|
||||
$O_recipe = self::getFullRecipeById($I_id);
|
||||
if ($O_recipe === null)return null;
|
||||
|
||||
$O_recipe->getApprs();
|
||||
|
||||
return $O_recipe;
|
||||
}
|
||||
|
||||
//TODO: return array object
|
||||
public static function searchRecipesByName($S_query)
|
||||
{
|
||||
|
||||
$O_model = Model::get();
|
||||
@ -72,7 +168,7 @@ final class RecipeModel
|
||||
WHERE CTE.NAME is not null
|
||||
AND INSTR(RECIPE.NAME, CTE.NAME) > 0
|
||||
GROUP BY RECIPE.ID
|
||||
ORDER BY count(RECIPE.ID) DESC
|
||||
ORDER BY count(RECIPE.ID) DESCR
|
||||
LIMIT 10;
|
||||
");
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user