Skip to content

HouseCham/express-api

Repository files navigation

🎬 Movie Catalog API

An Express + TypeScript API for managing a movie catalog with PostgreSQL and Sequelize. This API allows you to create, update, delete, and fetch movies and categories, with proper validation, error handling, and a clean architecture using OOP principles.


πŸ› οΈ Technologies Used

  • Language: TypeScript
  • Framework: Express
  • Database: PostgreSQL
  • ORM: Sequelize
  • Environment Variables: dotenv
  • Validation: Zod (or Joi if preferred)
  • Architecture: Hexagonal (Ports and Adapters)
  • OOP: Classes, Interfaces, and SOLID Principles

πŸ“‚ Project Structure

src/
β”œβ”€β”€ application/        # Use Cases - Business logic
β”‚   β”œβ”€β”€ services/       # Service classes for business operations
β”‚   β”œβ”€β”€ dto/            # Data Transfer Objects
β”‚   β”œβ”€β”€ interfaces/     # Application-level interfaces (ports)
β”œβ”€β”€ domain/             # Core Domain
β”‚   β”œβ”€β”€ entities/       # Domain models (classes, interfaces)
β”‚   β”œβ”€β”€ repositories/   # Repository interfaces (ports)
β”‚   β”œβ”€β”€ exceptions/     # Domain-specific exceptions
β”œβ”€β”€ infrastructure/     # Adapters & Infrastructure
β”‚   β”œβ”€β”€ api/            # Express routes and controllers
β”‚   β”œβ”€β”€ db/             # Database connection and repositories (adapters)
β”‚   β”œβ”€β”€ validation/     # Validation schemas
β”‚   β”œβ”€β”€ middleware/     # Express middleware
β”‚   β”œβ”€β”€ config/         # Environment and configuration files
β”œβ”€β”€ shared/             # Shared utilities (e.g., logger, error handling)
β”œβ”€β”€ index.ts            # Entry point - Express app setup
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json

πŸ“¦ Environment Variables (.env)

DB_CONTAINER_NAME=movie_db
DB_HOST=localhost
DB_PORT=5432
DB_USER=admin
DB_PASSWORD=my_secret_password
DB_NAME=movies_db
DB_DIALECT=postgres

API_CONTAINER_NAME=movies-api
API_IMAGE_NAME=web-api:1.0.0
API_HOST=http://localhost
API_PORT=3000
API_CLIENT_URL=http://localhost:3000

πŸ—οΈ Database Models

πŸŽ₯ Movie

Field Type Description
id INTEGER (PK) Unique identifier
title STRING Movie title
description TEXT Movie description
categoryId INTEGER (FK) Reference to Category
createdAt DATE Record creation date
updatedAt DATE Record update date

πŸ“‚ Category

Field Type Description
id INTEGER (PK) Unique identifier
name STRING Category name
createdAt DATE Record creation date
updatedAt DATE Record update date

πŸ“‹ API Endpoints

πŸŽ₯ Movie Endpoints

  • POST /api/movies - Create a new movie
  • GET /api/movies/:id - Get movie details by ID
  • GET /api/movies - Get a paginated list of movies (filter by title/category)
  • PUT /api/movies/:id - Update a movie
  • DELETE /api/movies/:id - Delete a movie

πŸ“‚ Category Endpoints

  • POST /api/categories - Create a new category
  • GET /api/categories - Get all categories
  • GET /api/categories/:id - Get category details by ID
  • PUT /api/categories/:id - Update a category
  • DELETE /api/categories/:id - Delete a category (only if no movies are linked)

βœ… Validations

  • Required Fields: Ensure required fields are present.
  • Data Types: Validate types (string, number, etc.).
  • Constraints: Unique, non-null constraints.

πŸ”„ Pagination and Filtering

  • Movies: Supports pagination with page and limit query params.
  • Filtering: Filter movies by title or categoryId.

⚠️ Error Handling

  • 404: Resource not found.
  • 400: Validation errors.
  • 500: Internal server errors.

πŸ“Œ Testing

This application has been thoroughly tested using Jest, a JavaScript testing framework.

πŸ›  Test Setup

  • All tests are located inside the __tests__ directory.
  • The tests cover controllers, services, and repository layers.
  • The application includes unit and integration tests to validate API endpoints.

πŸš€ Running the Tests

To execute the tests, run the following command:

npm run test

βœ… Test Results

The latest test run successfully passed all test cases:

Test Suites: 2 passed, 2 total
Tests:       40 passed, 40 total
Snapshots:   0 total
Time:        3.423 s
Ran all test suites.

πŸ“ Coverage

The tests include:

  • Movies API (/movies)

    • βœ… Retrieve all movies (paginated, sorted, and filtered)
    • βœ… Retrieve a movie by ID
    • βœ… Create a new movie (with validation checks)
    • βœ… Update a movie (validations & duplicate handling)
    • βœ… Soft delete a movie
  • Categories API (/categories)

    • βœ… Retrieve all categories (paginated, sorted, and filtered)
    • βœ… Retrieve a category by ID
    • βœ… Create a new category (validations & duplicate handling)
    • βœ… Update a category
    • βœ… Delete a category (checks for associated movies)

πŸ“‚ Test Directory Structure

/__tests__
│── categoryRoutes.test.ts  # Tests for category endpoints
│── movieRoutes.test.ts     # Tests for movie endpoints

πŸš€ Getting Started

1. Clone the Repository

git clone https://github.com/HouseCham/express-api.git
cd express-api

2. Install Dependencies

npm install

3. Run Migrations

npx sequelize-cli db:migrate --config src/infrastructure/config/config.js

4. Run tests

npm run test

5. Build project

npm run build

6. Start the Server for DEV or PRODUCTION

npm run dev

or

npm run start

Server will run at: http://localhost:3000


About

Express + TypeScript API for managing a movie catalog with PostgreSQL and Sequelize

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published