64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
|
|
import { Usuario } from '@src/entity/Usuario'
|
||
|
|
import { Request, Response } from 'express'
|
||
|
|
import { validate } from 'class-validator'
|
||
|
|
import { getRepository } from 'typeorm'
|
||
|
|
export class UsuarioController {
|
||
|
|
static async create (req: Request, res: Response) {
|
||
|
|
const {
|
||
|
|
id,
|
||
|
|
nombre,
|
||
|
|
apellidoPaterno,
|
||
|
|
apellidoMaterno,
|
||
|
|
correoAlternativo,
|
||
|
|
numeroTelefonico,
|
||
|
|
identificacion,
|
||
|
|
institucion
|
||
|
|
} = req.body
|
||
|
|
|
||
|
|
const usuario = new Usuario()
|
||
|
|
usuario.id = id
|
||
|
|
usuario.nombre = nombre
|
||
|
|
usuario.apellidoPaterno = apellidoPaterno
|
||
|
|
usuario.apellidoMaterno = apellidoMaterno
|
||
|
|
usuario.correoAlternativo = correoAlternativo
|
||
|
|
usuario.numeroTelefonico = numeroTelefonico
|
||
|
|
usuario.identificacion = identificacion
|
||
|
|
usuario.institucion = institucion
|
||
|
|
|
||
|
|
const errors = await validate(usuario)
|
||
|
|
if (errors.length > 0) {
|
||
|
|
res.status(400).send(errors)
|
||
|
|
}
|
||
|
|
|
||
|
|
const repository = getRepository(Usuario)
|
||
|
|
try {
|
||
|
|
await repository.save(usuario)
|
||
|
|
} catch (e) {
|
||
|
|
res.status(409).send(e.message)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
res.status(201).send('User create')
|
||
|
|
}
|
||
|
|
|
||
|
|
static async getAll (req: Request, res: Response) {
|
||
|
|
const respository = getRepository(Usuario)
|
||
|
|
const result = await respository.find({
|
||
|
|
select: ['id', 'nombre', 'apellidoMaterno', 'apellidoPaterno']
|
||
|
|
})
|
||
|
|
res.status(200).json(result)
|
||
|
|
}
|
||
|
|
|
||
|
|
static async getOne (req: Request, res: Response) {
|
||
|
|
const respository = getRepository(Usuario)
|
||
|
|
const id: number = +req.params.id
|
||
|
|
try {
|
||
|
|
const result = await respository.findOneOrFail(id, {
|
||
|
|
select: ['id', 'nombre', 'apellidoMaterno', 'apellidoPaterno']
|
||
|
|
})
|
||
|
|
res.status(200).json(result)
|
||
|
|
} catch (e) {
|
||
|
|
res.status(404).send('Usuario no encontrado')
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|