DaniPoletto / node-with-express

Simple nodejs api project of a english school

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Node with Express

🚧 Project under construction 🚧

This project is a example of an API for a English school. The database schema is shown below:

Imagem

Technologies used:

  • NodeJs
  • Express
  • Sequelize
  • MySql

Initializing the project

  1. Install dependencies
npm install
  1. Run server in server's folder:
npm start

Routes

1. People

1.1. Get all people

Method Route Description BODY PARAMS QUERY PARAMS
GET /people Return a list of all people - -

Imagem

1.2. Store

Method Route Description BODY PARAMS QUERY PARAMS
POST /people Store a person
{
"name": "Pedro",
"active": true,
"email": "testando@teste.com.br",
"role": "aluno"
}
-
1.2.1. Fields
Name Type Description
name string required
active boolean required
email string required
role string required

Imagem

1.3 Get a person

Method Route Description BODY PARAMS QUERY PARAMS
GET /person/{id} Return a person's information by id - -

Video

1.4 Update a person

Method Route Description BODY PARAMS QUERY PARAMS
PUT /person/{id} Update a person's information by id
{
"name": "Pedro",
"active": true,
"email": "testando@teste.com.br",
"role": "aluno"
}
-
1.4.1 Fields
Name Type Description
name string required
active boolean required
email string required
role string required

Video

1.5 Delete a person

Method Route Description BODY PARAMS QUERY PARAMS
DELETE /people/{id} Delete a person by id - -

Video

2. Levels

2.1. Get all levels

Method Route Description BODY PARAMS QUERY PARAMS
GET /levels Return a list of all levels - -

Imagem

2.2. Store

Method Route Description BODY PARAMS QUERY PARAMS
POST /levels Store a level
{
"description": "basic"
}
-
2.2.1. Fields
Name Type Description
description string required

Imagem

2.3 Get a level

Method Route Description BODY PARAMS QUERY PARAMS
GET /levels/{id} Return the level's information by id - -

Video

2.4 Update a level

Method Route Description BODY PARAMS QUERY PARAMS
PUT /levels/{id} Update the levels's information by id
{
"description": "master"
}
-
2.4.1 Fields
Name Type Description
description string required

Video

2.5 Delete a level

Method Route Description BODY PARAMS QUERY PARAMS
DELETE /levels/{id} Delete a level by id - -

Video

3. Classes

3.1. Get all classes

Method Route Description BODY PARAMS QUERY PARAMS
GET /classes Return a list of all classes - -

Imagem

3.2. Store

Method Route Description BODY PARAMS QUERY PARAMS
POST /classes Store a class
{
"start_date": "2023-07-01",
"teacher_id": 6,
"level_id": 1
}
-
3.2.1. Fields
Name Type Description
start_date string "Y-m-d" required
teacher_id integer required
level_id integer required

Imagem

3.3 Get a class

Method Route Description BODY PARAMS QUERY PARAMS
GET /classes/{id} Return the class's information by id - -

Video

3.4 Update a class

Method Route Description BODY PARAMS QUERY PARAMS
PUT /classes/{id} Update the levels's information by id
{
"start_date": "2023-07-01",
"teacher_id": 6,
"level_id": 1
}
-
3.4.1 Fields
Name Type Description
start_date string "Y-m-d" required
teacher_id integer required
level_id integer required

Video

3.5 Delete a class

Method Route Description BODY PARAMS QUERY PARAMS
DELETE /classes/{id} Delete a class by id - -

Video

4. Enrollments

4.1. Get a enrollment of a person

Method Route Description BODY PARAMS QUERY PARAMS
GET /people/{person_id}/enrollments/{enrollment_id} Return the enrollment's information of a person - -

Imagem

4.2. Store

Method Route Description BODY PARAMS QUERY PARAMS
POST /{person_id}/enrollments Store a enrollment for a person
{
"status" : "confirmed"
,"class_id": 4
}
-
4.2.1. Fields
Name Type Description
status string required
class_id integer required

Imagem

4.3 Update a enrollment of a person

Method Route Description BODY PARAMS QUERY PARAMS
PUT /{person_id}/enrollments/{enrollment_id} Update the enrollment's information of a person by id
{
"status" : "confirmed"
,"class_id": 4
}
-
4.3.1 Fields
Name Type Description
status string required
class_id integer required

Video

4.4 Delete a enrollment of a person

Method Route Description BODY PARAMS QUERY PARAMS
DELETE /{person_id}/enrollments/{enrollment_id} Delete a enrollment of a person by id - -

Video

Getting started with Node.js

npm init -y

Libraries

Express

Body-parser

Installing Nodemon Library

npm install nodemon -D

Database

Installing Mysql

npm install mysql2

Installing ORM

npm install sequelize sequelize-cli path
npx sequelize-cli init

Creating file .sequelizerc

https://sequelize.org/docs/v6/other-topics/migrations/#the-sequelizerc-file

Creating models

npx sequelize-cli model:generate --name People --attributes name:string,active:boolean,email:string,role:string

Running migrations

npx sequelize-cli db:migrate

Creating migration to add a colunm

npx sequelize-cli migration:create --name modify_users_add_new_fields

Creating seeds

npx sequelize-cli seed:generate --name people

Running all seeds

npx sequelize-cli db:seed:all

Creating a default scope

In People's Model:

defaultScope: {
      where: {
        active: true,
      }
    }

Especific scopes

scopes: {
      all: {
        where: {}
      }
    },

Validation

In the model:

People.init({
    name: DataTypes.STRING,
    active: DataTypes.BOOLEAN,
    email: {
      type: DataTypes.STRING,
      validate: {
        isEmail: {
          args: true,
          msg: 'Invalid E-mail'
        }
      }
    },
    role: DataTypes.STRING
  }
    name: {
      type: DataTypes.STRING,
      validate: {
        validateFunction: function(data) {
          if (data.length < 3) throw new Error('Name must have more than 3 characters.')
        }
      }
    }

About

Simple nodejs api project of a english school


Languages

Language:JavaScript 100.0%