The aim of this project was to create a RESTful API back-end for an imaginary store.
TypeScript
to reduce type errors.Postgres
for the database.Node.js
as a runtime environment.Express
for the application routing.bcrypt
for password security.dotenv
from npm for managing environment variables.db-migrate
from npm for migrations.jsonwebtoken
from npm for working with JWTs.jasmine
andsupertest
from npm for testing.
The application supports the following actions:
- User registration and authentication
- CRUD operations for Products and Orders
- Ability to add products to specific orders.
GET /users
returns all registered users (authentication required).GET /users/:id
returns the user with the specified id (authentication required).POST /users
creates a new user.PUT /users/:id
updates user details (authentication required).DELETE /users/:id
deletes the user with the specified id from the database.
GET /products
returns all the avalable products.GET /products/:id
returns the products with the specified id.POST /products
creates a new product.PUT /products/:id
updates product details (authentication required).DELETE /products/:id
deletes the product from the database (authentication required).
N.B: Authentication required for ALL of the following routes
GET /orders
returns all orders.GET /orders/:id
returns the order with the specified id.GET /orders/current/:user_id
returns the current order for the user of iduser_id
.GET /orders/completed/:user_id
returns the completed orders for the user of iduser_id
.POST /orders
creates a new order.POST /orders/:id/products
adds a new product to the current order.PUT /orders/:id
updates the current order.DELETE /orders/:id
deletes the current order.
Column | Type |
---|---|
id | integer |
first_name | string (64) |
last_name | string (64) |
password | string (varying) |
Column | Type |
---|---|
id | integer |
name | string (64) |
price | float |
Column | Type |
---|---|
id | integer |
user_id | integer |
status | string (varying) |
Intermediary table that represents the product quantities via product id in the order and does not directly correspond to any models.
Column | Type |
---|---|
id | integer |
order_id | integer |
product_id | integer |
quantity | integer |
A list of the project's dependencies may be found in the package.json
file.
- Insall the dependencies required by running
npm install
oryarn
. - Setup postgres image inside docker container
- Start docker container using
docker-compose.yml
which contains information related to postgres image sudodocker-compose up
- In a bash terminal within the docker container execute
sudo docker exec -i -t <docker_container_name> bash
- Login to the Postgres server running on port 5432 (default) by executing
psql -U <postgres_user>
- Create database for development and testing by executing
CREATE DATABASE store;
andCREATE DATABASE store_test;
respectively. - Create a new database user and grant the user access to the database.
- Connect to the database by executing
\c <database_name> <database_user>;
. - Execute
\dt
to display the tables. N.B.: Until migrations are run the output should beNo relations found
. - Please refer to Postgres documentation if in doubt.
POSTGRES_HOST=127.0.0.1
POSTGRES_PORT=5432
POSTGRES_DB=store
POSTGRES_TEST_DB=store_test
POSTGRES_USER=store_dev
POSTGRES_PASSWORD=''
POSTGRES_PASSWORD_TEST=''
ENV=dev
BCRYPT_PASSWORD=''
SALT_ROUNDS=10
TOKEN_SECRET=''
The ''
signify the parameters to be set.
The db-migrate up
run thedatabase migrations.
yarn build
ornpm run build
will produce the build directory.yarn start
ornpm run start
will start the development server.yarn watch
ornpm run watch
will start the production server.- The application should be running on port 3000.
To test the application execute the following command:
yarn test
ornpm run test
.