You already have a working Books API.
Your task is to extend it by adding a few new endpoints and improving existing ones.
Note that there is no need for having a Database or adding any external packages. All you need to do is create some new .cs files.
Current behavior:
GET /books/{id} always returns the first book.
Your task is to update this endpoint so that:
-
It returns the book with the matching id
-
If the book does not exist, return 404 Not Found
📌 Hint:
Use FirstOrDefault from LINQ.
Create a new endpoint: PUT /books/{id}
Behavior:
-
Accepts a
BookRequestfrom the body -
Updates the Title and Category of the existing book
-
If the book does not exist → return 404 Not Found
-
If the category is invalid → return 400 Bad Request
-
If successful → return 204 No Content
📌 Rules:
-
The Id must not be changed
-
Reuse the same allowed categories logic
Create a new endpoint: GET /books/category/{category}
Behavior: Behavior:
-
Returns all books that belong to the given category
-
If no books exist in that category → return an empty list
-
Category matching should be case-insensitive. (i.e sending
GET /books/category/novelshould behave the same as if we sendGET /books/category/NOvel)
Improve the existing POST endpoint: POST /books
New Rule:
- If a book with the same title already exists → return 400 Bad Request.
- Titles should be compared case-insensitively
You are going to extend the API by introducing Authors. This task focuses on:
- Creating a new model class.
- Creating a new controller.
- Practicing basic CRUD operations.
You need to create a new Author class as below:
public class Author
{
public int Id { get; set; }
public string Name { get; set; }
}Create a new controller called AuthorsController with following details:
- Route:
/authors. - Use
[ApiController]. - Inherit from
ControllerBase. - Store authors in a static in-memory list.
- Have some initial data. Start with at least 2 authors in the list, example below:
public static List<Author> authors = new List<Author>(){
new Author { Id = 1, Name = "George Orwell" }
new Author { Id = 2, Name = "Yuval Noah Harari" }
};- It should return all authors in the list.
- Returns a single author by
id. - If not found → return 404
Not Found.
- Accepts an
AuthorRequestobject from the request body (you need to create this class and it should contain only a stringNameproperty) - Automatically generates a new Id.
- Basic validation: If
Nameis empty or shorter than 3 characters → return 400Bad Request - If successful → return 201
Created
- Deletes the author with the given id.
- If the author does not exist → return 404
Not Found. - If deleted → return 204
No Content.