<?php

session_start();
require_once '../../bootstrap.php';
require '../../services/UserService.php';
require '../../services/ItemService.php';
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    $unit = new unit();
    $unit->setName($_POST["name"]);
    $saved = ItemService::saveWithValidation($unit, 200);
    if ($saved == true) {
        $id = $unit->getId();
        $result = array("id" => $id);
        echo json_encode($result);
    }
});
<?php

session_start();
require_once '../../bootstrap.php';
require '../../services/UserService.php';
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    if (array_key_exists("id", $_GET)) {
        $recipeStepId = $_GET["id"];
        $step = RecipeStepsQuery::create()->findPk($recipeStepId);
    }
    if (isset($step)) {
        $next = $step->getNextStep();
        if (isset($next)) {
            $swap = $step->getorder();
            $step->setorder($next->getorder());
            $next->setorder($swap);
            $step->save();
            $next->save();
        }
        http_response_code(204);
        echo "true";
    } else {
        http_response_code(404);
    }
});
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    if (array_key_exists("id", $_GET)) {
        $ingredientId = $_GET["id"];
        $ingredient = ingredientTypeQuery::create()->findPk($ingredientId);
    }
    $force = false;
    if (array_key_exists("force", $_GET)) {
        $force = $_GET["force"];
    }
    if (isset($ingredient)) {
        try {
            $databaseName = unitPeer::DATABASE_NAME;
            $constraints = array("ingredient" => "ingredientId");
            if ($force == true) {
                ForeignService::forceForeignConstraints($databaseName, $constraints, $ingredientId, true);
                $ingredient->delete();
                http_response_code(204);
            } else {
                $result = ForeignService::verifyForeignConstraints($databaseName, $constraints, $ingredientId);
                if ($result) {
                    $ingredient->delete();
                    http_response_code(204);
                } else {
                    echo json_encode(array("result" => "Cet ingredient est référencée dans les ingredients de recette."));
                }
            }
        } catch (PropelException $ex) {
            http_response_code(500);
        }
    } else {
        http_response_code(404);
    }
});
<?php

session_start();
require_once '../../../bootstrap.php';
require '../../../services/UserService.php';
require '../../../services/ItemService.php';
require '../../../utils/Exceptions.php';
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    $ingredient = new ingredient();
    $ingredient->setquantity($_POST["quantity"]);
    if (array_key_exists("unitId", $_POST)) {
        $unitId = $_POST["unitId"];
        if ($unitId != "-1") {
            $ingredient->setunitId($_POST["unitId"]);
        }
    }
    $ingredient->setingredientId($_POST["ingredientId"]);
    $recipeId = $_GET["recipeId"];
    $recipe = RecipeQuery::create()->findPk($recipeId);
    if (isset($recipe)) {
        $recipe->addingredient($ingredient);
        $saved = ItemService::saveWithValidation($ingredient, 200);
        if ($saved == true) {
            $id = $ingredient->getId();
            $result = array("id" => $id);
            echo json_encode($result);
        }
    } else {
        http_response_code(404);
    }
});
<?php

session_start();
require_once '../../bootstrap.php';
require '../../services/UserService.php';
require '../../services/ItemService.php';
require '../../services/RequestService.php';
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    $_PUT = RequestService::processPutParams();
    if (array_key_exists("id", $_GET)) {
        $ingredientId = $_GET["id"];
        $ingredient = ingredientTypeQuery::create()->findPk($ingredientId);
    }
    if (isset($ingredient)) {
        $ingredient->setName($_PUT["name"]);
        ItemService::saveWithValidation($ingredient, 204);
    } else {
        http_response_code(404);
    }
});
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    $recipe = new Recipe();
    if (array_key_exists("name", $_POST)) {
        $recipe->setName($_POST["name"]);
    }
    if (array_key_exists("description", $_POST)) {
        $recipe->setDescription($_POST["description"]);
    }
    if (array_key_exists("photo", $_POST)) {
        $recipe->setphoto($_POST['photo']);
    }
    if (array_key_exists("cost", $_POST)) {
        $recipe->setcost($_POST['cost']);
    }
    if (array_key_exists("difficulty", $_POST)) {
        $recipe->setdifficulty($_POST['difficulty']);
    }
    if (array_key_exists("time", $_POST)) {
        $recipe->settime($_POST['time']);
    }
    if (array_key_exists("calories", $_POST)) {
        $recipe->setcalories($_POST['calories']);
    }
    if (array_key_exists("countryId", $_POST)) {
        $origin = $_POST["countryId"];
        if ($origin != "-1") {
            $recipe->setorigin($origin);
        }
    }
    if (array_key_exists("categoryId", $_POST)) {
        $category = $_POST["categoryId"];
        if ($category != "-1") {
            $recipe->setcategory($category);
        }
    }
    if (array_key_exists("tags", $_POST)) {
        $tags = TagService::processTags($_POST["tags"]);
        foreach ($tags as $current) {
            $newTag = new tags();
            $newTag->settagNames($current);
            $recipe->addtags($newTag);
        }
    }
    $saved = ItemService::saveWithValidation($recipe, 200);
    if ($saved == true) {
        $id = $recipe->getId();
        $result = array("id" => $id);
        echo json_encode($result);
    }
});
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    if (array_key_exists("id", $_GET)) {
        $countryId = $_GET["id"];
        $country = countryQuery::create()->findPk($countryId);
    }
    $force = false;
    if (array_key_exists("force", $_GET)) {
        $force = $_GET["force"];
    }
    if (isset($country)) {
        try {
            $databaseName = countryPeer::DATABASE_NAME;
            $constraints = array("recipes" => "origin");
            if ($force == true) {
                ForeignService::forceForeignConstraints($databaseName, $constraints, $countryId);
                $country->delete();
                http_response_code(204);
            } else {
                $result = ForeignService::verifyForeignConstraints($databaseName, $constraints, $countryId);
                if ($result) {
                    $country->delete();
                    http_response_code(204);
                } else {
                    echo json_encode(array("result" => "Ce pays est référencé par des recettes."));
                }
            }
        } catch (PropelException $ex) {
            http_response_code(500);
        }
    } else {
        http_response_code(404);
    }
});
<?php

session_start();
require_once '../../../bootstrap.php';
require '../../../services/UserService.php';
require '../../../services/ItemService.php';
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    $step = new RecipeSteps();
    $step->setdescription($_POST["description"]);
    $recipeId = $_GET["recipeId"];
    $recipe = RecipeQuery::create()->findPk($recipeId);
    if (isset($recipe)) {
        $recipe->addRecipeSteps($step);
        $saved = ItemService::saveWithValidation($recipe, 200);
        if ($saved == true) {
            $id = $step->getId();
            $result = array("id" => $id);
            echo json_encode($result);
        }
    } else {
        http_response_code(404);
    }
});
<?php

session_start();
require_once '../../bootstrap.php';
require '../../services/UserService.php';
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    if (array_key_exists("id", $_GET)) {
        $recipeId = $_GET["id"];
        $recipe = RecipeQuery::create()->findPk($recipeId);
    }
    if (isset($recipe)) {
        try {
            tagsQuery::create()->filterByRecipe($recipe)->delete();
            $recipe->delete();
            http_response_code(204);
        } catch (PropelException $ex) {
            http_response_code(500);
        }
    } else {
        http_response_code(404);
    }
});
<?php

session_start();
require_once '../../bootstrap.php';
require '../../services/UserService.php';
require '../../services/ItemService.php';
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    $country = new country();
    $country->setName($_POST["name"]);
    $country->setflag($_POST["flag"]);
    $saved = ItemService::saveWithValidation($country, 200);
    if ($saved == true) {
        $id = $country->getId();
        $result = array("id" => $id);
        echo json_encode($result);
    }
});
<?php

session_start();
require_once '../../bootstrap.php';
require '../../services/UserService.php';
require '../../services/ItemService.php';
require '../../services/RequestService.php';
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    $_PUT = RequestService::processPutParams();
    if (array_key_exists("id", $_GET)) {
        $countryId = $_GET["id"];
        $country = countryQuery::create()->findPk($countryId);
    }
    if (isset($country)) {
        $country->setName($_PUT["name"]);
        $country->setflag($_PUT["flag"]);
        ItemService::saveWithValidation($country, 204);
    } else {
        http_response_code(404);
    }
});
<?php

session_start();
require_once '../../bootstrap.php';
require '../../services/UserService.php';
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    if (array_key_exists("id", $_GET)) {
        $ingredient = ingredientQuery::create()->findPk($_GET["id"]);
    }
    if (isset($ingredient)) {
        try {
            $ingredient->delete();
            http_response_code(204);
        } catch (PropelException $ex) {
            http_response_code(500);
        }
    } else {
        http_response_code(404);
    }
});
<?php

session_start();
require_once '../../bootstrap.php';
require '../../services/UserService.php';
require '../../services/ItemService.php';
require '../../services/RequestService.php';
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    $_PUT = RequestService::processPutParams();
    if (array_key_exists("id", $_GET)) {
        $recipeStepId = $_GET["id"];
        $step = RecipeStepsQuery::create()->findPk($recipeStepId);
    }
    if (isset($step)) {
        $step->setdescription($_PUT["description"]);
        ItemService::saveWithValidation($step, 204);
    } else {
        http_response_code(404);
    }
});
<?php

session_start();
require_once '../../bootstrap.php';
require '../../services/UserService.php';
require '../../services/ForeignService.php';
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    if (array_key_exists("id", $_GET)) {
        $tagId = $_GET["id"];
        $tag = tagNamesQuery::create()->findPk($tagId);
    }
    if (isset($tag)) {
        try {
            $tag->delete();
            http_response_code(204);
        } catch (PropelException $ex) {
            http_response_code(500);
        }
    } else {
        http_response_code(404);
    }
});
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    $_PUT = RequestService::processPutParams();
    if (array_key_exists("id", $_GET)) {
        $recipeId = $_GET["id"];
        $recipe = RecipeQuery::create()->findPk($recipeId);
    }
    if (isset($recipe)) {
        $recipe->setName($_PUT["name"]);
        $recipe->setDescription($_PUT["description"]);
        if (array_key_exists("photo", $_PUT)) {
            $recipe->setphoto($_PUT['photo']);
        }
        if (array_key_exists("cost", $_PUT)) {
            $recipe->setcost($_PUT['cost']);
        }
        if (array_key_exists("difficulty", $_PUT)) {
            $recipe->setdifficulty($_PUT['difficulty']);
        }
        if (array_key_exists("time", $_PUT)) {
            $recipe->settime($_PUT['time']);
        }
        if (array_key_exists("calories", $_PUT)) {
            $recipe->setcalories($_PUT['calories']);
        }
        if (array_key_exists("countryId", $_PUT) && $_PUT["countryId"] != "-1") {
            $recipe->setorigin($_PUT["countryId"]);
        } else {
            $recipe->setcountry();
        }
        if (array_key_exists("categoryId", $_PUT) && $_PUT["categoryId"] != "-1") {
            $recipe->setcategory($_PUT["categoryId"]);
        } else {
            $recipe->setrecipeType();
        }
        if (array_key_exists("tags", $_PUT)) {
            $tags = TagService::processTags($_PUT["tags"]);
            foreach ($tags as $current) {
                $tagsId[] = $current->getid();
            }
            tagsQuery::create()->filterByRecipe($recipe)->where("tags.tagId NOT IN ?", $tagsId)->delete();
            $existingTags = tagsQuery::create()->filterByRecipe($recipe)->select(array("tagId"))->find()->toArray();
            foreach ($tagsId as $index => $current) {
                if (!in_array($current, $existingTags)) {
                    $newTag = new tags();
                    $newTag->settagNames($tags[$index]);
                    $recipe->addtags($newTag);
                }
            }
        }
        ItemService::saveWithValidation($recipe, 204);
    } else {
        http_response_code(404);
    }
});
<?php

session_start();
require_once '../../bootstrap.php';
require '../../services/UserService.php';
require '../../services/ItemService.php';
require '../../services/RequestService.php';
UserService::withRole(UserService::$CONTRIBUTOR, function () {
    $_PUT = RequestService::processPutParams();
    if (array_key_exists("id", $_GET)) {
        $ingredient = ingredientQuery::create()->findPk($_GET["id"]);
    }
    if (isset($ingredient)) {
        $ingredient->setquantity($_PUT["quantity"]);
        $ingredient->setingredientId($_PUT["ingredientId"]);
        if (array_key_exists("unitId", $_PUT)) {
            $ingredient->setunitId($_PUT["unitId"]);
        } else {
            $ingredient->setunitId(null);
        }
        ItemService::saveWithValidation($ingredient, 204);
    } else {
        http_response_code(404);
    }
});