This is a NestJS-based backend API for managing a bookstore. It includes features for user authentication, book management, customer management, and order management. The API uses Prisma as the ORM for PostgreSQL database interactions and Swagger for API documentation.
- Node.js (14.x or higher)
- PostgreSQL (for database)
- Prisma CLI installed globally:
npm install -g prisma
-
Clone the repository:
git clone https://github.com/your-repo/bookstore-backend.git
-
Install dependencies:
npm install
-
Initialize Prisma:
npx prisma init
-
Update
.envwith your PostgreSQL connection string:DATABASE_URL="postgresql://username:password@localhost:5432/database_name" JWT_SECRET = "your_key"
-
Generate Prisma Client:
npx prisma generate
-
Apply database migrations:
npx prisma migrate dev --name init
Start the development server:
npm run start:dev- User Authentication: JWT-based authentication for secure login and registration.
- Book Management: CRUD operations for managing books, including filtering and searching.
- Customer Management: Basic CRUD operations for customer management.
- Order Management: Create, read, and cancel orders with book-order relationships.
- Swagger Documentation: Interactive API documentation using Swagger.
-
POST /auth/login: Login with email and password.
- Request Body:
{ "email": "string", "password": "string" } - Response:
{ "access_token": "string" }
Example:
curl -X POST http://localhost:3000/auth/login \ -H 'Content-Type: application/json' \ -d '{"email": "user@example.com", "password": "password123"}'
- Request Body:
-
POST /auth/register: Register a new user.
- Request Body:
{ "email": "string", "password": "string" } - Response:
{ "access_token": "string" }
Example:
curl -X POST http://localhost:3000/auth/register \ -H 'Content-Type: application/json' \ -d '{"email": "newuser@example.com", "password": "newpassword123"}'
- Request Body:
-
POST /books: Create a new book.
- Request Body:
{ "title": "string", "author": "string", "category": "string", "price": "number", "rating?": "number", "publishedAt": "Date" } - Response: Created book object. Example:
curl -X POST http://localhost:3000/books \ -H 'Content-Type: application/json' \ -d '{"title": "New Book", "author": "Author Name", "category": "Fiction", "price": 19.99, "publishedAt": "2025-04-09T00:00:00Z"}'
- Request Body:
-
GET /books: Retrieve all books.
- Response: Array of book objects. Example:
curl http://localhost:3000/books
-
POST /customers: Create a new customer.
- Request Body:
{ "userId": "number" } - Response: Created customer object. Example:
curl -X POST http://localhost:3000/customers \ -H 'Content-Type: application/json' \ -d '{"userId": 1}'
- Request Body:
-
GET /customers: Retrieve all customers.
- Response: Array of customer objects. Example:
curl http://localhost:3000/customers
-
POST /orders: Create a new order.
- Request Body:
{ "customerId": "number", "bookIds": ["number[]"] } - Response: Created order object. Example:
curl -X POST http://localhost:3000/orders \ -H 'Content-Type: application/json' \ -d '{"customerId": 1, "bookIds": [1, 2]}'
- Request Body:
-
GET /orders: Retrieve all orders.
- Response: Array of order objects. Example:
curl http://localhost:3000/orders
src/
├── app.controller.ts
├── app.module.ts
├── app.service.ts
├── auth/
│ ├── auth.controller.ts
│ ├── auth.module.ts
│ ├── auth.service.ts
│ ├── dto/
│ │ ├── login.dto.ts
│ │ └── register.dto.ts
│ ├── guards/
│ │ └── jwt-auth.guard.ts
│ └── strategies/
│ └── jwt.strategy.ts
├── books/
│ ├── books.controller.ts
│ ├── books.module.ts
│ ├── books.service.ts
│ └── dto/
│ ├── create-book.dto.ts
│ └── update-book.dto.ts
├── customers/
│ ├── customers.controller.ts
│ ├── customers.module.ts
│ ├── customers.service.ts
│ └── entities/
│ └── customer.entity.ts
├── orders/
│ ├── orders.controller.ts
│ ├── orders.module.ts
│ ├── orders.service.ts
│ └── dto/
│ └── create-order.dto.ts
├── prisma/
│ ├── prisma.module.ts
│ └── prisma.service.ts
├── users/
│ ├── users.controller.ts
│ ├── users.module.ts
│ └── users.service.ts
└── main.ts
Contributions are welcome! Please submit a pull request with a detailed description of changes.
This project is licensed under the MIT License. See LICENSE for details.