programa listo
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
import { IsString } from 'class-validator';
|
||||
|
||||
export class ProgramaCreateDto {
|
||||
@IsString()
|
||||
programa: string;
|
||||
}
|
||||
@@ -1,10 +1,18 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { Body, Controller, Get, Post } from '@nestjs/common';
|
||||
import { ProgramaService } from './programa.service';
|
||||
import { ProgramaCreateDto } from './dto/programa-create.dto';
|
||||
|
||||
@Controller('programa')
|
||||
export class ProgramaController {
|
||||
constructor(private programaService: ProgramaService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() body: ProgramaCreateDto) {
|
||||
return this.programaService.create(body.programa);
|
||||
}
|
||||
|
||||
@Get()
|
||||
get() {}
|
||||
get() {
|
||||
return this.programaService.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConflictException, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Programa } from './entity/programa.entity';
|
||||
@@ -8,4 +8,19 @@ export class ProgramaService {
|
||||
constructor(
|
||||
@InjectRepository(Programa) private repository: Repository<Programa>,
|
||||
) {}
|
||||
|
||||
create(programa: string) {
|
||||
return this.repository
|
||||
.findOne({ programa })
|
||||
.then((existePrograma) => {
|
||||
if (existePrograma)
|
||||
throw new ConflictException('Ya existe este programa.');
|
||||
return this.repository.save(this.repository.create({ programa }));
|
||||
})
|
||||
.then((_) => ({ message: 'Se creo correctamente el programa.' }));
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return this.repository.find();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user