Skip to content

luigiMinardi/movieClubBackend

Repository files navigation

movieClubBackend

License Nodejs JavaScript

Sequelize Mysql Express JsonWebToken

Yarn OR NPM

This is a REST API of a movie club, using mysql as data base. Having tables for users, movies and orders.

Documentation

Installing

  • First clone the repository:

    git clone https://github.com/luigiMinardi/movieClubBackend

    enter in the cloned repo cd movieClubBackend or open in your IDE, for example code movieClubBackend if you're using VSC.

  • Install all the dependencies:

    NPM

    npm i

    Yarn

    yarn
  • At config/config.js change:

    "development": {
        ...,
        "password": process.env.DB_PASSWORD || null,
        "database": process.env.DB_NAME || 'database_development',
        ...
    }

    To

    "development": {
        ...,
        "password": process.env.DB_PASSWORD || "your_root_password",
        "database": process.env.DB_NAME || "yourDatabaseName",
        ...
    }
  • Create the data base:

    You need to have mysql installed and running on your computer for this to work. At the first time you may need to create the db directly on mysql (or your UI to use it (like Mysql Workbench)) since sometimes the sequelize db:create bugs, but after creating it will work fine so whenever you drop your db you can recreate by the command instead of manually.

    NPM

    npx sequelize db:create

    Yarn

    yarn sequelize db:create
  • Make the migrations:

    NPM

    npx sequelize db:migrate

    Yarn

    yarn sequelize db:migrate
  • Run the server:

    NPM

    npm run dev

    Yarn

    yarn nodemon

Configuring .env

In your root repository create a .env file and add this:

# index.js
PORT=3000 # server port

# config/auth
AUTH_SECRET="ShhDon'tTellHim" # Key the encryption will use to run
AUTH_EXPIRES="24h" # Token expiration
AUTH_ROUNDS=10 # Times the encryption will be done

# DB.js
DB_HOST="127.0.0.1" # DB server
DB_PORT="3306" # Port of DB
DB_DIALECT="mysql" # Type of DB
# DB.js & models/index
DB_NAME="yourDatabaseName" # Name of DB
DB_USER="root" # Name of DB superuser
DB_PASSWORD="YourRootPassword" # Password of superuser

# models/index
NODE_ENV="development" # Type of environment at config/config.js

############ You can ignore from here on out. ############
############ Its just a example of how it will be on your deployment
############ you probably wont need those variables below.
## Deploy DB
## Remove the comments of those lines bellow and add a comment to
## the lines above with same variable names to change from
## development db to production one.
# DB_HOST="yourDeploy.cleardb.net" # DB server url generated by cleardb
# DB_PORT="3306"
# DB_DIALECT="mysql"
# DB_NAME="heroku_yourName" # DB name generated by heroku
# DB_USER="user" # DB superuser generated by heroku
# DB_PASSWORD="password" # superuser password generated by heroku
# NODE_ENV="production"

Now you are ready to use it.

Using the API

Data Base draw

%%{init: {
    'theme': 'base',
    'themeVariables': {
        'primaryColor': '#282a36',
        'primaryTextColor': '#282a36',
        'mainBkg': '#bd93f9',
        'lineColor': '#6272a4'
    }
}}%%
erDiagram
    USER ||--o{ ORDER : ""
    USER {
        integer id
        string name
        integer age
        string surname
        string email
        string nickname
        string password
        boolean isAdmin
        date createdAt
        date updatedAt
    }
    ORDER {
        integer id
        integer price
        integer movieId
        integer userId
        date createdAt
        date updatedAt
    }
    MOVIE ||--o{ ORDER : ""
    MOVIE {
        integer id
        string title
        string description
        boolean adult
        float popularity
        string image
        string date
        date createdAt
        date updatedAt
    }
Loading

Expected Behavior

createdAt, updatedAt, id, are obligatory and auto-generated.

the id is the Primary Key of the tables.

USER name, email, are obligatory.

USER email, nickname, are unique.

MOVIE title, description, adult, are obligatory.

ORDER movieId, userId, date, are obligatory.

movieId and userId are the MOVIE and the USER Foreign Key respectively.

You may see references for the Primary Key as pk and for the foreign key as fk.

Endpoints

To see the endpoints and how they works, check our documentation.

Documentation

WIP

  • - Improve admin rights

    • - User
  • - Creation of payment

  • - Refactor of view-controllers to be more restful

    • - User
    • - Movie
    • - Order
    • - MovieDB
  • - turn some endpoints in more generic versions of them to be more scalable

  • - Field validation

    • - Email, Name, Nickname should NOT allow blank values ("")
    • - Email should NOT accept non email values (asdf = error, asdf@asdf = error, asdf@asdf.asdf = valid)
    • - Password minimal length and complexity.
    • - Return all input errors at once.
  • - Automated tests

  • - Adding error responses to the documentation

TODO Later

  • - Don't allow null, empty or undefined in any field that don't make sense to be so.
  • - Create default user when db is generated by the fist time (if is everything empty).
  • - If new admin where created it need to be accepted by other admin before it can use its account.