This RESTful API allows clients to create, update, delete, and retrieve information about books.
- Create a new book
- Update an existing book's details
- Delete a book from the system
- Retrieve a list of books
- Retrieve details of a specific book
- Input validation
- Paging
- Sorting
- Searching
- Filtering
- Logging
- ASP.NET Core Web API (v8.0)
- Entity Framework Core (v8.0.7)
- MySQL
- Git
- Nlog (logging)
- Clone the repository
- Open the project in Visual Studio
- Install any necessary NuGet packages
- Create a MySQL database and insert fake data using the sql script in
DatabaseMigrationdirectory - Update the database connection string, userid and password in the
appsettings.jsonfile - Change the log file directory in
nlog.config - Run the application
- The API will be available at
https://localhost:7030/api/book.
This project follows the repository pattern to ensure maintainability and scalability.
Category
- id: GUID (PK)
- name: VARCHAR(45)
Book
- id: GUID (PK)
- name: VARCHAR(45)
- author: VARCHAR(45)
- category_id: GUID (FK)
- POST
api/book - Example Request:
{
"name": "Pride and Prejudice",
"author": "Jane Austeeeeen",
"category_Id": "72a17923-04a6-494b-9d3c-2c0cb161434c",
}- Example Response:
{
"$id": "1",
"id": "08dcad94-3cc1-4458-8f66-146b367bd147",
"name": "Pride and Prejudice",
"author": "Jane Austeeeeen",
"category_Id": "72a17923-04a6-494b-9d3c-2c0cb161434c",
"category": null
}Note: $id is generated by json to identified json object. It will not be recorded in database.
- GET
api/book/{id} - Example Request:
GET api/book/08dcad48-bd22-4a80-8215-67153f73ec8c - Example Response:
{
"$id": "1",
"id": "2c1541b8-3a93-47bf-9ab7-fbe807bdf68d",
"name": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"category_Id": "289ac3de-1cf3-4a0d-8795-f6e9b48b9ddb",
"category": {
"$id": "2",
"id": "289ac3de-1cf3-4a0d-8795-f6e9b48b9ddb",
"name": "Fiction"
}
}- PUT
api/book/{id} - Example Request:
{
"id": "08dcad48-bd22-4a80-8215-67153f73ec8c",
"name": "Pride and Prejudiceeeeeeeeeeee",
"author": "Jane Austennnnnnnnnnnn",
"category_Id": "72a17923-04a6-494b-9d3c-2c0cb161434c"
}- DELETE
api/book/{id} - Example Request:
DELETE api/book/08dcad48-bd22-4a80-8215-67153f73ec8c
- GET
api/book - Example Request:
GET api/book
{
"$id": "1",
"$values": [
{
"$id": "2",
"id": "08dcb822-1498-4c25-886f-6f5d803ca091",
"name": "Pride and Prejudice",
"author": "Jane Austen",
"category_Id": "72a17923-04a6-494b-9d3c-2c0cb161434c",
"category": {
"$id": "3",
"id": "72a17923-04a6-494b-9d3c-2c0cb161434c",
"name": "Romance"
}
},
{
"$id": "4",
"id": "08dcb836-d75c-4d02-86f1-792d74576a5f",
"name": "test2",
"author": "test",
"category_Id": "289ac3de-1cf3-4a0d-8795-f6e9b48b9ddb",
"category": {
"$id": "5",
"id": "289ac3de-1cf3-4a0d-8795-f6e9b48b9ddb",
"name": "Fiction"
}
},
{
"$id": "6",
"id": "2c1541b8-3a93-47bf-9ab7-fbe807bdf68d",
"name": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"category_Id": "289ac3de-1cf3-4a0d-8795-f6e9b48b9ddb",
"category": {
"$ref": "5"
}
}
]
}- GET
api/book?pageNumber={pageNumber}&pageSize={pageSize} - Query Parameters:
+
pageNumber: The page number to retrieve +pageSize: The number of books to retrieve per page - Example Request:
GET api/book?pageNumber=1&pageSize=2 - Additional Response Header:
X-Pagination: {
"CurrentPage":1,
"TotalPages":2,
"PageSize":10,
"TotalCount":14,
"HasPrevious":false,
"HasNext":true,
"OrderByAscending":true,
}- GET
api/book?author={author} - Example Request:
GET api/book?author=Jane Austen
- GET
api/book?category_id={category_id} - Example Request:
GET api/book?category_id=289ac3de-1cf3-4a0d-8795-f6e9b48b9ddb
- GET
api/book?searchterm={searchterm} - Example Request:
GET api/book?searchterm=the
- GET
api/book?orderBy={orderByQueryString} - Example Request:
GET api/book?orderBy=name,category_id desc
Log file location: LibraryManagementSystem\Logs\[Date]_logfile.txt
Example log record:
- 2024-07-26 17:25:03.2086 INFO Create book with id: 08dcad54-d3e4-4ce1-8be3-d43053e8ed09
- 2024-07-26 17:25:19.7825 INFO Returned book with id: 08dcad54-d3e4-4ce1-8be3-d43053e8ed09
- 2024-07-26 17:25:53.9957 INFO Update book with id: 08dcad54-d3e4-4ce1-8be3-d43053e8ed09
- 2024-07-26 17:26:07.3571 INFO Delete book with id: 08dcad54-d3e4-4ce1-8be3-d43053e8ed09
- 2024-07-26 17:26:25.3391 INFO Returned 13 books from database.
- 2024-07-26 17:27:02.0288 INFO Returned 1 books from database.