A comprehensive RESTful API for managing a library system, including books, authors, users, loans, and reviews.
- Installation
- Environment Variables
- Running the Application
- API Documentation
- Data Models
- Error Handling
- API Usage Examples
-
Clone the repository:
git clone <repository-url> cd library-management-api -
Install dependencies:
npm install
Create a .env file in the root directory with the following variables:
MONGO_URI=your_mongodb_connection_string
PORT=3000
MONGO_URI: MongoDB connection stringPORT: Port number for the server (defaults to 3000 if not specified)
Development mode:
npx nodemon server.js
Production mode:
npm start
The API will be available at http://localhost:3000
Swagger documentation is available at http://localhost:3000/api-docs
- Endpoint:
GET /books - Description: Retrieve all books in the library
- Query Parameters:
limit(optional): Limit the number of resultspage(optional): Page number for pagination
- Success Response:
- Code: 200 OK
- Content:
[ { "_id": 1, "title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "publishedYear": 1925, "coverImage": { "data": "...", "contentType": "image/jpeg" } }, ... ]
- Error Response:
- Code: 500 Internal Server Error
- Content:
{ "message": "Error retrieving books", "error": "..." }
- Endpoint:
GET /books/:id - Description: Retrieve a specific book by ID
- URL Parameters:
id: Book ID
- Success Response:
- Code: 200 OK
- Content:
{ "_id": 1, "title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "publishedYear": 1925, "coverImage": { "data": "...", "contentType": "image/jpeg" } }
- Error Responses:
- Code: 404 Not Found
{ "message": "Book not found" } - Code: 500 Internal Server Error
{ "message": "Error retrieving book", "error": "..." }
- Code: 404 Not Found
- Endpoint:
POST /books - Description: Add a new book to the library
- Request Body:
{ "title": "Book Title", "author": "Author Name", "publishedYear": 2023 } - Success Response:
- Code: 201 Created
- Content:
{ "_id": 3, "title": "Book Title", "author": "Author Name", "publishedYear": 2023 }
- Error Responses:
- Code: 400 Bad Request
{ "message": "Validation error", "error": "..." } - Code: 500 Internal Server Error
{ "message": "Error adding book", "error": "..." }
- Code: 400 Bad Request
- Endpoint:
PUT /books/:id - Description: Update an existing book
- URL Parameters:
id: Book ID
- Request Body:
{ "title": "Updated Title", "author": "Updated Author", "publishedYear": 2023 } - Success Response:
- Code: 200 OK
- Content:
{ "_id": 1, "title": "Updated Title", "author": "Updated Author", "publishedYear": 2023 }
- Error Responses:
- Code: 400 Bad Request
{ "message": "Validation error", "error": "..." } - Code: 404 Not Found
{ "message": "Book not found" } - Code: 500 Internal Server Error
{ "message": "Error updating book", "error": "..." }
- Code: 400 Bad Request
- Endpoint:
DELETE /books/:id - Description: Remove a book from the library
- URL Parameters:
id: Book ID
- Success Response:
- Code: 200 OK
- Content:
{ "message": "Book deleted successfully" }
- Error Responses:
- Code: 404 Not Found
{ "message": "Book not found" } - Code: 500 Internal Server Error
{ "message": "Error deleting book", "error": "..." }
- Code: 404 Not Found
- Endpoint:
POST /books/:id/upload-cover - Description: Upload a cover image for a book
- URL Parameters:
id: Book ID
- Request Body: Form data with field name
coverImagecontaining the image file - Success Response:
- Code: 200 OK
- Content:
{ "message": "Book cover uploaded", "coverImage": "..." }
- Error Responses:
- Code: 400 Bad Request
{ "message": "No file uploaded" } - Code: 404 Not Found
{ "message": "Book not found" } - Code: 500 Internal Server Error
{ "message": "Error message", "error": "..." }
- Code: 400 Bad Request
- Endpoint:
GET /authors - Description: Retrieve all authors
- Success Response:
- Code: 200 OK
- Content:
[ { "_id": 1, "name": "F. Scott Fitzgerald", "bio": "American novelist...", "birthdate": "1896-09-24T00:00:00.000Z" }, ... ]
- Error Response:
- Code: 500 Internal Server Error
- Content:
{ "message": "Error message" }
- Endpoint:
GET /authors/:id - Description: Retrieve a specific author by ID
- URL Parameters:
id: Author ID
- Success Response:
- Code: 200 OK
- Content:
{ "_id": 1, "name": "F. Scott Fitzgerald", "bio": "American novelist...", "birthdate": "1896-09-24T00:00:00.000Z" }
- Error Responses:
- Code: 404 Not Found
{ "message": "Author not found" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 404 Not Found
- Endpoint:
POST /authors - Description: Add a new author
- Request Body:
{ "name": "Author Name", "bio": "Author biography", "birthdate": "1990-01-01" } - Success Response:
- Code: 201 Created
- Content:
{ "_id": 3, "name": "Author Name", "bio": "Author biography", "birthdate": "1990-01-01T00:00:00.000Z" }
- Error Responses:
- Code: 400 Bad Request
{ "message": "Name is required" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 400 Bad Request
- Endpoint:
PUT /authors/:id - Description: Update an existing author
- URL Parameters:
id: Author ID
- Request Body:
{ "name": "Updated Name", "bio": "Updated biography", "birthdate": "1990-01-01" } - Success Response:
- Code: 200 OK
- Content:
{ "_id": 1, "name": "Updated Name", "bio": "Updated biography", "birthdate": "1990-01-01T00:00:00.000Z" }
- Error Responses:
- Code: 404 Not Found
{ "message": "Author not found" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 404 Not Found
- Endpoint:
DELETE /authors/:id - Description: Remove an author
- URL Parameters:
id: Author ID
- Success Response:
- Code: 200 OK
- Content:
{ "message": "Author deleted" }
- Error Responses:
- Code: 404 Not Found
{ "message": "Author not found" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 404 Not Found
- Endpoint:
GET /users - Description: Retrieve all users
- Success Response:
- Code: 200 OK
- Content:
[ { "_id": 1, "name": "John Doe", "email": "[email protected]" }, ... ]
- Error Response:
- Code: 500 Internal Server Error
- Content:
{ "message": "Error message" }
- Endpoint:
GET /users/:id - Description: Retrieve a specific user by ID
- URL Parameters:
id: User ID
- Success Response:
- Code: 200 OK
- Content:
{ "_id": 1, "name": "John Doe", "email": "[email protected]" }
- Error Responses:
- Code: 404 Not Found
{ "message": "User not found" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 404 Not Found
- Endpoint:
POST /users - Description: Create a new user
- Request Body:
{ "name": "User Name", "email": "[email protected]", "password": "password" } - Success Response:
- Code: 201 Created
- Content:
{ "_id": 3, "name": "User Name", "email": "[email protected]" }
- Error Responses:
- Code: 400 Bad Request
{ "message": "All fields are required" }{ "message": "Email already exists" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 400 Bad Request
- Endpoint:
PUT /users/:id - Description: Update an existing user
- URL Parameters:
id: User ID
- Request Body:
{ "name": "Updated Name", "email": "[email protected]", "password": "newpassword" } - Success Response:
- Code: 200 OK
- Content:
{ "_id": 1, "name": "Updated Name", "email": "[email protected]" }
- Error Responses:
- Code: 404 Not Found
{ "message": "User not found" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 404 Not Found
- Endpoint:
DELETE /users/:id - Description: Remove a user
- URL Parameters:
id: User ID
- Success Response:
- Code: 200 OK
- Content:
{ "message": "User deleted" }
- Error Responses:
- Code: 404 Not Found
{ "message": "User not found" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 404 Not Found
- Endpoint:
POST /users/:id/upload-profile-picture - Description: Upload a profile picture for a user
- URL Parameters:
id: User ID
- Request Body: Form data with field name
profilePicturecontaining the image file - Success Response:
- Code: 200 OK
- Content:
{ "message": "Profile picture uploaded" }
- Error Responses:
- Code: 400 Bad Request
{ "message": "No file uploaded" } - Code: 404 Not Found
{ "message": "User not found" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 400 Bad Request
- Endpoint:
GET /loans - Description: Retrieve all loans, with book and user details
- Success Response:
- Code: 200 OK
- Content:
[ { "_id": 1, "bookId": { "_id": 1, "title": "The Great Gatsby", "author": "F. Scott Fitzgerald" }, "userId": { "_id": 1, "name": "John Doe", "email": "[email protected]" }, "issueDate": "2023-06-01T10:00:00.000Z", "status": "issued" }, ... ]
- Empty Response:
- Code: 200 OK
- Content:
{ "message": "0 loans right now" }
- Error Response:
- Code: 500 Internal Server Error
- Content:
{ "message": "Error message" }
- Endpoint:
POST /loans - Description: Create a new loan record (borrow a book)
- Request Body:
{ "bookId": 1, "userId": 1 } - Success Response:
- Code: 201 Created
- Content:
{ "_id": 1, "bookId": 1, "userId": 1, "issueDate": "2023-06-01T10:00:00.000Z", "status": "issued" }
- Error Responses:
- Code: 400 Bad Request
{ "message": "Book not found" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 400 Bad Request
- Endpoint:
PUT /loans/:id - Description: Update a loan (typically used to return a book)
- URL Parameters:
id: Loan ID
- Success Response:
- Code: 200 OK
- Content:
{ "_id": 1, "bookId": 1, "userId": 1, "issueDate": "2023-06-01T10:00:00.000Z", "returnDate": "2023-06-15T10:00:00.000Z", "status": "returned" }
- Error Responses:
- Code: 404 Not Found
{ "message": "Loan not found" } - Code: 400 Bad Request
{ "message": "Loan already returned" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 404 Not Found
- Endpoint:
DELETE /loans/:id - Description: Delete a loan record
- URL Parameters:
id: Loan ID
- Success Response:
- Code: 200 OK
- Content:
{ "message": "Loan deleted and marked as returned" }
- Error Responses:
- Code: 404 Not Found
{ "message": "Loan not found" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 404 Not Found
- Endpoint:
GET /books/:id/reviews - Description: Get all reviews for a specific book
- URL Parameters:
id: Book ID
- Success Response:
- Code: 200 OK
- Content:
[ { "_id": 1, "bookId": { "_id": 1, "title": "The Great Gatsby", "author": "F. Scott Fitzgerald" }, "userId": { "_id": 1, "name": "John Doe", "email": "[email protected]" }, "rating": 5, "review": "Amazing book!", "createdAt": "2023-06-01T10:00:00.000Z", "updatedAt": "2023-06-01T10:00:00.000Z" }, ... ]
- Error Responses:
- Code: 404 Not Found
{ "message": "Review not found" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 404 Not Found
- Endpoint:
POST /books/:id/reviews - Description: Add a review for a specific book
- URL Parameters:
id: Book ID
- Request Body:
{ "userId": 1, "rating": 5, "review": "Great book!" } - Success Response:
- Code: 201 Created
- Content:
{ "_id": 1, "bookId": 1, "userId": 1, "rating": 5, "review": "Great book!", "createdAt": "2023-06-01T10:00:00.000Z", "updatedAt": "2023-06-01T10:00:00.000Z" }
- Error Responses:
- Code: 404 Not Found
{ "message": "Book not found" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 404 Not Found
- Endpoint:
PUT /reviews/:_id/:bookId - Description: Update a review
- URL Parameters:
_id: Review IDbookId: Book ID
- Request Body:
{ "rating": 4, "review": "Updated opinion: Good book but not great" } - Success Response:
- Code: 200 OK
- Content:
{ "_id": 1, "bookId": 1, "userId": 1, "rating": 4, "review": "Updated opinion: Good book but not great", "createdAt": "2023-06-01T10:00:00.000Z", "updatedAt": "2023-06-02T10:00:00.000Z" }
- Error Responses:
- Code: 404 Not Found
{ "message": "Review not found" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 404 Not Found
- Endpoint:
DELETE /reviews/:_id/:bookId - Description: Delete a review
- URL Parameters:
_id: Review IDbookId: Book ID
- Success Response:
- Code: 200 OK
- Content:
{ "message": "Review deleted" }
- Error Responses:
- Code: 404 Not Found
{ "message": "Review not found" } - Code: 500 Internal Server Error
{ "message": "Error message" }
- Code: 404 Not Found
{
_id: Number,
title: String (required),
author: String (required),
publishedYear: Number (required),
coverImage: {
data: Buffer,
contentType: String
}
}{
_id: Number,
name: String (required),
bio: String,
birthdate: Date
}{
_id: Number,
name: String (required),
email: String (required, unique),
password: String (required),
profilePicture: {
data: Buffer,
contentType: String
}
}{
_id: Number,
bookId: Number (required, reference to Book),
userId: Number (required, reference to User),
issueDate: Date (required, default: current date),
returnDate: Date,
status: String (enum: ["issued", "returned"], default: "issued")
}{
_id: Number,
bookId: Number (required, reference to Book),
userId: Number (required, reference to User),
rating: Number (required, min: 1, max: 5),
review: String (required),
timestamps: true (createdAt, updatedAt)
}All endpoints return appropriate HTTP status codes:
200- Success: The request was successfully processed201- Created: A resource was successfully created400- Bad Request: The request could not be understood or was missing required parameters404- Not Found: The requested resource could not be found500- Server Error: An error occurred on the server
Error responses include a message explaining the error and sometimes additional error details.
- Create a user:
POST /users
Content-Type: application/json
{
"name": "John Doe",
"email": "[email protected]",
"password": "password123"
}
- Add a book:
POST /books
Content-Type: application/json
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"publishedYear": 1925
}
- Create a loan (borrow the book):
POST /loans
Content-Type: application/json
{
"bookId": 1,
"userId": 1
}
- Return the book:
PUT /loans/1
- Add a review for the book:
POST /books/1/reviews
Content-Type: application/json
{
"userId": 1,
"rating": 5,
"review": "A masterpiece that captures the essence of the American Dream."
}
- Get all reviews for the book:
GET /books/1/reviews