marca service final

This commit is contained in:
lemuel 2022-12-23 07:50:47 -06:00
parent 5200cc1ba4
commit 7de9aa4046
2 changed files with 9 additions and 18 deletions

View File

@ -7,9 +7,9 @@ import { Marca } from './entity/marca.entity';
export class MarcaService {
constructor(@InjectRepository(Marca) private repository: Repository<Marca>) {}
create(marca: string, tipo: string) {
// Buscamos una marca con ese nombre
return this.findByMarca(marca, tipo, false)
create(marca: string, tipo: string): Promise<{ message: string }> {
// Buscamos un registro con ese nombre
return this.findByMarca(marca, tipo)
.then((existeMarca) => {
// Sacamos error si existe
if (existeMarca) throw new ConflictException('Ya existe esta marca.');
@ -19,25 +19,20 @@ export class MarcaService {
.then((_) => ({ message: 'Se creó correctamente una nueva marca.' }));
}
findAll(tipo: string) {
findAll(tipo: string): Promise<Marca[]> {
return this.repository.find({ where: { tipo }, order: { tipo: 'ASC' } });
}
findById(id_marca: number, tipo: string, validarExistencia = true) {
findById(id_marca: number, tipo: string): Promise<Marca> {
return this.repository
.findOne({ where: { id_marca, tipo } })
.then((marca) => {
if (validarExistencia && !marca)
throw new ConflictException('No existe esta marca.');
if (!marca) throw new ConflictException('No existe este id marca.');
return marca;
});
}
findByMarca(marca: string, tipo: string, validarExistencia = true) {
return this.repository.findOne({ where: { marca, tipo } }).then((marca) => {
if (validarExistencia && !marca)
throw new ConflictException('No existe esta marca.');
return marca;
});
findByMarca(marca: string, tipo: string): Promise<Marca> {
return this.repository.findOne({ where: { marca, tipo } });
}
}

View File

@ -190,11 +190,7 @@ export class UploadFileService {
institucion,
dataEquipo.modulo,
);
const marca = await this.marcaService.findByMarca(
dataEquipo.marca,
'e',
false,
);
const marca = await this.marcaService.findByMarca(dataEquipo.marca, 'e');
const modelo = await this.modeloService.findByModelo(
dataEquipo.modelo,
'e',