no registrar categoria si la nueva tiene un nombre duplicado

This commit is contained in:
Eduardo Quiros 2021-07-11 13:44:20 -06:00
parent 10b98b1b8e
commit 1308498603
No known key found for this signature in database
GPG Key ID: B77F36C3F12720B4
1 changed files with 27 additions and 4 deletions

View File

@ -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<HttpResponse<ICategoria>>): void {
result.pipe(finalize(() => this.onSaveFinalize())).subscribe(
() => this.onSaveSuccess(),