generated from HenriqueAmorim20/GEROcuidadoAPITemplate
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from fga-eps-mds/feature/#116-CRUD-metricas
Feature/#116 crud metricas
- Loading branch information
Showing
23 changed files
with
991 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export enum ECategoriaMetrica { | ||
FREQUENCIA_CARDIACA = 'Frequência Cardíaca', | ||
PRESSAO_SANGUINEA = 'Pressão', | ||
TEMPERATURA = 'Temperatura', | ||
PESO = 'Peso', | ||
GLICEMIA = 'Glicemia', | ||
SATURACAO = 'Saturação', | ||
HORAS_DORMIDAS = 'Horas Dormidas', | ||
ALTURA = 'Altura', | ||
IMC = 'IMC', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { IsEnum, IsNotEmpty, IsNumber } from "class-validator"; | ||
import { ECategoriaMetrica } from "../classes/tipo-metrica.enum"; | ||
|
||
export class CreateMetricaDto { | ||
@IsNotEmpty() | ||
@IsNumber() | ||
idIdoso!: number; | ||
|
||
@IsNotEmpty() | ||
@IsEnum(ECategoriaMetrica) | ||
categoria?: ECategoriaMetrica; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { PartialType } from "@nestjs/mapped-types"; | ||
import { CreateMetricaDto } from "./create-metrica-dto"; | ||
|
||
export class UpdateMetricaDto extends PartialType(CreateMetricaDto) { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from "typeorm"; | ||
import { Idoso } from "../../idoso/entities/idoso.entity"; | ||
import { ECategoriaMetrica } from "../classes/tipo-metrica.enum"; | ||
import { CreateMetricaDto } from "../dto/create-metrica-dto"; | ||
import { UpdateMetricaDto } from "../dto/update-metrica-dto"; | ||
|
||
@Entity({ name: 'metrica' }) | ||
export class Metrica { | ||
@PrimaryGeneratedColumn() | ||
id!: number; | ||
|
||
@ManyToOne(() => Idoso) | ||
@JoinColumn({ name: 'idIdoso' }) | ||
idIdoso!: number; | ||
|
||
@Column('enum', { enum: ECategoriaMetrica }) | ||
categoria!: ECategoriaMetrica; | ||
|
||
constructor(createMetricaDto: CreateMetricaDto | UpdateMetricaDto) { | ||
Object.assign(this, createMetricaDto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface IMetricaFilter { | ||
idIdoso?: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { Filtering } from '../shared/decorators/filtrate.decorator'; | ||
import { OrderParams, Ordering } from '../shared/decorators/ordenate.decorator'; | ||
import { | ||
Pagination, | ||
PaginationParams, | ||
} from '../shared/decorators/paginate.decorator'; | ||
import { ECategoriaMetrica } from './classes/tipo-metrica.enum'; | ||
import { Metrica } from './entities/metrica.entity'; | ||
import { IMetricaFilter } from './interfaces/metrica-filter.interface'; | ||
import { MetricaController } from './metrica.controller'; | ||
import { MetricaService } from './metrica.service'; | ||
|
||
describe('MetricaController', () => { | ||
let controller: MetricaController; | ||
let service: MetricaService; | ||
|
||
const metricaDto = { | ||
idIdoso: 1, | ||
categoria: ECategoriaMetrica.FREQUENCIA_CARDIACA, | ||
}; | ||
|
||
const metrica = { | ||
...metricaDto, | ||
id: 1, | ||
}; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [], | ||
controllers: [MetricaController], | ||
providers: [ | ||
{ | ||
provide: MetricaService, | ||
useValue: { | ||
create: jest.fn(), | ||
findOne: jest.fn(), | ||
remove: jest.fn(), | ||
update: jest.fn(), | ||
findAll: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: getRepositoryToken(Metrica), | ||
useValue: {}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
controller = module.get<MetricaController>(MetricaController); | ||
service = module.get<MetricaService>(MetricaService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
|
||
it('should create IMetrica', async () => { | ||
jest | ||
.spyOn(service, 'create') | ||
.mockReturnValue(Promise.resolve(metrica as Metrica)); | ||
|
||
const response = await controller.create(metricaDto); | ||
expect(response.data).toEqual(metrica); | ||
expect(response.message).toEqual('Salvo com sucesso!'); | ||
}); | ||
|
||
it('should find IMetrica', async () => { | ||
jest | ||
.spyOn(service, 'findOne') | ||
.mockReturnValue(Promise.resolve(metrica as Metrica)); | ||
|
||
const response = await controller.findOne({ id: 1 }); | ||
expect(response).toEqual(metrica); | ||
}); | ||
|
||
it('should remove IMetrica', async () => { | ||
jest | ||
.spyOn(service, 'remove') | ||
.mockReturnValue(Promise.resolve(metrica as Metrica)); | ||
|
||
const response = await controller.remove({ id: 1 }); | ||
expect(response.data).toEqual(metrica); | ||
expect(response.message).toEqual('Excluído com sucesso!'); | ||
}); | ||
|
||
it('should update IMetrica', async () => { | ||
jest | ||
.spyOn(service, 'update') | ||
.mockReturnValue(Promise.resolve(metrica as Metrica)); | ||
|
||
const response = await controller.update({ id: 1 }, { idIdoso: 2 }); | ||
expect(response.data).toEqual(metrica); | ||
expect(response.message).toEqual('Atualizado com sucesso!'); | ||
}); | ||
|
||
describe('findAll', () => { | ||
const filter: IMetricaFilter = { | ||
idIdoso: 1, | ||
}; | ||
const filtering = new Filtering<Metrica>(JSON.stringify(filter)); | ||
|
||
const order: OrderParams = { | ||
column: 'id', | ||
dir: 'ASC', | ||
}; | ||
const ordering: Ordering = new Ordering(JSON.stringify(order)); | ||
|
||
const paginate: PaginationParams = { | ||
limit: 10, | ||
offset: 0, | ||
}; | ||
const pagination: Pagination = new Pagination(paginate); | ||
|
||
it('should findAll Metrica', async () => { | ||
const expected = { data: [metrica], count: 1, pageSize: 1 }; | ||
|
||
jest.spyOn(service, 'findAll').mockReturnValue(Promise.resolve(expected)); | ||
|
||
const { data, count, pageSize } = await controller.findAll( | ||
filtering as any, | ||
pagination, | ||
ordering, | ||
); | ||
|
||
expect(count).toEqual(1); | ||
expect(pageSize).toEqual(1); | ||
expect(data).toEqual([metrica]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
Param, | ||
Patch, | ||
Post, | ||
Delete, | ||
} from '@nestjs/common'; | ||
import { MetricaService } from './metrica.service'; | ||
import { Response } from '../shared/interceptors/data-transform.interceptor'; | ||
import { Paginate, Pagination } from '../shared/decorators/paginate.decorator'; | ||
import { Ordenate, Ordering } from '../shared/decorators/ordenate.decorator'; | ||
import { ResponsePaginate } from '../shared/interfaces/response-paginate.interface'; | ||
import { Metrica } from './entities/metrica.entity'; | ||
import { IdValidator } from '../shared/validators/id.validator'; | ||
import { UpdateMetricaDto } from './dto/update-metrica-dto'; | ||
import { HttpResponse } from '../shared/classes/http-response'; | ||
import { CreateMetricaDto } from './dto/create-metrica-dto'; | ||
import { PublicRoute } from '../shared/decorators/public-route.decorator'; | ||
import { Filtering, Filtrate } from '../shared/decorators/filtrate.decorator'; | ||
import { IMetricaFilter } from './interfaces/metrica-filter.interface'; | ||
|
||
@Controller('metrica') | ||
export class MetricaController { | ||
constructor(private readonly _service: MetricaService) {} | ||
|
||
@Get() | ||
@PublicRoute() | ||
async findAll( | ||
@Filtrate() queryParam: Filtering<IMetricaFilter>, | ||
@Paginate() pagination: Pagination, | ||
@Ordenate() ordering: Ordering, | ||
): Promise<ResponsePaginate<Metrica[]>> { | ||
return this._service.findAll(queryParam.filter, ordering, pagination); | ||
} | ||
@Get(':id') | ||
async findOne(@Param() param: IdValidator): Promise<Metrica> { | ||
return this._service.findOne(param.id); | ||
} | ||
@Patch(':id') | ||
async update( | ||
@Param() param: IdValidator, | ||
@Body() body: UpdateMetricaDto, | ||
): Promise<Response<Metrica>> { | ||
const updated = await this._service.update(param.id, body); | ||
return new HttpResponse<Metrica>(updated).onUpdated(); | ||
} | ||
@Post() | ||
async create(@Body() body: CreateMetricaDto) { | ||
const created = await this._service.create(body); | ||
return new HttpResponse<Metrica>(created).onCreated(); | ||
} | ||
|
||
@Delete(':id') | ||
async remove(@Param() param: IdValidator): Promise<Response<unknown>> { | ||
const deleted = await this._service.remove(param.id); | ||
return new HttpResponse(deleted).onDeleted(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { Metrica } from './entities/metrica.entity'; | ||
import { MetricaController } from './metrica.controller'; | ||
import { MetricaService } from './metrica.service'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Metrica])], | ||
controllers: [MetricaController], | ||
providers: [MetricaService, Repository], | ||
exports: [MetricaService], | ||
}) | ||
export class MetricaModule { } |
Oops, something went wrong.