diff --git a/src/tipo-entrada/dto/tipo-entrada-create.dto.ts b/src/tipo-entrada/dto/tipo-entrada-create.dto.ts new file mode 100644 index 0000000..7c09d61 --- /dev/null +++ b/src/tipo-entrada/dto/tipo-entrada-create.dto.ts @@ -0,0 +1,6 @@ +import { IsString } from 'class-validator'; + +export class TipoEntradaCreateDto { + @IsString() + tipo_entrada: string; +} diff --git a/src/tipo-entrada/tipo-entrada.controller.ts b/src/tipo-entrada/tipo-entrada.controller.ts index f3bf954..cf11e72 100644 --- a/src/tipo-entrada/tipo-entrada.controller.ts +++ b/src/tipo-entrada/tipo-entrada.controller.ts @@ -1,12 +1,20 @@ -import { Controller, Get } from '@nestjs/common'; +import { Body, Controller, Get, Post } from '@nestjs/common'; import { TipoEntradaService } from './tipo-entrada.service'; -import {ApiTags} from '@nestjs/swagger' +import { TipoEntradaCreateDto } from './dto/tipo-entrada-create.dto' +import { ApiTags } from '@nestjs/swagger' @Controller('tipo-entrada') @ApiTags('tipo-entrada') export class TipoEntradaController { constructor(private tipoEntradaService: TipoEntradaService) {} + @Post() + create(@Body() body: TipoEntradaCreateDto) { + return this.tipoEntradaService.create(body.tipo_entrada) + } + @Get() - get() {} + get() { + return this.tipoEntradaService.findAll() + } } diff --git a/src/tipo-entrada/tipo-entrada.service.ts b/src/tipo-entrada/tipo-entrada.service.ts index 76efa73..9f73af0 100644 --- a/src/tipo-entrada/tipo-entrada.service.ts +++ b/src/tipo-entrada/tipo-entrada.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, ConflictException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { TipoEntrada } from './entity/tipo-entrada.entity'; @@ -7,5 +7,22 @@ import { TipoEntrada } from './entity/tipo-entrada.entity'; export class TipoEntradaService { constructor( @InjectRepository(TipoEntrada) private repository: Repository, - ) {} + ) { } + + async create(tipo_entrada: string) { + const nuevoTipoEntrada = this.repository.create({ + tipo_entrada + }) + + return this.repository.findOne({ tipo_entrada }) + .then((existeTipoEntrada) => { + if(existeTipoEntrada) throw new ConflictException('Ya existe este tipo de entrada') + return this.repository.save(nuevoTipoEntrada) + }) + .then(()=> ({message: 'Se creo correctamente la nueva entrada'})) + } + + findAll() { + return this.repository.find() + } }