StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 3 · Minimal APIs and Routing

Book Catalog Endpoints

easy100%1 of 1 solved

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 }

More in Minimal APIs and Routing

See the module
Route Constraints For Order LookupThis problem covers adding route constraints, so an order number only matches a whole number and a tracking code only matches a GUID, and a badly formed URL is turned away by routing before it reaches the handler.100%1 of 1 solvedeasySearch Endpoint Query BindingThis problem covers taking search filters from the query string as typed handler parameters, each falling back to a default when the caller leaves it out, and turning away a value of the wrong type.100%1 of 1 solvedeasyTask Tracker API With Route GroupsThis problem covers mapping the five endpoints of a task tracker in one route group with typed results, so listing can be filtered, creating points at what it made, and updating or deleting something that is not there says so.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.