StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 2 · REST Controllers & Endpoints

Comic Book Issue API

easyREST RoutingPartial Update67%2 of 3 solved

Problem statement

Build the REST endpoints for comic book issues that live under a series, using every verb, status code and parameter type from this module.

Requirements

The Series and Issue entities, the request bodies, the ErrorResponse body and the IssueService are already provided. Your task is to write the four endpoints in IssueController. Issues live under a series, so every path starts with /series/{seriesId}/issues, and a seriesId or id that is not a number returns 400 Bad Request.

IssueService

  • seriesExists(seriesId) tells whether the series exists, and titleTaken(seriesId, title) whether it already has an issue with that title
  • findBySeries(seriesId) returns the issues of the series, and findBySeriesAndTitle(seriesId, title) only those with exactly that title
  • findInSeries(seriesId, id) returns an Optional holding the issue only when it belongs to the series
  • create(seriesId, request), save(issue) and delete(issue) create, save and delete issues

IssueController

GET /series/{seriesId}/issues

  • Return 200 OK with the issues of the series; issues of other series never appear
  • Read an optional title query parameter; when it is sent, return only the issues of the series with that title
  • Return 404 Not Found when no series has that seriesId

POST /series/{seriesId}/issues

  • Read a JSON body with title and pageCount, save the issue in the series, and return 201 Created with the saved issue
  • When the series already has an issue with that title, return 409 Conflict with an ErrorResponse whose error is "Issue already exists: " followed by the title
  • Return 404 Not Found when no series has that seriesId

PATCH /series/{seriesId}/issues/{id}

  • Read a JSON body in which title and pageCount are both optional
  • Change only the fields that are sent, keep the others, save the issue, and return 200 OK with it

DELETE /series/{seriesId}/issues/{id}

  • Delete the issue and return 204 No Content with an empty body

On PATCH and DELETE, return 404 Not Found when no issue with that id belongs to the series, even if the issue exists under another series.

Sample Requests & Responses

GET /series/1/issues

Response (200 OK):

[
  {
    "title": "Origins",
    "pageCount": 32,
    "seriesId": 1,
    "id": 1
  },
  {
    "title": "Dark Moon",
    "pageCount": 28,
    "seriesId": 1,
    "id": 2
  }
]

GET /series/1/issues?title=Dark Moon

Response (200 OK):

[
  {
    "title": "Dark Moon",
    "pageCount": 28,
    "seriesId": 1,
    "id": 2
  }
]

POST /series/1/issues

Request:

{
  "title": "Dark Moon",
  "pageCount": 30
}

Response (409 Conflict):

{
  "error": "Issue already exists: Dark Moon"
}

PATCH /series/1/issues/2

Request:

{
  "title": "Dark Moon Rising"
}

Response (200 OK):

{
  "title": "Dark Moon Rising",
  "pageCount": 28,
  "seriesId": 1,
  "id": 2
}

PATCH /series/2/issues/2

Request:

{
  "title": "Wrong Series"
}

Response (404 Not Found): empty body

DELETE /series/1/issues/2

Response (204 No Content): empty body

GET /series/99/issues

Response (404 Not Found): empty body

More in REST Controllers & Endpoints

See the module
Planetarium Show CatalogThis problem covers adding, listing, and removing shows, and what to answer when someone asks for one that isn't there.50%2 of 4 solvedeasyBird Sightings APIThis problem covers recording bird sightings and finding them by species, and what to show when nothing matches.100%2 of 2 solvedeasyVending Machine InventoryThis problem covers changing stored state from a request, and telling apart a slot that is empty from one that does not exist.100%2 of 2 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.