# Books API
##Project details
Project description: An api that manages books along with authors.
Author: Nwinyinya David (the-last_php_bender)
Email: [email protected]
## Project Setup
1. **Clone the repository**:
```bash
git clone <repository_url>
cd books-api
-
Install dependencies:
npm install
-
Set up the
.env
file:MONGO_URI=your_mongodb_connection_string PORT=5000
-
Start the application:
npm start
-
Create a Book:
POST /api/books
- Request Body:
{ "title": "string", "author": "string", "publishedDate": "date", "ISBN": "string" }
- Response:
{ "_id": "string", "title": "string", "author": "string", "publishedDate": "date", "ISBN": "string" }
- Request Body:
-
Get All Books:
GET /api/books
- Response:
[ { "_id": "string", "title": "string", "author": "string", "publishedDate": "date", "ISBN": "string" } ]
- Response:
-
Get a Single Book:
GET /api/books/:id
- Response:
{ "_id": "string", "title": "string", "author": "string", "publishedDate": "date", "ISBN": "string" }
- Response:
-
Update a Book:
PUT /api/books/:id
- Request Body:
{ "title": "string", "author": "string", "publishedDate": "date", "ISBN": "string" }
- Response:
{ "_id": "string", "title": "string", "author": "string", "publishedDate": "date", "ISBN": "string" }
- Request Body:
-
Delete a Book:
DELETE /api/books/:id
- Response:
{ "message": "Book deleted successfully" }
- Response:
-
Update Book Cover:
PATCH /api/books/cover-image/:id
- Request Body: FormData with field
coverImage
- Response:
{ "_id": "string", "title": "string", "author": "string", "publishedDate": "date", "ISBN": "string", "coverImage": "string" }
- Request Body: FormData with field
To run tests:
npm test
-
Install testing libraries:
npm install jest supertest ts-jest @types/jest @types/supertest --save-dev
-
Configure Jest in
package.json
:"jest": { "preset": "ts-jest", "testEnvironment": "node", "testPathIgnorePatterns": ["/node_modules/"] }
books-api/
├── src/
│ ├── controllers/
│ │ └── bookController.ts
│ ├── models/
│ │ └── book.ts
│ ├── routes/
│ │ └── bookRoutes.ts
│ ├── middlewares/
│ │ └── upload.ts
│ ├── utils/
│ │ └── db.ts
│ └── app.ts
├── .env
├── .gitignore
├── package.json
├── README.md
└── tsconfig.json
- Ensure you have MongoDB running locally or use a cloud-based MongoDB service.
- The
uploads/
directory is where the book cover images will be stored. Ensure this directory exists or create it in your project root. - Modify the
MONGO_URI
in your.env
file to match your MongoDB connection string.