├── .eslintrc.js
├── .gitignore
├── .prettierrc
├── .vscode
└── settings.json
├── README.md
├── nest-cli.json
├── package-lock.json
├── package.json
├── src
├── app.module.ts
├── config
│ └── typeorm.config.ts
├── holidays
│ ├── dto
│ │ └── create-holiday.dto.ts
│ ├── holiday-type.enum.ts
│ ├── holiday.entity.ts
│ ├── holiday.repository.ts
│ ├── holidays.controller.ts
│ ├── holidays.model.ts
│ ├── holidays.module.ts
│ └── holidays.service.ts
└── main.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 | sourceType: 'module',
6 | },
7 | plugins: ['@typescript-eslint/eslint-plugin'],
8 | extends: [
9 | 'plugin:@typescript-eslint/recommended',
10 | 'prettier/@typescript-eslint',
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 | yarn-debug.log*
10 | yarn-error.log*
11 | lerna-debug.log*
12 |
13 | # OS
14 | .DS_Store
15 |
16 | # Tests
17 | /coverage
18 | /.nyc_output
19 |
20 | # IDEs and editors
21 | /.idea
22 | .project
23 | .classpath
24 | .c9/
25 | *.launch
26 | .settings/
27 | *.sublime-workspace
28 |
29 | # IDE - VSCode
30 | .vscode/*
31 | !.vscode/settings.json
32 | !.vscode/tasks.json
33 | !.vscode/launch.json
34 | !.vscode/extensions.json
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": true,
3 | "trailingComma": "all"
4 | }
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "workbench.colorCustomizations": {
3 | "activityBar.background": "#112F4B",
4 | "titleBar.activeBackground": "#184269",
5 | "titleBar.activeForeground": "#FAFCFE"
6 | }
7 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
6 | [circleci-url]: https://circleci.com/gh/nestjs/nest
7 |
8 | A progressive Node.js framework for building efficient and scalable server-side applications.
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
24 |
25 | ## Description
26 |
27 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
28 |
29 | ## Installation
30 |
31 | ```bash
32 | $ npm install
33 | ```
34 |
35 | ## Running the app
36 |
37 | ```bash
38 | # development
39 | $ npm run start
40 |
41 | # watch mode
42 | $ npm run start:dev
43 |
44 | # production mode
45 | $ npm run start:prod
46 | ```
47 |
48 | ## Test
49 |
50 | ```bash
51 | # unit tests
52 | $ npm run test
53 |
54 | # e2e tests
55 | $ npm run test:e2e
56 |
57 | # test coverage
58 | $ npm run test:cov
59 | ```
60 |
61 | ## Support
62 |
63 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
64 |
65 | ## Stay in touch
66 |
67 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
68 | - Website - [https://nestjs.com](https://nestjs.com/)
69 | - Twitter - [@nestframework](https://twitter.com/nestframework)
70 |
71 | ## License
72 |
73 | Nest is [MIT licensed](LICENSE).
74 |
--------------------------------------------------------------------------------
/nest-cli.json:
--------------------------------------------------------------------------------
1 | {
2 | "collection": "@nestjs/schematics",
3 | "sourceRoot": "src"
4 | }
5 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "project-one",
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 | },
23 | "dependencies": {
24 | "@nestjs/common": "^7.5.1",
25 | "@nestjs/core": "^7.5.1",
26 | "@nestjs/platform-express": "^7.5.1",
27 | "@nestjs/typeorm": "^7.1.5",
28 | "mysql2": "^2.2.5",
29 | "reflect-metadata": "^0.1.13",
30 | "rimraf": "^3.0.2",
31 | "rxjs": "^6.6.3",
32 | "typeorm": "^0.2.31",
33 | "uuid": "^8.3.2"
34 | },
35 | "devDependencies": {
36 | "@nestjs/cli": "^7.5.1",
37 | "@nestjs/schematics": "^7.1.3",
38 | "@nestjs/testing": "^7.5.1",
39 | "@types/express": "^4.17.8",
40 | "@types/jest": "^26.0.15",
41 | "@types/node": "^14.14.6",
42 | "@types/supertest": "^2.0.10",
43 | "@typescript-eslint/eslint-plugin": "^4.6.1",
44 | "@typescript-eslint/parser": "^4.6.1",
45 | "eslint": "^7.12.1",
46 | "eslint-config-prettier": "7.2.0",
47 | "eslint-plugin-prettier": "^3.1.4",
48 | "jest": "^26.6.3",
49 | "prettier": "^2.1.2",
50 | "supertest": "^6.0.0",
51 | "ts-jest": "^26.4.3",
52 | "ts-loader": "^8.0.8",
53 | "ts-node": "^9.0.0",
54 | "tsconfig-paths": "^3.9.0",
55 | "typescript": "^4.0.5"
56 | },
57 | "jest": {
58 | "moduleFileExtensions": [
59 | "js",
60 | "json",
61 | "ts"
62 | ],
63 | "rootDir": "src",
64 | "testRegex": ".*\\.spec\\.ts$",
65 | "transform": {
66 | "^.+\\.(t|j)s$": "ts-jest"
67 | },
68 | "collectCoverageFrom": [
69 | "**/*.(t|j)s"
70 | ],
71 | "coverageDirectory": "../coverage",
72 | "testEnvironment": "node"
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/src/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { HolidaysModule } from './holidays/holidays.module';
3 | import { typeOrmConfig } from './config/typeorm.config';
4 | import {TypeOrmModule } from '@nestjs/typeorm';
5 | //Este é o módulo Root, toda aplicação tem ao menos um módulo.
6 | //o módulo Root, veja que está usando um decorator @Module
7 |
8 | @Module({
9 | imports: [
10 | TypeOrmModule.forRoot(typeOrmConfig),
11 | HolidaysModule]
12 | })
13 | export class AppModule {}
14 |
--------------------------------------------------------------------------------
/src/config/typeorm.config.ts:
--------------------------------------------------------------------------------
1 |
2 | import { TypeOrmModuleOptions } from '@nestjs/typeorm';
3 |
4 | export const typeOrmConfig: TypeOrmModuleOptions = {
5 | type: 'mysql',
6 | host: 'localhost',
7 | port: 3306,
8 | username: 'projectone',
9 | password: 'projectone',
10 | database: 'projectone',
11 | entities: [__dirname + '/../**/*.entity.js'],
12 | synchronize: true,
13 | };
--------------------------------------------------------------------------------
/src/holidays/dto/create-holiday.dto.ts:
--------------------------------------------------------------------------------
1 | export class CreateHolidayDto {
2 | name: string;
3 | date: Date;
4 | }
--------------------------------------------------------------------------------
/src/holidays/holiday-type.enum.ts:
--------------------------------------------------------------------------------
1 | export enum HolidaysType {
2 | NACIONAL = 'NACIONAL',
3 | ESTADUAL = 'ESTADUAL',
4 | MUNICIPAL = 'MUNICIPAL',
5 | FACULTATIVO = 'FACULTATIVO',
6 | }
--------------------------------------------------------------------------------
/src/holidays/holiday.entity.ts:
--------------------------------------------------------------------------------
1 | import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from "typeorm";
2 | import { HolidaysType } from "./holiday-type.enum";
3 |
4 | @Entity()
5 | export class Holiday extends BaseEntity {
6 | @PrimaryGeneratedColumn()
7 | id: number;
8 |
9 | @Column()
10 | name: string;
11 |
12 | @Column()
13 | date: Date;
14 |
15 | @Column()
16 | type: HolidaysType;
17 |
18 | }
--------------------------------------------------------------------------------
/src/holidays/holiday.repository.ts:
--------------------------------------------------------------------------------
1 | import { Holiday } from './holiday.entity';
2 | import { EntityRepository, Repository } from 'typeorm';
3 | import { CreateHolidayDto } from './dto/create-holiday.dto';
4 | import { HolidaysType } from './holiday-type.enum';
5 |
6 | @EntityRepository(Holiday)
7 | export class HolidayRepository extends Repository {
8 | async createHoliday(createHolidayDto: CreateHolidayDto): Promise {
9 | const {name, date } = createHolidayDto;
10 |
11 | const holiday = new Holiday();
12 | holiday.name = name;
13 | holiday.date = date;
14 | holiday.type = HolidaysType.NACIONAL;
15 | await holiday.save();
16 |
17 | return holiday;
18 | }
19 | }
--------------------------------------------------------------------------------
/src/holidays/holidays.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get, Post, Body, Param, Delete, Patch} from '@nestjs/common';
2 | import { CreateHolidayDto } from './dto/create-holiday.dto';
3 | //import { Holiday } from './holidays.model';
4 | import { HolidaysService } from './holidays.service';
5 | import {Holiday} from './holiday.entity';
6 | @Controller('holidays')
7 | export class HolidaysController {
8 |
9 | //passamos como um parâmetro no construtor;
10 | //assim qualquer método dessa classe consegue ter acesso a ele;
11 | constructor(private holidaysService: HolidaysService){}
12 |
13 | // @Get()
14 | // getAllHolidays(): Holiday[]{
15 | // return this.holidaysService.getAllHolidays();
16 | // }
17 |
18 | @Get('/:id')
19 | getHolidayById(@Param('id') id: number): Promise{
20 | return this.holidaysService.getHolidayById(id);
21 | }
22 |
23 | @Post()
24 | createHoliday(@Body() createHolidayDto: CreateHolidayDto): Promise{
25 |
26 | return this.holidaysService.createHoliday(createHolidayDto)
27 | }
28 |
29 | @Delete('/:id')
30 | deleteHoliday(@Param('id') id: number): Promise {
31 | return this.holidaysService.deleteHoliday(id);
32 | }
33 |
34 | @Patch('/:id')
35 | updateHoliday(
36 | @Param('id') id: number,
37 | @Body('name') name: string,
38 | @Body('date') date: Date): Promise{
39 | return this.holidaysService.updateHoliday(id, name, date);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/holidays/holidays.model.ts:
--------------------------------------------------------------------------------
1 | //2º passo, veja o primeiro abaixo........
2 | //apagar este arquivo quando criar o Enum;
3 | // export interface Holiday {
4 | // id: string;
5 | // name: string;
6 | // date: Date;
7 | // type: HolidaysType;
8 | // }
9 |
10 | //1º - RECORTAR DAQUI e criar um arquivo holidays-type.enum.ts
11 | // export enum HolidaysType {
12 | // NACIONAL = 'NACIONAL',
13 | // ESTADUAL = 'ESTADUAL',
14 | // MUNICIPAL = 'MUNICIPAL',
15 | // FACULTATIVO = 'FACULTATIVO',
16 | // }
--------------------------------------------------------------------------------
/src/holidays/holidays.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { HolidaysController } from './holidays.controller';
3 | import { HolidaysService } from './holidays.service';
4 | import { TypeOrmModule} from '@nestjs/typeorm';
5 | import { HolidayRepository } from './holiday.repository';
6 |
7 | @Module({
8 | imports: [
9 | TypeOrmModule.forFeature([HolidayRepository])
10 | ],
11 | controllers: [HolidaysController],
12 | providers: [HolidaysService]
13 | })
14 | export class HolidaysModule {}
15 |
--------------------------------------------------------------------------------
/src/holidays/holidays.service.ts:
--------------------------------------------------------------------------------
1 | import { Injectable, NotFoundException } from '@nestjs/common';
2 | import { InjectRepository } from '@nestjs/typeorm';
3 | //import { Holiday, HolidaysType } from './holidays.model';
4 | //import { v4 as uuidv4 } from 'uuid';
5 | import { CreateHolidayDto } from './dto/create-holiday.dto';
6 | import { HolidayRepository } from './holiday.repository';
7 | import { Holiday} from './holiday.entity';
8 | import { HolidaysType } from './holiday-type.enum';
9 | @Injectable()
10 | export class HolidaysService {
11 | constructor(
12 | @InjectRepository(HolidayRepository)
13 | private holidayRepository: HolidayRepository
14 | ){}
15 |
16 | //REMOVER: private holidays: Holiday[] = [];
17 |
18 |
19 | // getAllHolidays(): Holiday[] {
20 | // return this.holidays;
21 | // }
22 |
23 | async getHolidayById(id: number): Promise {
24 | //ele retorna uma promise.
25 | const found = await this.holidayRepository.findOne(id);
26 |
27 | if(!found){
28 | throw new NotFoundException(`Holiday ${id} not found`);
29 | }
30 | return found;
31 | }
32 | // getHolidayById(id: string): Holiday {
33 | // return this.holidays.find(holiday => holiday.id == id)
34 |
35 | // }
36 | async createHoliday(createHolidayDto: CreateHolidayDto): Promise {
37 | return this.holidayRepository.createHoliday(createHolidayDto);
38 | }
39 | // createHoliday(createHolidayDto: CreateHolidayDto){
40 | // const {name, date} = createHolidayDto;
41 |
42 | // const holiday: Holiday = {
43 | // id: uuidv4(),
44 | // name,
45 | // date,
46 | // type: HolidaysType.NACIONAL
47 | // };
48 |
49 | // this.holidays.push(holiday);
50 | // return holiday;
51 | // }
52 |
53 | async deleteHoliday(id: number): Promise {
54 | const result = await this.holidayRepository.delete(id);
55 | console.log(result);
56 | }
57 |
58 | async updateHoliday(id: number, name: string, date: Date): Promise {
59 | const holiday = await this.getHolidayById(id);
60 |
61 | holiday.name = name;
62 | holiday.date = date;
63 | await holiday.save();
64 | return holiday;
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/main.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { AppModule } from './app.module';
3 |
4 | async function bootstrap() {
5 | //2º - vai criar um módulo padrão,com base na Classe AppModule que é a nossa aplicação.
6 | //ele vai utilizar o objeto NestFactory para criar uma instância de uma aplicação
7 | const app = await NestFactory.create(AppModule);
8 | //3º - Internamente o Nest vai utilizar um framework HTTP para subir as requisições e respostas,
9 | //por padrão utiliza o Express e abstrai o Express para dentro do NestJS
10 | await app.listen(3000);
11 | }
12 | //1º - método bootstrap é executado
13 | bootstrap();
14 |
--------------------------------------------------------------------------------
/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 | }
15 | }
16 |
--------------------------------------------------------------------------------