Skip to content

Latest commit

 

History

History
255 lines (185 loc) · 5.76 KB

README.md

File metadata and controls

255 lines (185 loc) · 5.76 KB

Description

A RESTFul API for an e-commerce app.

This project has been developed with Python using Django and Django Rest Framework. It is powered by a PostgreSQL database.

It has a CI workflow defined with GitHub Actions which sets up and tests the code committed to this GitHub repo, subsequently, it builds and pushes a Docker Image to Docker Hub.

This RESTFul API features Jason Web Token authentication and several endpoints with defined levels of authorization based on the requesting user's role.

The project's endpoints allow performing CRUD operations on the project's database models which are:

  • Accounts
  • Products
  • Inventory
  • Categories
  • Shopping Cart
  • Shopping Cart Items
  • Orders
  • Order Items
  • Reviews

The project is being structured as a three-tiers architecture which consists of:

  • Data Tier: PostgreSQL database
  • Presentation Tier: Netx.JS Application
  • Backend Tier: Django Application (This API)

The presentation tier (front-end) can be found in the E-Commerce-Next.js repo.

API Documentation

This API has been documented using Postman. The documentation provides extensive information on how to work with each of the endpoints that the API offers. Find the documentation in this link.

Installation

Clone The Repository

git clone https://github.com/Eadwulf/e-commerce-API

Change Directory

cd e-commerce-API

Install The Dependencies And Activate The Virtual Environment

pipenv install && pipenv shell
💡 Make sure to install pipenv on your system

Database Setup

This project requires a PosgreSQL database, but alternatively, you could use a Sqlite3 database.

Setup With Sqlite3 Database

Change The Database Configurations In The config/settings.py

# config/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

If you decided to set up the Sqile3 database, skip the next section and jump to the Environment Variables section

Setup With PostgreSQL Database

The Database Settings Are Set As Follows

# config/settings.py

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': env('POSTGRES_DB'),
    'HOST': env('POSTGRES_HOST'),
    'PORT': env('POSTGRES_PORT'),
    'USER': env('POSTGRES_USER'),
    'PASSWORD': env('POSTGRES_PASSWORD'),
    },
}
💡 If you do not have an existing database and user to use with these settings, follow the instructions below and create new ones.

Enter Into The PostgreSQL Prompt

psql -U postgres -d postgres

Create The Database

CREATE DATABASE <database_name>;

Create The User

CREATE USER <username> WITH ENCRYPTED PASSWORD '<password>';

Modifying User's Parameters

ALTER ROLE <database_user> SET client_encoding TO 'utf8';
ALTER ROLE <database_user> SET default_transaction_isolation TO 'read committed';
ALTER ROLE <database_user> SET timezone TO 'UTC';

Grant Permissions To The User

 GRANT ALL PRIVILEGES ON DATABASE <database_name> TO <username>;

Exit The Prompt

\q

Environment Variables

Create The Environment Variables File (.env)

In the root directory (e-commerce-API/), create the .env file and add to it the following

DEBUG=<boolean_value>
SECRET_KEY=<your_django_api_key>
POSTGRES_DB=<your_database_name>
POSTGRES_HOST=<your_database_host>
POSTGRES_PORT=<your_database_port>
POSTGRES_USER=<your_database_user>
POSTGRES_PASSWORD=<your_database_password>
💡 Note:
  • django-environ is required to load the environment variables in the `config/settings.py` file. Such dependency should be installed by running pipenv install

  • If you are setting a Sqlite3 database instead of a PostgreSQL, don't include the environment variables for the database as they are not required when working with Sqlite3.


Generate The SECRET_KEY

To run the project, you will need to set a Django secret key to the SECRET_KEY environment variable. Create one by running

$ python manage.py shell

Once in the Django Shell

>>> from django.core.management.utils import get_random_secret_key

>>> get_random_secret_key()

It will output a key such as

'30p0cw(#l0z7%2ao7t)%!%h+(v3y+6(#=vbj8x&-snly(#(pu#'
💡 Add the key to the corresponding environment variable. Don't forget to remove the single quotation marks (') at the beginning and the end of the key

Migrations

Once the database has been set up as well as the environment variables, you can apply the migrations

python manage.py migrate

Tests

The project includes more than eighty (80) tests that ensure its correct functioning. The tests cover constraints, business logic, permission, and authorization.

All the endpoints and their allowed HTTP methods are tested.

Run the tests

python manage.py test

Example output

Found 88 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
........................................................................................
----------------------------------------------------------------------
Ran 88 tests in 54.432s

OK
Destroying test database for alias 'default'...

Entity Relationship Diagram

Database ERD