pushHandler(new StreamHandler('php://stdout', Logger::DEBUG)); // Prepare Slim framework and use Error Middleware to get better errors. $app = AppFactory::create(); $app->addErrorMiddleware(true, true, true); // Mock of data store. $books = [ ['id' => 1, 'title' => 'Clean Code', 'author' => 'Robert C. Martin'], ['id' => 2, 'title' => 'The Pragmatic Programmer', 'author' => 'David Thomas'], ['id' => 3, 'title' => 'Designing Data-Intensive Applications', 'author' => 'Martin Kleppmann'], ['id' => 4, 'title' => 'Clean Architecture', 'author' => 'Robert C. Martin'], ]; $app->get('/', function (Request $request, Response $response, $args) { $response->getBody()->write("Book library\nIt is working!"); return $response ->withHeader('Content-Type', 'text/html') ->withStatus(200); }); // Task 1: Read route handling carefully. // The navigator's first job is to understand what is happening here // and explain it to the driver. $app->get('/api/v1/books', function (Request $request, Response $response, $args) use ($books, $logger) { $logger->info('GET /books — returning all books', ['count' => count($books)]); $response->getBody()->write(json_encode($books, JSON_PRETTY_PRINT)); return $response ->withHeader('Content-Type', 'application/json') ->withStatus(200); }); // Task 2: GET /books/{id} // $app->get('/api/v1/books/{id}', function (...) use (...) { ... }); // Return the matching book as JSON. // If the id does not exist, return a proper 404 response with a JSON error body // Log a warning when a book is not found. // Task 3: POST /books // $app->post('/api/v1/books', function (...) use (...) { ... }); // Parse the JSON request body and add a new book to the array, assigning it the next available id. // Return the created book with a 201 status. // Log the addition at info level including the new book's title. // The operation wil not be persistent. // Task 4: DELETE /api/v1/books/{id} // $app->delete('/api/v1/books/{id}', function (...) use (...) { ... }); // Remove the book if it exists and return 204. Return 404 if not. // Log both outcomes. // The operation wil not be persistent. // Task 5: Add support for filtering by title into GET /api/v1/books/{id} // The query /api/v1/books?title=Clean should return all books that contain // "clean" in their title. The function must be case insensitive. $app->run();