Skip to content

AntosDev/MovieAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 

Repository files navigation

Simple Movie API using NestJS

To Run:

move into movie-api folder
cd movie-api
and
docker compose up
and wait (it will take some time to run the db migrations)

Enjoy!

Useful commands:

  • npm run start:dev
    to start the project with hot reload
  • npm run typeorm:run-migrations
    to run needed migrations
  • npm run typeorm:generate-migration
    to create a migration after a change in the entities

What could've been added?

  • Better Error Handling
  • More granular e2es
  • Customised authentication guards

Technology stack

  • NestJS
  • Docker
  • Postgres
  • TyperORM

Main Design and Functional Features

  • Architecture Design: SOLID, DDD, Hexagonal
  • Everything is Dockerised
  • Authentication: JWT, generated through ('auth/login')
  • APIs
    • GET ('/movies'): returns list of the authenticates user's movies
    • POST('/movies'): using a provided title fetches a movie from omdAPI and saves it's data
  • Security: Authentication guards, password hashing using bcrypt
  • Seperate .env files for docker and local
  • ...

How it was developed

The process tried to adhere as much as possible to: - SOLID (especially Seperation of concerns and IoC), - hexagonal architecture (dependencies are outwards) - DDD maintaining a clean domain layer where the business rules are inforced and an in the application layer used use cases instead of services and seperated the persistance logic from the business logic

The source code contains:

  • main files containing docker files, cofigurations and .env
  • under src
    • external providers: implementation of wrapper around omdAPI (since we have IoC the module will use the interface and not the actual implementation)
    • healthcheck: the hello world APIs generated by nest
    • modules:
      • userauths: (the authentication stuff, does not strictly follow DDD as there is no need)
        • application: the local and JWT strategies (the implementations needed to validate credentials, generate tokens andvalidate them; followed nestjs official documentation)
        • domain: contains the entities used by the module
        • infra:
          • http: the auth controller containing the ('auth/login') api
          • data-access: the user database entity the repository used to retrieve users
        • mocks: the classes used by ject
        • the module.ts which is linking it all together
      • movies: (this is the core module of the application and does adhere to tactical patterns of DDD)
        • application: the orchetrator layer, is not persistance ignorant (as it connects with the repository but do so using IoC)
          • providers: the interfaces of the external providers that the module's application needs to function, the implementation of which will be offered by the module.ts
          • the use cases of the application, tahese are the main functionalities of the module, each complete of its own accord
          • the use cases' spec files to run unit tests
        • domain: the core entities of the movies module, responsible for ensuring business rules
        • infra: everything not part of the core business of the module is sent to the edge of the hexagone!
          • http: the movies controller containing the APIs
            • dtos: cotaining the objects that will be shared with teh outside world
          • data-access:
            • repositories: responsible for the data logic and returning the aggregate root
            • entities: the database entities (ORMs) that will be used to generate the database and access it
        • mocks: the classes used by jest
        • the module.ts which is linking it all together
    • tests: contains the e2e tests used to validate this application

Steps(Gross-modo):

  1. Create project: nest new movie-api with npm
  2. Build modular project structure
    • application containing the use cases
    • domain containing the domain objects and logic
    • infra: containing both http (controllers) and data-access (entities, repositories implementation)
  3. Added online movies provider, used explicit injection instead of injection by interface to respect hexagonal architecture
  4. Implemented movies domain functionality
  5. Added repository (no DB access yet)
  6. Link the above together orchastrated in the use case
  7. Implement authentication module (following nestjs official documentation)
  8. Add local and jwt guards
  9. Guard create movie APi by JWT token
  10. Create entities and install typeORM packages
  11. Create docker files for postgres
  12. Create database from TypeORM to postgres docker
  13. Save created movie in DB
  14. Get authenticated user from context
  15. Implement business logic (only 5 movies for basic users)
  16. Implement get movies API fully
  17. Implement unit tests for movies use cases
  18. dockerise nestjs