- Table of Contents
- Project Overview
- Features
- Tech Stack
- Installation
- API Reference
- Service Repository Pattern
- Directory Structure
- Testing
- Contributing
- License
This project implements a shopping cart functionality for a shopping system using Laravel. It is designed as a RESTful API that follows the Service Repository pattern, ensuring a clean architecture and separation of concerns. The API allows users to manage their shopping carts, including adding products, viewing cart contents, and checking out.
- User Authentication: Users can register and log in to manage their carts.
- Cart Management: Users can create, update, and delete carts.
- Product Management: Users can add products to their carts and view the cart contents.
- Checkout Process: Users can proceed to checkout, providing necessary details for order processing.
- RESTful API: All functionalities are accessible via a RESTful API.
Laravel: PHP framework for building the API.
Eloquent ORM: For database interactions.
Service Repository Pattern: To separate business logic from data access logic.
MySQL: Database for storing user and cart data.
Prerequisites
- PHP 7.4 or higher
- Composer
- MySQL
- Laravel Installer
Steps to Install
1- Clone the Repository:
git clone https://github.com/mhdGit402/Shopping-Cart-API.git
cd laravel-shopping-cart-api2- Install Dependencies:
composer install3- Set Up Environment:
- Copy the
.env.examplefile to.env
cp .env.example .env- Update the
.envfile with your database credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password4- Generate Application Key:
php artisan key:generate5- Run Migrations:
php artisan migrate6- Seed the Database (Optional):
If you want to populate the database with sample data, run:
php artisan db:seed7- Start the Development Server:
php artisan serveThe application will be available at http://127.0.0.1:8000.
1- Create User
- Endpoint:
POST/user - Description: Creates a new user in the system.
- Request Body:
{
"name": "John Doe",
"email": "john@example.com",
"password": "securepassword"
}- Response:
- 201 Created: Returns the created user object with API token.
- 400 Bad Request: If validation fails.
2- Get User Token
-
Endpoint:
GET/user/{email} -
Description: Retrieves the API token for a specific user.
-
Authorization: Requires
view tokenspermission. -
Response:
- 200 OK: Returns the user's API token.
- 403 Forbidden: If the user does not have permission.
1- Resource Routes for Products
-
Endpoint:
/product -
Description: Retrieves the API token for a specific user.
-
Methods:
GET /product: List all products.POST /product: Create a new product.GET /product/{id}: Retrieve a specific product.
-
Response:
- 200 OK: Returns the product data.
- 201 Created: Returns the created product data.
- 404 Not Found: If the product does not exist.
1- View All Carts
-
Endpoint:
GET /cart -
Description: Lists all carts.
-
Authorization: Requires
view all cartspermission -
Response:
- 200 OK: Returns an array of carts.
- 403 Forbidden: If the user does not have permission.
2- Get Specific Cart
-
Endpoint:
GET /cart/{id} -
Description: Retrieves a specific cart by ID.
-
Response:
- 200 OK: Returns the cart data.
- 404 Not Found: If the cart does not exist.
3- Add to Cart
- Endpoint:
POST /cart/add - Description: Adds a product to the user's cart.
- Request Body:
{
"product_id": 1,
"quantity": 2
}- Response:
- 200 OK: Returns the updated cart.
- 404 Not Found: If the product does not exist.
4- Remove from Cart
- Endpoint:
DELETE /cart/remove - Description: Removes a product from the user's cart.
- Request Body:
{
"product_id": 1
}- Response:
- 200 OK: Returns the updated cart.
- 404 Not Found: If the product does not exist in the cart.
This project utilizes the Service Repository pattern to separate the business logic from the data access logic. Each service handles the business rules, while the repository manages the data interactions with the database.
Directory Structure
- app/Models: Contains Eloquent models.
- app/Repositories: Contains repository classes for data access.
- app/Services: Contains service classes for business logic.
- app/Http/Controllers: Contains API controllers.
To run the tests, use the following command:
php artisan testContributions are welcome! Please fork the repository and submit a pull request for any enhancements or bug fixes.
This project is licensed under the MIT License. See the LICENSE file for details.