Skip to content

danilopalmisano/vinyl_shop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Vinyl Shop

πŸ›’ A simple RESTful API for Purchases and Products πŸ›’

Table of Contents

Installation:

No dotenv Installation Required:

As long as you have a .env file placed in the root directory of your project, environment variables will be loaded automatically when you execute a script defined in your package.json file. This eliminates the need for manual installation of the dotenv package.
If you want to add any other key to the .env file, make sure you do the same in the env.ts you'll find in the utility folder.

You can access them in any file of your code importing: import { env } from "./utility/env";

Setting up .env file

Create a file named .env in the root directory of your project (usually where your package.json file is). Add key-value pairs: Each line represents a variable. The format is KEY=VALUE.
If you want to add any other key to the .env file, make sure you do the same in the env.ts you'll find in the utility folder.

how your .env file should look like:

MONGODB_URI=mongodb://localhost:"Your port number ex.= 27017, use no quotation mark"/

ACCESS_SECRET_TOKEN= Random Key no quotation mark
REFRESH_SECRET_TOKEN= Random Key no quotation mark

LOCAL_DBNAME= VinylShop_db_local
DEV_DBNAME= VinylShop_db_dev
PROD_DBNAME= VinylShop_db_prod

LOCAL_PORT= "NUMBER for ex.= 3***, use no quotation mark"
DEV_PORT= "NUMBER for ex.= 808*, use no quotation mark"
PROD_PORT= "NUMBER for ex.= 808*, use no quotation mark"

Generating Random Keys

To generate random keys to use as ACCESS/REFRESH TOKEN copy this in your the terminal: node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"

Initial Setup:

Open a terminal in your project directory and run:

npm install

This command will download and install all the necessary dependencies listed in the package.json file.

Transpilation (Run it the first time only):

Transpilation (First Time Only): After the initial installation, run:

npm run tsc

only once to transpile your TypeScript code into JavaScript. This step is only required the first time you set up the project.

Running the Server:

From then on, you can use the single command:

npm run server

to both transpile your code and start the server using Nodemon. Nodemon will automatically watch for changes in your TypeScript files and restart the server whenever you make modifications, streamlining your development workflow.

Used Technologies

  • Zod: a simple, lightweight and powerful schema validation library. Its main benefit is that it provides a very intuitive and easy to use API, making it straightforward to define complex validations.

  • Zod-Validation-Error: a library that simplifies the process of handling validation errors thrown by Zod. It provides a more user-friendly way to deal with errors, making it easier to display them in the UI.

  • Mongoose: a MongoDB object modeling tool that makes working with MongoDB easy and simple. One of its main benefits is that it provides a lot of built-in functionality that makes CRUD operations very easy, such as validation, pre/post hooks and more.

  • Bcrypt: a password hashing library that provides a simple and secure way to store passwords in a database. Its main benefit is that it uses a slow and expensive hashing algorithm, making it more secure against brute-force attacks.

Features

Documentation

Please check the Documentation for an example on how the API calls work; to use them you need to download Rest client or launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.

ext install humao.rest-client

Authentication API

Feature Endpoint Coded? PrivateRoutes
Register User POST /api/auth/register βœ” PUBLIC
Register Admin POST /api/auth/admin/register βœ” PUBLIC
Login POST /api/auth/login βœ” USER / ADMIN
Logout POST /api/auth/logout βœ” USER/ADMIN
Auth Info GET /api/auth/user βœ” USER/ADMIN

Products API

Feature Endpoint Coded? PrivateRoutes
Show all Products GET /api/products βœ” PUBLIC
Show a specific Products GET /api/products/:id βœ” PUBLIC
Add a ProductADMIN POST /api/products βœ” ADMIN
Edit a Product PUT /api/products/:id βœ” ADMIN
Delete a Product DELETE /api/products/:id βœ” ADMIN

Cart API

Feature Endpoint Coded? PrivateRoutes
Show Current User Cart GET /api/cart βœ” USER/ADMIN
Add one Item to a line POST /api/cart/add/:id βœ” USER
Remove one Item to a line DELETE /api/cart/remove/:id βœ” USER
Remove Cart DELETE /api/cart/clear βœ” USER

Order API

Feature Endpoint Coded? PrivateRoutes
Show Current User Orders GET /api/order βœ” USER/ADMIN
Show a specific Order GET /api/order/:id βœ” USER/ADMIN
Add an Order POST /api/order βœ” USER
Edit an Order PUT /api/order/:id βœ” ADMIN
Delete an Order DELETE /api/order/:id βœ” ADMIN

Interfaces

User Interface

Key Type
_id ObjectId
username String
login Login Interface
role Enum

Login Interface

Key Type
email String
password String
loggedIn Boolean

Role Enum

Value Description
0 User
1 Admin

Product Interface

Key Type
name String
description String
price Number
images [String]
stockQuantity Number
StockStatus Enum
category String

StockStatus Enum

Value Description
0 Out of Stock
1 In Stock
2 Discontinued

Cart Interface

Key Type
userId ObjectId
lines [LineItem]
totalPrice Number

LineItem Interface

Key Type
productId ObjectId
quantity Number
price Number
subtotal Number

Order Interface

Key Type
userId ObjectId
cart Cart
totalPrice Number
status Enum
shippingAddress Object
shippingAddress.name String
shippingAddress.surname String
shippingAddress.addressLine String
shippingAddress.zipCode String
shippingAddress.city String
shippingAddress.country String
shippingAddress.state String

Status Enum

Value Description
0 Order Created
1 Processing
2 Packed
3 Shipped
4 Delivered
5 Canceled

Decoded Token Interface

Key Type
id String

To be implemented

  • A system of jwt refresh token, since at the moment for pure testing reason token doesn't expire;
  • whitelist to be a collection in the database, so the list can expand or shrink for the customer needs.
  • known bug: at the moment it's possible to make an order with the same product for only a customer; it will be fixed ASAP!

Credits

This development/educational scenario was coded and created by Andrea Risiglione, Danilo Palmisano and Giada Napoli. The objective of this repository it's as practical test of RESTful API's with TypeScript, node.js, express, mongoose and zod.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published