Exemplo n.º 1
0
            $itemArray = array('id' => $row['id'], 'recipeId' => $row['recipe_id'], 'name' => $row['name']);
            array_push($results, $itemArray);
        }
        $app->response()->header('Content-Type', 'application/json');
        echo json_encode($results);
    } else {
        $app->response()->status(500);
    }
});
// POST route
$app->post('/ingredients', function () use($app) {
    $request = (array) json_decode($app->request()->getBody());
    $recipe_id = $request['recipeId'];
    $name = $request['name'];
    $db = new Ingredients();
    $item = $db->add_ingredient($recipe_id, $name);
    if ($item) {
        // if successful, just return the JSON sent for use on the client-side
        $results = array('id' => $item, 'recipeId' => $recipe_id, 'name' => $name);
        $app->response()->header('Content-Type', 'application/json');
        echo json_encode($results);
    }
});
// PUT route
$app->put('/ingredients/:id', function ($id) use($app) {
    $request = (array) json_decode($app->request()->getBody());
    $name = $request['name'];
    $db = new Ingredients();
    $items = $db->update_ingredient($id, $name);
    if ($items) {
        // if successful, return the new object with the values