listo tipo usuario

This commit is contained in:
xXpuma99Xx
2022-04-20 16:02:05 -05:00
parent 74c05d5071
commit 73129b313f
3 changed files with 36 additions and 4 deletions
@@ -0,0 +1,6 @@
import { IsString } from 'class-validator';
export class TipoUsuarioUpdateDto {
@IsString()
tipo_usuario: string;
}
+10 -3
View File
@@ -1,10 +1,17 @@
import { Controller, Get } from '@nestjs/common';
import { Body, Controller, Get, Post } from '@nestjs/common';
import { TipoUsuarioService } from './tipo-usuario.service';
import { TipoUsuarioUpdateDto } from './dto/tipo-usuario-update.dto';
@Controller('tipo-usuario')
export class TipoUsuarioController {
constructor(private tipoUsuarioService: TipoUsuarioService) {}
@Post()
create(@Body() body: TipoUsuarioUpdateDto) {
return this.tipoUsuarioService.create(body.tipo_usuario);
}
@Get()
get() {}
get() {
return this.tipoUsuarioService.findAll();
}
}
+20 -1
View File
@@ -1,4 +1,8 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import {
ConflictException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { TipoUsuario } from './entity/tipo-usuario.entity';
@@ -9,6 +13,21 @@ export class TipoUsuarioService {
@InjectRepository(TipoUsuario) private repository: Repository<TipoUsuario>,
) {}
create(tipo_usuario: string) {
return this.repository
.findOne({ tipo_usuario })
.then((existeTipoUsuario) => {
if (existeTipoUsuario)
throw new ConflictException('Ya existe este tipo usuario');
return this.repository.save(this.repository.create({ tipo_usuario }));
})
.then((_) => ({ message: 'Se creo correctamente el tipo usuario.' }));
}
findAll() {
return this.repository.find();
}
findById(id_tipo_usuario: number) {
return this.repository.findOne({ id_tipo_usuario }).then((tipoUsuario) => {
if (!tipoUsuario)