This repository contains a RESTful API built with Express.js and MySQL for managing a shop's inventory. The API supports CRUD (Create, Read, Update, Delete) operations on inventory items.
- Features
- Technologies Used
- API Endpoints
- Installation
- Usage
- Environment Variables
- Testing
- Contributing
- Acknowledgments
- License
- Author
- Create, Read, Update, and Delete inventory items.
- Validate request bodies to ensure data integrity.
- Check for duplicate products before adding to the inventory.
- Fetch inventory items by ID or name.
- Implement pagination for retrieving large datasets.
- Error handling for invalid requests and server errors.
- Node.js - JavaScript runtime for server-side programming.
- Express.js - Web framework for building APIs.
- MySQL - Relational database for storing inventory data.
- Sequelize - ORM for interacting with the MySQL database.
- Joi - Schema description language and data validator for JavaScript.
- dotenv - Module to load environment variables from a
.envfile.
The following endpoints are available:
| Method | Endpoint | Description |
|---|---|---|
| POST | /shop/inventory |
Add a new product to the inventory. |
| GET | /shop/inventory/all |
Retrieve all products from the inventory. |
| GET | /shop/inventory/:id |
Retrieve a product by its ID. |
| GET | /shop/inventory/name/:name |
Retrieve a product by its name. |
| PUT | /shop/inventory/:id |
Update a product by its ID. |
| DELETE | /shop/inventory/:id |
Delete a product by its ID. |
Creating a new inventory item:
POST /shop/inventory
Content-Type: application/json
{
"product_name": "Sample Product",
"product_quantity": 10,
"product_price": 100,
"product_category": "Electronics",
"product_description": "A sample product description."
}Getting All Inventory Items:
GET /shop/inventory/allgit clone https://github.com/Pirate-Emperor/Trolley_APIcd Trolley_APInpm install- Create a MySQL database for the inventory.
- Update your database connection settings in the Sequelize configuration.
Create a .env file in the root directory and add your database credentials:
DB_HOST=localhost
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_NAME=your_db_name
npm startYou can test the API using tools like Postman or cURL. Make sure to send requests with the appropriate HTTP methods and payloads as described in the API endpoints section. I have included a Postman collection in the postman folder for your convenience.
To run tests for this API, you can use tools like Mocha and Chai. Here’s how to run the tests:
- Install the testing dependencies:
npm install --save-dev mocha chai-
Create a
testdirectory and add your test files. -
Run the tests:
npx mocha testContributions are welcome! If you have suggestions for improvements or new features, feel free to open an issue or submit a pull request. Please follow these steps:
- Fork the repository.
- Create your feature branch (
git checkout -b feature/YourFeature). - Commit your changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature/YourFeature). - Open a pull request.
- Express.js - For making API development easy.
- Sequelize - For providing a simple ORM for MySQL.
- Joi - For schema validation.
- Postman - For testing APIs effortlessly.
This project is licensed under the MIT License. See the LICENSE file for details.
Pirate-Emperor
- GitHub: Pirate-Emperor
- Reddit: PirateKingRahul
- Twitter: PirateKingRahul
- Discord: PirateKingRahul
- LinkedIn: PirateKingRahul
- Skype: Join Skype
- Medium: PirateKingRahul