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

Task Tracker API With Route Groups

easy100%1 of 1 solved

Problem statement

Use everything from this module in one API: map the five task endpoints in a route group, with typed results, the right status codes, an id constraint and query filters with defaults.

Requirements

The task store, the TaskItem model and the request contracts are already provided and registered. Your task has one part: map the task endpoints in MapTaskEndpoints in TaskTrackerApi/Endpoints/TaskEndpoints.cs.

A route group, made with MapGroup(prefix), gives several endpoints one shared prefix: each endpoint mapped on the group adds only the rest of its path. TypedResults work like Results, and when a handler's return type names its results, such as Results<Ok<TaskItem>, NotFound>, the endpoint also declares the status codes it can answer with.

ITaskStore (provided)

  • GetTasks(status, minPriority) returns the matching tasks ordered by Id. The status all keeps every status
  • Find(id), Add(request), Update(id, request) and Remove(id). Update and Remove return false when no task has that id

TaskEndpoints

  • MapTaskEndpoints returns the route group for /api/v1/tasks, and all five endpoints are mapped on it, so no route repeats the prefix
  • GET /api/v1/tasks answers 200 with the tasks. status (default all) and minPriority (an int, default 1) come from the query string, and a minPriority that is not a whole number is answered with 400
  • GET /api/v1/tasks/{id} answers 200 with the task
  • POST /api/v1/tasks takes a CreateTaskRequest and answers 201 with the new task. Its Location header is the task's detail path, such as /api/v1/tasks/6
  • PUT /api/v1/tasks/{id} takes an UpdateTaskRequest, replaces the task and answers 204 with no body
  • DELETE /api/v1/tasks/{id} removes the task and answers 204 with no body
  • GET, PUT and DELETE answer 404 for an id that no task has
  • {id} only matches a whole number that fits in an int, so /api/v1/tasks/abc gets a 404 from routing
  • Each handler's return type names its typed results, so every endpoint declares its status codes
Sample Requests & Responses

GET /api/v1/tasks?status=open&minPriority=3

Response (200 OK):

[
  { "id": 3, "title": "Update onboarding guide", "status": "open", "priority": 4 },
  { "id": 5, "title": "Plan sprint retro", "status": "open", "priority": 5 }
]

GET /api/v1/tasks/2

Response (200 OK):

{ "id": 2, "title": "Review pull request", "status": "done", "priority": 1 }

POST /api/v1/tasks

Request body:

{ "title": "Book the venue", "status": "open", "priority": 3 }

Response (201 Created), with the header Location: /api/v1/tasks/6:

{ "id": 6, "title": "Book the venue", "status": "open", "priority": 3 }

PUT /api/v1/tasks/2 with { "title": "Review pull request feedback", "status": "open", "priority": 4 }

Response (204 No Content).

DELETE /api/v1/tasks/3

Response (204 No Content).

GET /api/v1/tasks/99 and GET /api/v1/tasks/abc

Response (404 Not Found) for each.

GET /api/v1/tasks?minPriority=abc

Response (400 Bad Request).

More in Minimal APIs and Routing

See the module
Book Catalog EndpointsThis problem covers mapping the list, detail and create endpoints of a resource as minimal APIs, each answering with the right status code, and pointing the caller at what was just created.100%1 of 1 solvedeasyRoute 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 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.