Ejemplo n.º 1
0
        return $response->withHeader('Content-Type', 'application/json')->withStatus(400);
    }
    $bookId = Uuid::fromString($requestBody['bookId']);
    $library->createReservation($reservationId, $bookId, $requestBody['email']);
    $responseBody = $response->getBody();
    $responseBody->write(json_encode(['id' => (string) $reservationId]));
    return $response->withHeader('Content-Type', 'application/json')->withStatus(201);
});
//Give away reservation for book
$app->patch('/reservations/{reservationId}', function (ServerRequestInterface $request, ResponseInterface $response, $args = []) use($library, $app, $givenAwayValidator) {
    $reservationId = Uuid::fromString($args['reservationId']);
    $requestBody = $request->getParsedBody();
    if ($givenAwayValidator($requestBody) == false) {
        return $response->withHeader('Content-Type', 'application/json')->withStatus(400);
    }
    try {
        $library->giveAwayBookInReservation($reservationId, new \DateTime($requestBody['givenAwayAt']));
    } catch (BookInReservationAlreadyGivenAway $e) {
        return $response->withHeader('Content-Type', 'application/json')->withStatus(400);
    }
    return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
});
//Give back book from reservation
$app->delete('/reservations/{reservationId}', function (ServerRequestInterface $request, ResponseInterface $response, $args = []) use($library, $app, $reservationDataValidator) {
    $reservationId = Uuid::fromString($args['reservationId']);
    try {
        $library->giveBackBookFromReservation($reservationId);
    } catch (CannotGiveBackReservationWhichWasNotGivenAway $e) {
        return $response->withHeader('Content-Type', 'application/json')->withStatus(400);
    }
    return $response->withStatus(204);
Ejemplo n.º 2
0
    return $response->withStatus(201)->withHeader("Content-Type", "application/json")->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
});
$app->get("/todos/{uid}", function ($request, $response, $arguments) {
    $todo = $this->spot->mapper("App\\Todo")->first(["uid" => $arguments["uid"]]);
    $fractal = new Manager();
    $fractal->setSerializer(new ArraySerializer());
    $resource = new Item($todo, new TodoTransformer());
    $data = $fractal->createData($resource)->toArray();
    return $response->withStatus(200)->withHeader("Content-Type", "application/json")->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
});
$app->patch("/todos/{uid}", function ($request, $response, $arguments) {
    $body = $request->getParsedBody();
    $todo = $this->spot->mapper("App\\Todo")->first(["uid" => $arguments["uid"]]);
    $todo->data($body);
    $this->spot->mapper("App\\Todo")->save($todo);
    $fractal = new Manager();
    $fractal->setSerializer(new ArraySerializer());
    $resource = new Item($todo, new TodoTransformer());
    $data = $fractal->createData($resource)->toArray();
    return $response->withStatus(200)->withHeader("Content-Type", "application/json")->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
});
$app->delete("/todos/{uid}", function ($request, $response, $arguments) {
    $todo = $this->spot->mapper("App\\Todo")->first(["uid" => $arguments["uid"]]);
    $this->spot->mapper("App\\Todo")->delete($todo);
    return $response->withStatus(204)->withHeader("Content-Type", "application/json")->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
});
/* In real life this this is probably a bad idea. */
$app->delete("/todos", function ($request, $response, $arguments) {
    $this->spot->mapper("App\\Todo")->delete();
    return $response->withStatus(204);
});
Ejemplo n.º 3
0
                </p>
                <p><a href="https://github.com/codeguy/Slim-Extras" target="_blank">Browse the Extras Repository</a></p>
            </section>
        </body>
    </html>
EOT;
    echo $template;
});
// POST route
$app->post('/post', function () {
    echo 'This is a POST route';
});
// PUT route
$app->put('/put', function () {
    echo 'This is a PUT route';
});
// PATCH route
$app->patch('/patch', function () {
    echo 'This is a PATCH route';
});
// DELETE route
$app->delete('/delete', function () {
    echo 'This is a DELETE route';
});
/**
 * Step 4: Run the Slim application
 *
 * This method should be called last. This executes the Slim application
 * and returns the HTTP response to the HTTP client.
 */
$app->run();