From 1308498603c4da9cbf3a0f3ca01de84b1158202c Mon Sep 17 00:00:00 2001 From: Eduardo Quiros Date: Sun, 11 Jul 2021 13:44:20 -0600 Subject: [PATCH] no registrar categoria si la nueva tiene un nombre duplicado --- .../update/categoria-update.component.ts | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/webapp/app/entities/categoria/update/categoria-update.component.ts b/src/main/webapp/app/entities/categoria/update/categoria-update.component.ts index b5d24ae..45f8a09 100644 --- a/src/main/webapp/app/entities/categoria/update/categoria-update.component.ts +++ b/src/main/webapp/app/entities/categoria/update/categoria-update.component.ts @@ -14,6 +14,7 @@ import { CategoriaService } from '../service/categoria.service'; }) export class CategoriaUpdateComponent implements OnInit { isSaving = false; + public categorias?: ICategoria[]; editForm = this.fb.group({ id: [], @@ -21,12 +22,22 @@ export class CategoriaUpdateComponent implements OnInit { estado: [null, [Validators.required]], }); - constructor(protected categoriaService: CategoriaService, protected activatedRoute: ActivatedRoute, protected fb: FormBuilder) {} + constructor(protected categoriaService: CategoriaService, protected activatedRoute: ActivatedRoute, protected fb: FormBuilder) { + this.categorias = []; + this.loadAll(); + } ngOnInit(): void { this.activatedRoute.data.subscribe(({ categoria }) => { this.updateForm(categoria); }); + this.loadAll(); + } + + loadAll(): void { + this.categoriaService.query().subscribe(res => { + this.categorias = res.body ?? []; + }); } previousState(): void { @@ -36,13 +47,25 @@ export class CategoriaUpdateComponent implements OnInit { save(): void { this.isSaving = true; const categoria = this.createFromForm(); - if (categoria.id !== undefined) { - this.subscribeToSaveResponse(this.categoriaService.update(categoria)); + const condicion = this.categoryExists(categoria); + if (!condicion) { + if (categoria.id !== undefined) { + this.subscribeToSaveResponse(this.categoriaService.update(categoria)); + } else { + this.subscribeToSaveResponse(this.categoriaService.create(categoria)); + } } else { - this.subscribeToSaveResponse(this.categoriaService.create(categoria)); + this.previousState(); } } + protected categoryExists(categoria: ICategoria): boolean { + this.loadAll(); + var condicion = this.categorias!.some(cat => cat.nombre === categoria.nombre); + debugger; + return condicion; + } + protected subscribeToSaveResponse(result: Observable>): void { result.pipe(finalize(() => this.onSaveFinalize())).subscribe( () => this.onSaveSuccess(),