Problem statement
Map the list, detail and create endpoints of a book catalog as minimal APIs, each answering with the right status code.
Requirements
The Book model, the CreateBookRequest contract and the IBookCatalog store are already provided and registered. Your task has one part: map the three endpoints in MapBookEndpoints in BookCatalogApi/Endpoints/BookEndpoints.cs.
A minimal API maps a URL straight to a small function with MapGet or MapPost. The function's parameters are filled from the route, the body or the registered services, and it returns a result such as Results.Ok(value), Results.NotFound() or Results.Created(location, value), which sets the status code.
IBookCatalog (provided)
- GetAll() returns every book, ordered by Id
- Find(id) returns the book with that id, or null
- Add(request) stores a new book with the next free id and returns it
BookEndpoints
- GET /books answers 200 with every book
- GET /books/{id} answers 200 with that book, or 404 when no book has that id
- POST /books takes a CreateBookRequest body, adds the book, and answers 201 with the new book. Its Location header is the new book's detail path, such as /books/6
Sample Requests & Responses
GET /books
Response (200 OK):
[
{ "id": 1, "title": "The Silent Cartographer", "author": "Marisol Vane", "publishedYear": 1998 },
{ "id": 2, "title": "Harbor Lights", "author": "Tobias Renn", "publishedYear": 2004 },
{ "id": 3, "title": "Notes on Rain", "author": "Ada Fenwick", "publishedYear": 2011 },
{ "id": 4, "title": "The Copper Orchard", "author": "Ivan Petrescu", "publishedYear": 2016 },
{ "id": 5, "title": "Every Third Sunday", "author": "Naomi Blackwood", "publishedYear": 2021 }
]
GET /books/3
Response (200 OK):
{ "id": 3, "title": "Notes on Rain", "author": "Ada Fenwick", "publishedYear": 2011 }
GET /books/99
Response (404 Not Found).
POST /books
Request body:
{ "title": "The Lantern Ledger", "author": "Priya Raghavan", "publishedYear": 2024 }
Response (201 Created), with the header Location: /books/6:
{ "id": 6, "title": "The Lantern Ledger", "author": "Priya Raghavan", "publishedYear": 2024 }
