POC - Typescript - DrivenFit

  • In this back-end project, a simple system of a Gym is used as a Proof of concept (POC) of TypeScript coding.
  • A CRUD (Create, Update and Delete) protocol was used for manage two self-explained tables: customers and enrollments.
  • The table courses (with gym courses in Portuguese) is already created in the database;
  • Furthermore, ranking routes are implemented, aggregating the courses and customers metrics (see routes below).

πŸ‘£ Initial steps

    Install all dependencies

    npm i 

    Create the .env file following the .env.example file in the root folder

  • Example of .env file:
  • POSTGRES_USERNAME= 
    POSTGRES_PASSWORD= 
    POSTGRES_HOST= 
    POSTGRES_PORT= 
    POSTGRES_DATABASE= 
    
    PORT=
    

    Run the server API locally

    npm run dev

    Build the application (for deploy)

    npm run build

    Start the application (for deploy)

    npm run start

    ⚠️ You'll also need to download the database structure (see database section below)

πŸ—‚ Folders organization

    β”œβ”€β”€ README.md
    β”œβ”€β”€ package-lock.json
    β”œβ”€β”€ package.json
    β”œβ”€β”€ πŸ“ src
    β”‚   β”œβ”€β”€ πŸ“ controllers
    β”‚   β”‚   β”œβ”€β”€ courses.controllers.ts
    β”‚   β”‚   β”œβ”€β”€ customers.controllers.ts
    β”‚   β”‚   └── enrollments.controllers.ts
    β”‚   β”œβ”€β”€ πŸ“ database
    β”‚   β”‚   β”œβ”€β”€ connectionDB.ts
    β”‚   β”‚   β”œβ”€β”€ dbdiagram.png
    β”‚   β”‚   └── dump.sql
    β”‚   β”œβ”€β”€ index.ts
    β”‚   β”œβ”€β”€ πŸ“ middlewares
    β”‚   β”‚   β”œβ”€β”€ courses.middlewares.ts
    β”‚   β”‚   β”œβ”€β”€ customers.middleware.ts
    β”‚   β”‚   └── schemas.validation.ts
    β”‚   β”œβ”€β”€ πŸ“ protocols
    β”‚   β”‚   β”œβ”€β”€ courses-rank.ts
    β”‚   β”‚   β”œβ”€β”€ customer.ts
    β”‚   β”‚   β”œβ”€β”€ customers-rank.ts
    β”‚   β”‚   └── enrollment.ts
    β”‚   β”œβ”€β”€ πŸ“ repositories
    β”‚   β”‚   β”œβ”€β”€ courses.repository.ts
    β”‚   β”‚   β”œβ”€β”€ customers.repository.ts
    β”‚   β”‚   └── enrollments.repository.ts
    β”‚   β”œβ”€β”€ πŸ“ routes
    β”‚   β”‚   β”œβ”€β”€ courses.routes.ts
    β”‚   β”‚   β”œβ”€β”€ customers.routes.ts
    β”‚   β”‚   β”œβ”€β”€ enrollments.routes.ts
    β”‚   β”‚   └── index.ts
    β”‚   β”œβ”€β”€ πŸ“ schemas
    β”‚   β”‚   β”œβ”€β”€ customer.schema.ts
    β”‚   β”‚   β”œβ”€β”€ enrollment.schema.ts
    β”‚   β”‚   └── top-query.schema.ts
    β”‚   └── πŸ“ services
    └── tsconfig.json
    

🧭 Routes

    Customers routes

    πŸ‘‰πŸ» Registering a customer

    • POST /register
    • Send customer via body as follow
    • {
          "name": "Customer Name",
          "email": "test@test.com",
          "cpf": "12345678910"
      }
    • If succeed, receive an answer in the format:
    • {
          "message": "Customer_name was registered!"
      }

    πŸ‘‰πŸ» Updating a customer's email

    • PUT /update/email
    • Send the infos via body as follow
    • {
          "new_email": "new_email@example.com",
          "previous_email": "previous_email@example.com"
      }
    • If succeed, receive an answer in the format:
    • {
          "message": "Customer_name's email updated!"
      }

    Enrolments routes

    πŸ‘‰πŸ» Enrolling a customer in a course

    • POST /enroll
    • Send a body containing the following infos
    • {
          "customer_id": "1",
          "course_id": "2"
      }
    • If succeed, receive an answer in the format:
    • {
          "message": "Success: Customer_X was enrolled into Course_X!"
      }

    πŸ‘‰πŸ» Unenrolling a customer from a course

    • DELETE /enroll
    • Send a body containing the following infos
    • {
          "customer_id": "1",
          "course_id": "2"
      }
    • If succeed, receive an answer in the format:
    • {
          "message": "Success: Customer_X was unenrolled from Course_X!"
      }

    πŸ‘‰πŸ» Rank of customers with higher number of enrolements

    • GET /rank/customers
    • Optional a query top with a number, as the example below:
    • /rank/customers?top=3
      
    • Response in the format:
    • [
        {
          "customer": "Γ‰rick",
          "courses": "5"
        },
        {
          "customer": "Brun0",
          "courses": "4"
        },
        {
          "customer": "Jader",
          "courses": "3"
        }
      ]

    Courses routes

    πŸ‘‰πŸ» Rank of courses with higher number of enrollments

    • GET /rank/courses
    • Optional a query top with a number, as the example below:
    • /rank/courses?top=3
      
    • Response in the format:
    • [
        {
          "course": "LPO",
          "customers": "7"
        },
        {
          "course": "Dança",
          "customers": "5"
        },
        {
          "course": "Zumba",
          "customers": "3"
        }
      ]

    πŸ‘‰πŸ» Courses that a customer is enrolled in

    • GET /courses/:customer_id
    • customer_id is a number and required, as the example below:
    • /courses/2
      
    • Response in the format of an array, as shown below:
    • [
        "Spining",
        "Funcional",
        "Dança",
        "LPO",
        "HidroginΓ‘stica"
      ]

πŸ—„οΈ Database