The present application is meant to be a complete REST API utilizing Express framework and TypeScript along with MongoDB for managing Book Store. It handles products/articles or books along with orders to be included in a CRUD inventory, as well as revenues from the sales. Mongoose is being used for defining the schema relevant to the data integrity validated by built-in rules.
- Create, Update, Retrieve and Delete books (products)
- Look up books by their respective ID
- Order books, decrement inventory as orders come in
- All income generated from orders using MongoDB aggregation
- Data integrity with Mongoose built-in validators for validating schema
- Node.js with Express.js
- TypeScript
- MongoDB with Mongoose
- Mongoose built-in validators (e.g., required, min, enum, maxlength)
Before you begin, ensure you have the following installed:
- Node.js (v14 or higher)
- MongoDB (locally or use MongoDB Atlas)
-
Clone this repository:
git clone https://github.com/aro-arko/Book-Shop-Server.git
-
Install Dependencies:
cd book-shop-api npm install
-
Set up environment variables: Create a .env file in the root of the project and configure it as needed (e.g., MongoDB connection URL).
-
Start the application:
npm run dev
This will start the server at
http://localhost:5000/
If you want to use our online live server, here is the link:
https://book-shop-server-api.vercel.app/
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"price": 10,
"category": "Fiction",
"description": "A story about the American dream.",
"quantity": 100,
"inStock": true
}
{
"message": "Book created successfully",
"success": true,
"data": {
"_id": "648a45e5f0123c45678d9012",
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"price": 10,
"category": "Fiction",
"description": "A story about the American dream.",
"quantity": 100,
"inStock": true,
"createdAt": "2024-11-19T10:23:45.123Z",
"updatedAt": "2024-11-19T10:23:45.123Z"
}
}
{
"message": "Books retrieved successfully",
"status": true,
"data": [
{
"_id": "648a45e5f0123c45678d9012",
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"price": 10,
"category": "Fiction",
"description": "A story about the American dream.",
"quantity": 100,
"inStock": true,
"createdAt": "2024-11-19T10:23:45.123Z",
"updatedAt": "2024-11-19T10:23:45.123Z"
}
]
}
{
"message": "Book retrieved successfully",
"status": true,
"data": {
"_id": "648a45e5f0123c45678d9012",
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"price": 10,
"category": "Fiction",
"description": "A story about the American dream.",
"quantity": 100,
"inStock": true,
"createdAt": "2024-11-19T10:23:45.123Z",
"updatedAt": "2024-11-19T10:23:45.123Z"
}
}
{
"price": 15,
"quantity": 25
}
{
"message": "Book updated successfully",
"status": true,
"data": {
"_id": "648a45e5f0123c45678d9012",
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"price": 15,
"category": "Fiction",
"description": "A story about the American dream.",
"quantity": 25,
"inStock": true,
"createdAt": "2024-11-19T10:23:45.123Z",
"updatedAt": "2024-11-19T11:00:00.000Z"
}
}
{
"message": "Book deleted successfully",
"status": true,
"data": {}
}
{
"email": "customer@example.com",
"product": "648a45e5f0123c45678d9012",
"quantity": 2,
"totalPrice": 30
}
{
"message": "Order created successfully",
"status": true,
"data": {
"_id": "648b45f5e1234b56789a6789",
"email": "customer@example.com",
"product": "648a45e5f0123c45678d9012",
"quantity": 2,
"totalPrice": 30,
"createdAt": "2024-11-19T12:00:00.000Z",
"updatedAt": "2024-11-19T12:00:00.000Z"
}
}
{
"message": "Revenue calculated successfully",
"status": true,
"data": {
"totalRevenue": 450
}
}
The error responses follow a consistent format
{
"message": "Validation failed",
"success": false,
"error": {
"name": "ValidationError",
"errors": {
"price": {
"message": "Price cannot be negative",
"name": "ValidatorError",
"properties": {
"message": "Price cannot be negative",
"type": "min",
"min": 0
},
"kind": "min",
"path": "price",
"value": -5
}
}
},
"stack": "Error: Something went wrong\n at app.js:23:13\n at..."
}
- Special thanks to MongoDB and Mongoose for their robust database solutions.
- Thanks to Express.js for simplifying the development of the REST API.