Skip to content

yy-cc-20/Library-Management-System-API

Repository files navigation

Library Management System (API)

This RESTful API allows clients to create, update, delete, and retrieve information about books.

Table of Contents

Features

  • 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

Technology Used

  • ASP.NET Core Web API (v8.0)
  • Entity Framework Core (v8.0.7)
  • MySQL
  • Git
  • Nlog (logging)

Installation

  1. Clone the repository
  2. Open the project in Visual Studio
  3. Install any necessary NuGet packages
  4. Create a MySQL database and insert fake data using the sql script in DatabaseMigration directory
  5. Update the database connection string, userid and password in the appsettings.json file
  6. Change the log file directory in nlog.config
  7. Run the application
  8. The API will be available at https://localhost:7030/api/book.

Design Pattern

This project follows the repository pattern to ensure maintainability and scalability.

Database Design

Category

  • id: GUID (PK)
  • name: VARCHAR(45)

Book

  • id: GUID (PK)
  • name: VARCHAR(45)
  • author: VARCHAR(45)
  • category_id: GUID (FK)

API Endpoints

Create a Book

  • 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 a Book by ID

  • 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"
    }
}

Update a Book

  • 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 a Book

  • DELETE api/book/{id}
  • Example Request: DELETE api/book/08dcad48-bd22-4a80-8215-67153f73ec8c

Get All Books

  • 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 Books with Paging

  • 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,
}

Filter Books

Get Books by Author
  • GET api/book?author={author}
  • Example Request: GET api/book?author=Jane Austen
Get Books by Category
  • GET api/book?category_id={category_id}
  • Example Request: GET api/book?category_id=289ac3de-1cf3-4a0d-8795-f6e9b48b9ddb

Search Books

  • GET api/book?searchterm={searchterm}
  • Example Request: GET api/book?searchterm=the

Sort Books

  • GET api/book?orderBy={orderByQueryString}
  • Example Request: GET api/book?orderBy=name,category_id desc

Log File

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.

About

This RESTful API allows clients to create, update, delete, and retrieve information about books.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages