/project-trybesmith

Medieval items store API developed using TypeScript, where it is possible to do a CRUD (Create, Read, Update and Delete) in the MySQL database.

Primary LanguageTypeScript

Welcome to the Trybesmith project repository!!

Introduction

API developed to manage a fictional store of medieval items, using TypeScript and following REST principles.

Among the features, you can register a new user, login to get the token that is needed to manipulate the database, list products and add new products, list orders and add new orders.

Basically, it is possible to perform the basic operations that can be done on a given database: Creation, Reading, Update and Deletion (CRUD) in the MySQL database.


Installation

Installation instructions

Clone the repository

git clone git@github.com:lucas-da-silva/project-trybesmith.git

Enter the repository

cd project-trybesmith

Climbing the containers (docker is needed)

docker-compose up -d

Entering the Node.js container

docker exec -it trybesmith bash

Install dependencies

npm install

Run the mysql script Trybesmith.sql

Copy and paste into an SQL tab or open the script and run it.

It will create the database, the tables and insert the values.

Run the application

npm start

You can use Thunder Client or Insomnia (or whatever) to check API routes.

To stop containers

docker-compose down

Aplication

As the project was developed using TypeScript, functions, objects and function parameters were typed, ensuring greater readability and functionality of the application.

The MSC (Model, Service, Controller) software architecture was used to organize the project structure.

The jwt library was also used to generate a token, which is necessary to have complete access to the application.

There are multiple validations to perform a request, from middlewares and functions dedicated to validations, so pay attention to what is expected when making a request.

Products routes

  • GET /products: lists all products in the database;

  • POST /products: register a new product;

The request body should follow the format below
  {
    "name": "Espada longa",
    "amount": "30 peças de ouro"
  }

Users routes

  • POST /users: adds a new user to the database and returns a token;
The request body should follow the format below
{
  "username": "MAX",
  "vocation": "swordsman",
  "level": 10,
  "password": "SavingPeople"
}

Login routes

  • POST /login: with valid username and password (registered in the database) returns a token;
The request body should follow the format below
  {
    "username": "string",
    "password": "string"
  }

Orders routes

  • GET /orders: lists all orders in the database;

  • POST /orders: adds a new order to the database and updates the products with the created order;

To register new orders, it is necessary to have a token in the Authorization key in the request.

The request body should follow the format below
  {
    "productsIds": [1, 2]
  }
  • The values that are in the array will be inserted in the products (table products) in the order_id column;

Technologies used

TypeScript Express MySQL Node.js JWT/JSON Web Token Docker