├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── Nest.js - Arquitetura da aplicação@1.25x.png ├── README.md ├── api.http ├── bank-accounts.sqlite ├── nest-cli.json ├── package-lock.json ├── package.json ├── src ├── app.controller.spec.ts ├── app.controller.ts ├── app.module.ts ├── app.service.ts ├── bank-accounts │ ├── bank-accounts.controller.spec.ts │ ├── bank-accounts.controller.ts │ ├── bank-accounts.module.ts │ ├── bank-accounts.service.spec.ts │ ├── bank-accounts.service.ts │ ├── dto │ │ ├── create-bank-account.dto.ts │ │ └── transfer-bank-account.dto.ts │ └── entities │ │ └── bank-account.entity.ts ├── main.ts └── repl.ts ├── test ├── app.e2e-spec.ts └── jest-e2e.json ├── tsconfig.build.json └── tsconfig.json /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | tsconfigRootDir : __dirname, 6 | sourceType: 'module', 7 | }, 8 | plugins: ['@typescript-eslint/eslint-plugin'], 9 | extends: [ 10 | 'plugin:@typescript-eslint/recommended', 11 | 'plugin:prettier/recommended', 12 | ], 13 | root: true, 14 | env: { 15 | node: true, 16 | jest: true, 17 | }, 18 | ignorePatterns: ['.eslintrc.js'], 19 | rules: { 20 | '@typescript-eslint/interface-name-prefix': 'off', 21 | '@typescript-eslint/explicit-function-return-type': 'off', 22 | '@typescript-eslint/explicit-module-boundary-types': 'off', 23 | '@typescript-eslint/no-explicit-any': 'off', 24 | }, 25 | }; 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | pnpm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | lerna-debug.log* 13 | 14 | # OS 15 | .DS_Store 16 | 17 | # Tests 18 | /coverage 19 | /.nyc_output 20 | 21 | # IDEs and editors 22 | /.idea 23 | .project 24 | .classpath 25 | .c9/ 26 | *.launch 27 | .settings/ 28 | *.sublime-workspace 29 | 30 | # IDE - VSCode 31 | .vscode/* 32 | !.vscode/settings.json 33 | !.vscode/tasks.json 34 | !.vscode/launch.json 35 | !.vscode/extensions.json 36 | 37 | .history/ -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /Nest.js - Arquitetura da aplicação@1.25x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeedu/live-imersao-fullcycle9-nestjs/4aac46dc8a2acffe6fbba0a4e89a9a98e5e6f4ff/Nest.js - Arquitetura da aplicação@1.25x.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Imersão Full Stack && Full Cycle](https://events-fullcycle.s3.amazonaws.com/events-fullcycle/static/site/img/grupo_4417.png) 2 | 3 | Participe gratuitamente: https://imersao.fullcycle.com.br/ 4 | 5 | ## Sobre o repositório 6 | Esse repositório contém o código-fonte ministrado na aula Desenvolva uma aplicação com Nest.js 9 e aproveite as novidades da nova versão: [https://www.youtube.com/watch?v=1yu9084sVjs](https://www.youtube.com/watch?v=1yu9084sVjs) 7 | 8 | ## Rodar a aplicação 9 | 10 | Execute os comandos: 11 | 12 | ```bash 13 | npm install 14 | npm run start:dev 15 | ``` 16 | 17 | Acesse http://localhost:3000/bank-accounts ou use o arquivo `api.http` para testar a API usando a extensão Rest Client do VSCode ou outra ferramenta para brincar com o HTTP. -------------------------------------------------------------------------------- /api.http: -------------------------------------------------------------------------------- 1 | GET http://localhost:3000/bank-accounts 2 | 3 | ### 4 | GET http://localhost:3000/bank-accounts/ce669d21-4fa4-47bb-8918-e657b809910a 5 | 6 | ### 7 | POST http://localhost:3000/bank-accounts 8 | Content-Type: application/json 9 | 10 | { 11 | "account_number": "2222-22" 12 | } 13 | 14 | ### 15 | PATCH http://localhost:3000/bank-accounts/123 16 | Content-Type: application/json 17 | 18 | { 19 | "account_number": 1234 20 | } 21 | 22 | ### 23 | DELETE http://localhost:3000/bank-accounts/123 24 | 25 | ### 26 | POST http://localhost:3000/bank-accounts/transfer 27 | Content-Type: application/json 28 | 29 | { 30 | "from": "1111-11", 31 | "to": "2222-22", 32 | "amount": 100 33 | } -------------------------------------------------------------------------------- /bank-accounts.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeedu/live-imersao-fullcycle9-nestjs/4aac46dc8a2acffe6fbba0a4e89a9a98e5e6f4ff/bank-accounts.sqlite -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/nest-cli", 3 | "collection": "@nestjs/schematics", 4 | "sourceRoot": "src" 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "iniciando-nestjs-9", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json", 22 | "repl": "npm run start -- --entryFile repl" 23 | }, 24 | "dependencies": { 25 | "@nestjs/common": "^9.0.0", 26 | "@nestjs/core": "^9.0.0", 27 | "@nestjs/mapped-types": "*", 28 | "@nestjs/platform-express": "^9.0.0", 29 | "@nestjs/typeorm": "^9.0.0", 30 | "reflect-metadata": "^0.1.13", 31 | "rimraf": "^3.0.2", 32 | "rxjs": "^7.2.0", 33 | "sqlite3": "^5.0.11", 34 | "typeorm": "^0.3.7" 35 | }, 36 | "devDependencies": { 37 | "@nestjs/cli": "^9.0.0", 38 | "@nestjs/schematics": "^9.0.0", 39 | "@nestjs/testing": "^9.0.0", 40 | "@types/express": "^4.17.13", 41 | "@types/jest": "28.1.4", 42 | "@types/node": "^16.0.0", 43 | "@types/supertest": "^2.0.11", 44 | "@typescript-eslint/eslint-plugin": "^5.0.0", 45 | "@typescript-eslint/parser": "^5.0.0", 46 | "eslint": "^8.0.1", 47 | "eslint-config-prettier": "^8.3.0", 48 | "eslint-plugin-prettier": "^4.0.0", 49 | "jest": "28.1.2", 50 | "prettier": "^2.3.2", 51 | "source-map-support": "^0.5.20", 52 | "supertest": "^6.1.3", 53 | "ts-jest": "28.0.5", 54 | "ts-loader": "^9.2.3", 55 | "ts-node": "^10.0.0", 56 | "tsconfig-paths": "4.0.0", 57 | "typescript": "^4.3.5" 58 | }, 59 | "jest": { 60 | "moduleFileExtensions": [ 61 | "js", 62 | "json", 63 | "ts" 64 | ], 65 | "rootDir": "src", 66 | "testRegex": ".*\\.spec\\.ts$", 67 | "transform": { 68 | "^.+\\.(t|j)s$": "ts-jest" 69 | }, 70 | "collectCoverageFrom": [ 71 | "**/*.(t|j)s" 72 | ], 73 | "coverageDirectory": "../coverage", 74 | "testEnvironment": "node" 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/app.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | 5 | describe('AppController', () => { 6 | let appController: AppController; 7 | 8 | beforeEach(async () => { 9 | const app: TestingModule = await Test.createTestingModule({ 10 | controllers: [AppController], 11 | providers: [AppService], 12 | }).compile(); 13 | 14 | appController = app.get(AppController); 15 | }); 16 | 17 | describe('root', () => { 18 | it('should return "Hello World!"', () => { 19 | expect(appController.getHello()).toBe('Hello World!'); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /src/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get } from '@nestjs/common'; 2 | import { AppService } from './app.service'; 3 | 4 | @Controller('prefixo') 5 | export class AppController { 6 | constructor(private readonly appService: AppService) {} 7 | 8 | @Get('test') 9 | getHello(): string { 10 | return this.appService.getHello(); 11 | } 12 | 13 | @Get('test2') 14 | test2(){ 15 | return 'test2'; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | import { BankAccountsModule } from './bank-accounts/bank-accounts.module'; 5 | import { TypeOrmModule } from '@nestjs/typeorm'; 6 | import { BankAccount } from './bank-accounts/entities/bank-account.entity'; 7 | @Module({ 8 | imports: [ 9 | TypeOrmModule.forRoot({ 10 | type: 'sqlite', 11 | database: __dirname + '/db.sqlite', 12 | synchronize: true, 13 | logging: true, 14 | entities: [BankAccount], 15 | }), 16 | BankAccountsModule, 17 | ], 18 | controllers: [AppController], 19 | providers: [AppService], 20 | }) 21 | export class AppModule {} 22 | 23 | //modular - Angular 24 | -------------------------------------------------------------------------------- /src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | 3 | @Injectable() 4 | export class AppService { 5 | getHello(): string { 6 | return 'Hello World!'; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/bank-accounts/bank-accounts.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { BankAccountsController } from './bank-accounts.controller'; 3 | import { BankAccountsService } from './bank-accounts.service'; 4 | 5 | describe('BankAccountsController', () => { 6 | let controller: BankAccountsController; 7 | 8 | beforeEach(async () => { 9 | const module: TestingModule = await Test.createTestingModule({ 10 | controllers: [BankAccountsController], 11 | providers: [BankAccountsService], 12 | }).compile(); 13 | 14 | controller = module.get(BankAccountsController); 15 | }); 16 | 17 | it('should be defined', () => { 18 | expect(controller).toBeDefined(); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /src/bank-accounts/bank-accounts.controller.ts: -------------------------------------------------------------------------------- 1 | import { 2 | Controller, 3 | Get, 4 | Post, 5 | Body, 6 | Patch, 7 | Param, 8 | Delete, 9 | HttpCode, 10 | } from '@nestjs/common'; 11 | import { BankAccountsService } from './bank-accounts.service'; 12 | import { CreateBankAccountDto } from './dto/create-bank-account.dto'; 13 | import { TransferBankAccountDto } from './dto/transfer-bank-account.dto'; 14 | 15 | @Controller('bank-accounts') 16 | export class BankAccountsController { 17 | constructor(private readonly bankAccountsService: BankAccountsService) {} 18 | 19 | @Post() 20 | create(@Body() createBankAccountDto: CreateBankAccountDto) { 21 | //Data transfer object 22 | return this.bankAccountsService.create(createBankAccountDto); 23 | } 24 | 25 | @Get() 26 | findAll() { 27 | return this.bankAccountsService.findAll(); 28 | } 29 | 30 | @Get(':id') 31 | findOne(@Param('id') id: string) { 32 | return this.bankAccountsService.findOne(id); 33 | } 34 | 35 | @HttpCode(204) 36 | @Post('transfer') 37 | transfer(@Body() transferDto: TransferBankAccountDto) { 38 | return this.bankAccountsService.transfer( 39 | transferDto.from, 40 | transferDto.to, 41 | transferDto.amount, 42 | ); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/bank-accounts/bank-accounts.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { BankAccountsService } from './bank-accounts.service'; 3 | import { BankAccountsController } from './bank-accounts.controller'; 4 | import { TypeOrmModule } from '@nestjs/typeorm'; 5 | import { BankAccount } from './entities/bank-account.entity'; 6 | 7 | @Module({ 8 | imports: [TypeOrmModule.forFeature([BankAccount])], 9 | controllers: [BankAccountsController], 10 | providers: [BankAccountsService], 11 | }) 12 | export class BankAccountsModule {} 13 | -------------------------------------------------------------------------------- /src/bank-accounts/bank-accounts.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { BankAccountsService } from './bank-accounts.service'; 3 | 4 | describe('BankAccountsService', () => { 5 | let service: BankAccountsService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [BankAccountsService], 10 | }).compile(); 11 | 12 | service = module.get(BankAccountsService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/bank-accounts/bank-accounts.service.ts: -------------------------------------------------------------------------------- 1 | import { Inject, Injectable, Scope } from '@nestjs/common'; 2 | import { CreateBankAccountDto } from './dto/create-bank-account.dto'; 3 | import { Repository, DataSource } from 'typeorm'; 4 | import { BankAccount } from './entities/bank-account.entity'; 5 | import { InjectRepository, getDataSourceToken } from '@nestjs/typeorm'; 6 | // Real Eval Print Loop 7 | @Injectable({ 8 | scope: Scope.REQUEST, 9 | durable: true, 10 | }) 11 | export class BankAccountsService { 12 | constructor( 13 | @InjectRepository(BankAccount) 14 | private repo: Repository, 15 | @Inject(getDataSourceToken()) 16 | private dataSource: DataSource, 17 | ) {} 18 | 19 | async create(createBankAccountDto: CreateBankAccountDto) { 20 | const bankAccount = this.repo.create({ 21 | account_number: createBankAccountDto.account_number, 22 | balance: 0, 23 | }); 24 | 25 | await this.repo.insert(bankAccount); 26 | return bankAccount; 27 | } 28 | 29 | findAll() { 30 | return this.repo.find(); 31 | } 32 | 33 | findOne(id: string) { 34 | return this.repo.findOneBy({ id }); 35 | } 36 | 37 | async transfer(from: string, to: string, amount: number) { 38 | //modo transação 39 | const queryRunner = this.dataSource.createQueryRunner(); 40 | try { 41 | await queryRunner.startTransaction(); 42 | const fromAccount = await this.repo.findOneBy({ account_number: from }); 43 | const toAccount = await this.repo.findOneBy({ account_number: to }); 44 | 45 | fromAccount.balance -= amount; 46 | toAccount.balance += amount; 47 | 48 | this.repo.save(fromAccount); 49 | this.repo.save(toAccount); 50 | await queryRunner.commitTransaction(); 51 | } catch (e) { 52 | await queryRunner.rollbackTransaction(); 53 | console.error(e); 54 | throw e; 55 | } 56 | } 57 | } 58 | 59 | // repl 60 | // ### durable providers - nested container 61 | // facilitar a criação de módulos personalizados 62 | 63 | //scope request -> request -> motivo ID do mini container 64 | 65 | // tenant - inquilino 66 | 67 | // conta azul - tenant - empresa 68 | 69 | //SaaS -------------------------------------------------------------------------------- /src/bank-accounts/dto/create-bank-account.dto.ts: -------------------------------------------------------------------------------- 1 | export class CreateBankAccountDto { 2 | account_number: string; 3 | } 4 | -------------------------------------------------------------------------------- /src/bank-accounts/dto/transfer-bank-account.dto.ts: -------------------------------------------------------------------------------- 1 | export class TransferBankAccountDto { 2 | from: string; 3 | to: string; 4 | amount: number; 5 | } 6 | -------------------------------------------------------------------------------- /src/bank-accounts/entities/bank-account.entity.ts: -------------------------------------------------------------------------------- 1 | import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; 2 | 3 | @Entity() 4 | export class BankAccount { 5 | @PrimaryGeneratedColumn('uuid') 6 | id: string; 7 | 8 | @Column({ type: 'decimal', scale: 2 }) 9 | balance: number; 10 | 11 | @Column({ length: 255 }) 12 | account_number: string; 13 | } 14 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | await app.listen(3000); 7 | } 8 | bootstrap(); 9 | -------------------------------------------------------------------------------- /src/repl.ts: -------------------------------------------------------------------------------- 1 | import { repl } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | await repl(AppModule); 6 | } 7 | bootstrap(); 8 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "es2017", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true, 14 | "skipLibCheck": true, 15 | "strictNullChecks": false, 16 | "noImplicitAny": false, 17 | "strictBindCallApply": false, 18 | "forceConsistentCasingInFileNames": false, 19 | "noFallthroughCasesInSwitch": false 20 | } 21 | } 22 | --------------------------------------------------------------------------------