Skip to content

danaelshrbiny10/ecommerce

Repository files navigation

Ecommerce Django Project

python Code style: black pydocstyle

This project is an e-commerce web application built using Django and Django Rest Framework. It provides functionality for user registration, authentication, product listing, cart management, order creation, and order viewing. The project utilizes PostgreSQL as the database backend and JWT authentication for user authentication. API endpoints are documented using Swagger.

Table of Contents

Installation

To get started with the e-commerce project:

  1. Clone the repository to your local machine
git clone https://github.com/danaelshrbiny10/ecommerce.git
  1. Create a virtual environment and activate it:
# Create a virtual environment
python3 -m venv ecommerce-venv

# Activate the virtual environment
source ecommerce-venv/bin/activate
  1. Install the project dependencies:
pip install -r requirements.txt
  1. Configure the database backend: use PostgreSQL, update the DATABASES settings in ecommerce/settings.py with your PostgreSQL database configuration.
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get("db_name"),
        'USER': os.environ.get("db_user"),
        'PASSWORD': os.environ.get("db_password"),
        'HOST': os.environ.get("db_host"),
        'PORT': os.environ.get("db_port"),
    }
}
  1. Apply the database migrations:
python manage.py makemigrations
python manage.py migrate
  1. Create a superuser (admin) account:
python manage.py createsuperuser
  1. Start the development server:
python manage.py runserver

The e-commerce web application should now be running locally at http://localhost:80/.

Usage

You can use this postman collection to learn more about the API usage

  1. User Registration:

To register a new user, send a POST request to /api/register/ with the required user information (username, password, email, phone_number, shipping_address, first_name, and last_name.).

  1. User Login:

To log in, send a POST request to /api/login/ with the user credentials (username and password). This will return a JSON response containing a JWT token.

  1. Product Listing:

To view a list of all products, send a GET request to /api/products/. The products will be displayed in ascending order of price.

  1. Searching Products:

To search for products by name, send a GET request to /api/products/?name=<name-query>. Replace <search-query> with the name of the product you want to search for.

  1. Adding Products to Cart:

To add a product to the cart, send a POST request to /api/cart/ with the product ID in the request body.

  1. Viewing Cart:

To view the cart, send a GET request to `/api/cart/. This will display the products currently in the cart along with the authenticated user details.

  1. Creating an Order:

To create an order with the products in the cart, send a POST request to /api/orders/, This will create a new order using the products in the cart and clear the cart.

  1. Viewing Orders:

To view the orders placed by you, send a GET request to /api/orders/. This will display a list of orders with their details that have been created by the authenticated user.

API Documentation

The API endpoints are documented using Swagger. To access the API documentation:

  1. Start the development server
python manage.py runserver
  1. Open your web browser and navigate to http://localhost:8000/swagger/ or http://localhost:8000/redoc/

Database Backend

use PostgreSQL and update the DATABASES settings in ecommerce/settings.py with your PostgreSQL database configuration.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get("db_name"),
        'USER': os.environ.get("db_user"),
        'PASSWORD': os.environ.get("db_password"),
        'HOST': os.environ.get("db_host"),
        'PORT': os.environ.get("db_port"),
    }
}

JWT Authentication

This project uses JWT (JSON Web Token) authentication for user authentication. JWT is a widely adopted standard for securing API endpoints and provides a stateless mechanism for authentication and involves the following components:

  1. Token-Based Authentication: Instead of traditional session-based authentication, JWT authentication relies on tokens. When a user logs in or registers, a JWT token is generated and returned to the client.
  2. Token Verification: On subsequent requests, the client includes the JWT token in the request headers to authenticate itself. The server verifies the token to authenticate and authorize the user.
  3. Token Expiration: JWT tokens have an expiration time, typically set to a short duration for security reasons. Once expired, the token is no longer valid and the client needs to obtain a new token.

To authenticate requests using JWT tokens, include the token in the request headers. The token should be included in the Authorization header using the Bearer scheme

Bearer <jwt-token>

Technologies

The application is built with the following technologies:

  • Django
  • Django Rest Framework
  • Docker
  • Docker Compose
  • Nginx

License Information

This project is licensed under the MIT License. For more details, see the LICENSE file.