Skip to content

Proyecto 4 del FSD Bootcamp deGeeksHubs. Backend correspondiente al sistema de gestión de citas para un estudio de tatuajes.

Notifications You must be signed in to change notification settings

SergioTorresGarcia/Proyecto4-Buscador

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TATTOO STUDIO searching engine

This is the FOURTH PROJECT of the Full Stack Development Bootcamp with Geekshubs Academy.

Tattoo studio

GitHubNode.jsTypeScriptExpress.jsMySQLDocker
Table of Contents
  1. Description (ES)
  2. Challenge
  3. Database diagram
  4. Instalation
  5. Endpoints
  6. Project development
  7. Deployment
  8. Author / Contact

Project description (ES)

Services management tool for a tattoo studio
Read about it (only in spanish)
Gestión de citas para un estudio de tatuajes:

El departamento de producto nos ha encomendado desarrollar el backend correspondiente al sistema de gestión de citas para un estudio de tatuajes.

Los usuarios podrán registrarse en la aplicación, iniciar sesión y acceder a su área personal. Dentro de su área, podrán visualizar una lista de las citas programadas para servicios de tatuajes y piercing, así como crear nuevas citas para diversos servicios que ofrece el estudio.

Además, se contará con una sección de usuario donde podrán acceder a sus datos personales, visualizables únicamente por el propio usuario (perfil).

Challenge

  • Create Endpoints (registration, login, CRUD):
  • Migrations & Seeders for all tables.
  • All non-public endpoints with corresponding middlewares.

Database diagram

Reverse engineered diagram of the DB

Instalation (local)

  1. Clone this repository
  2. Run $ npm install in terminal
  3. Conect repository with database
  4. Run migrations: $ npm run run-migrations
  5. *Run seeders: $ npm run seeders
  6. Start server: $ npm run dev
*Alternative to step 5:
  • you can select and copy ALL TEXT from the "data.sql" file
  • paste it into MySQL
  • execute it (⚡ button) to populate a smaller example of the database

    file route: ./src/database/seeders/data.sql

Endpoints

AUTH routes
  • REGISTER new user

    POST https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/auth/register
    

    body:

        {
            "first_name": "Sergio",
            "last_name": "Sergio",
            "email": "[email protected]",
            "password": "111111"
        }
  • LOGIN user

    POST https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/auth/login
    

    body:

        {
            "email": "[email protected]",
            "password": "123456"
        }
USERS routes
  • GET ALL USERS (including dinamic query search)

    GET https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/users
    GET https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/[email protected]
    
  • GET USER BY ID

    GET https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/users/:id
    
  • UPDATE PROFILE (for the currently logged user)

    PUT https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/users/self
    

    body:

        {
            "first_name": "name",
            "last_name": "surname",
            "email": "[email protected]",
            "password": "111111"
        }
  • UPDATE USER BY ID

    PUT https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/users/:id
    
  • UPDATE USER'S ROLE

    PUT https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/users/:id/:role
    
  • DELETE USER BY ID

    DELETE https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/users/:id
    
SERVICES routes
  • CREATE A NEW SERVICE

    POST https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/services
    
  • GET ALL SERVICES

    GET https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/services
    
  • UPDATE SERVICE BY ID

    UPDATE https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/services/:id
    
  • DELETE SERVICE BY ID

    DELETE https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/services/:id
    
APPOINTMENTS routes
  • CREATE A NEW APPOINTMENT

    POST https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/appointments
    
  • GET ALL APPOINTMENTS

    GET https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/appointments
    
  • GET OWN APPOINTMENTS

    GET https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/appointments/profile
    
  • GET APPOINTMENTS BY ID

    GET https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/appointments/:id
    
  • UPDATE APPOINTMENTS BY ID

    UPDATE https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/appointments/:id
    
  • DELETE APPOINTMENTS BY ID

    DELETE https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/api/appointments/:id
    

Find here the collection of all endpoints in Thunder Client:
  1. You have to open Thunder Client
  2. Go to collections
  3. Import this file: ./HTTP/thunder-collection_STUDIO TATTOO.json

Project Development:

1. SQL - Database design:
  • Analyze the task to find the purpose of the database and gather all requirements
  • Concept design: create an Entity-Relationship Diagram where we define tables, their attributes, and the relationships with one another.
  • Normalization: eliminate redundancy, identify primary keys (PK) and foreign keys (FK)
  • Logical thinking: decide what can and cannot be 'NULL' (not required) and which are 'UNIQUE' fields
2. DOCKER - Creating a container
  • Install docker
  • Create a container

    docker run -d -p 3306:3306 --name -e MYSQL_ROOT_PASSWORD=<your_password> mysql

  • Access it

    mysql -h localhost -P 3306 -u root -p you will need -h (host), -P (port), -u (username) and -p (password)

  • Execute it

    docker exec -it mysql-pruebas bash

3. EXPRESS - Create a server connection
  • We initiate NODE: $ npm init This creates 'package.json' where all the dependencies will be stored.

  • We run the command: $ npm install express --save This creates 'package-lock.json' and the 'node_modules' folder

  • We create the folder '.gitignore' and add '/node_modules' inside This blocks the heavy folder from being upload to github with the rest of the project.

  • We install TYPESCRIPT (as developers) $ npm install typescript -D

  • We create the 'tsconfig.json' file: $ npx tsc --init

  • We install types /express & node: $ npm install @types/express @types/node -D

  • We install dependencies to compile TS (nodemon): $ npm install ts-node nodemon -D

  • We add a shortcut to the package.json's scripts:

    "dev": "nodemon ./src/server.ts"

  • We create the file '.env' with the PORT (of the server) and add '.env' to the '.gitignore'.

    Also add a copy '.env.sample' where we will storage a blueprint of data, without the sensitive information (in this case: 'PORT= ')

  • We install 'dotenv': $ npm i dotenv This gets added to the dependencies and will grab data from the .env file

4. DOTENV - Connect to the DB
  • We create the folder 'src' with a 'server.ts' file inside. The main function connects to the server startServer();

  • We link a new file called app.ts to separate responsabilities.

  • In this file we write the following code:

    import express from "express";
    import dotenv from "dotenv";
    import { Request, Response } from "express";
    
    // links the .env folder
    dotenv.config(); 
    
    // runs server connection
    const app = express(); 
    
    // parses responses to .json)
    app.use(express.json()); 
    
    // sets up the connection port
    const PORT = process.env.PORT || 4002; 
    
    // server is up and listening to any upcomming request
    app.listen(3000, () => console.log('Servidor levantado en 3000')); 
    
    
    // testing request - 'Hello world' means we are ready to go!
    app.get('/', (req: Request, res: Response) => {
        res.send('Hello world!')
    }); 
  • We run the server using the previously created nodemon shortcut: $ npm run dev

5. MySQL Workbench
  • We open the workbench and run the following commands:

    CREATE DATABASE <project_name>;
    USE <project_name>;
6. MIGRATIONS & MODELS
  • Creating MIGRATIONS [Data Definition Language (DDL): with typeorm]: ./src/database/migrations
  • Adding them to DataSource.migrations in the db.ts file: Role, User, Service, Appointment
  • Creating MODELS (entities) [Data Manipulation Language (DML)]
  • Adding them to DataSource.entities in the db.ts file: Roles, Users, Services, Appointments
7. CONTROLLERS
  • We create controllers (in a folder on the same level with package.json):

    auth, roles, users, services, appointments

8. ROUTES
  • We create routes (in app.ts) for CRUD (create, read, update and delete) database records.
9. MIDDLEWARE: auth()
  • Additionally we need to control access to our data. We will use 'middleware' functions.

  • Auth (authorisation systembased on TOKENs) will block anything that is not to be seen by the general public. In our case, it only does not affect to register, login and getServices (as those are the endpoints reachable without logging in)

  • The auth() function verifies an encrypted TOKEN created automatically while logging in. With an active token we have access to other data.

10. MIDDLEWARE: isSuperAdmin()
  • We also want to grant special administrative access. With another middleware, the isSuperAdmin() function, we control PERMISSIONS.
  • The 'superadmin' role would be able to reach all data, while Users would have a more limited reach. More levels can be implemented
11. TOKENDATA
  • For the TOKEN to work, we create a new file ./types/index.d.ts with the following lines:

    export type tokenData = {
        userId: number;
        roleName: string;
    };
    
    declare global {
        namespace Express {
            export interface Request {
                tokenData: tokenData;
            }
        }
    }
12. SEEDERS
  • In order to check out this project, you'll need to ppopulate the database.

  • Follow steps 5 and 6 of the instalation

Deployment

The project is deployed here: https://proyecto4-buscador-dev-jzta.1.ie-1.fl0.io/

You can use this route for all the endpoints.

Author

GitHub

About

Proyecto 4 del FSD Bootcamp deGeeksHubs. Backend correspondiente al sistema de gestión de citas para un estudio de tatuajes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published