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.
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.
git clone https://github.com/Eadwulf/e-commerce-API
cd e-commerce-API
pipenv install && pipenv shell
This project requires a PosgreSQL database, but alternatively, you could use a Sqlite3 database.
# 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
# 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'),
},
}
psql -U postgres -d postgres
CREATE DATABASE <database_name>;
CREATE USER <username> WITH ENCRYPTED PASSWORD '<password>';
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 ALL PRIVILEGES ON DATABASE <database_name> TO <username>;
\q
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>
-
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.
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#'
Once the database has been set up as well as the environment variables, you can apply the migrations
python manage.py migrate
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.
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'...