I'm doing an app with Angular + Firebase, and I'm stuck on how to get a record and how to look for it by a certain attribute . When registering a loan with N installments, when registering it, I need to register the N installments with the respective $ key of the new loan.
// SERVICE
import { Injectable } from '@angular/core';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { Prestamo } from '../models/prestamo';
@Injectable({
providedIn: 'root'
})
export class PrestamoService {
prestamoLista: AngularFireList<any>;
prestamoSeleccionado: Prestamo = new Prestamo();
constructor(private firebase: AngularFireDatabase) { }
obtenerPrestamos()
{
return this.prestamoLista = this.firebase.list('prestamos');
}
insertarPrestamo(prestamo: Prestamo)
{
this.prestamoLista.push(
{
codigo: prestamo.codigo,
fechaAlta: prestamo.fechaAlta,
idcliente: prestamo.idCliente,
cantidadCuotas: prestamo.cantidadCuotas,
montoCuota: prestamo.montoCuota
}
);
}
modificarPrestamo(prestamo: Prestamo)
{
this.prestamoLista.update(prestamo.$key,
{
codigo: prestamo.codigo,
fechaAlta: prestamo.fechaAlta,
idcliente: prestamo.idCliente,
cantidadCuotas: prestamo.cantidadCuotas,
montoCuota: prestamo.montoCuota
});
}
eliminarPrestamo($key: string)
{
this.prestamoLista.remove($key);
}
// TS
import { Component, OnInit } from '@angular/core';
import { NgForm } from "@angular/forms";
import { FormControl } from '@angular/forms';
//Toastr
import { ToastrService } from "ngx-toastr";
//Service
import { ClienteService } from "../../../services/cliente.service";
import { PrestamoService } from "../../../services/prestamo.service";
import { CuotaService } from "../../../services/cuota.service";
//Clase
import { Prestamo } from 'src/app/models/prestamo';
import { Cliente } from 'src/app/models/cliente';
import { Cuota } from "src/app/models/cuota";
import { IfStmt } from '@angular/compiler';
import { stringify } from '@angular/core/src/util';
import { $ } from 'protractor';
@Component({
selector: 'app-prestamo',
templateUrl: './prestamo.component.html',
styleUrls: ['./prestamo.component.css']
})
export class PrestamoComponent implements OnInit {
constructor(private prestamoService: PrestamoService,
private clienteService: ClienteService,
private toastr: ToastrService
) { }
ngOnInit() {
}
onSubmit(FormularioPrestamo: NgForm)
{
if (this.validar(FormularioPrestamo))
{
FormularioPrestamo.value.idCliente = FormularioPrestamo.value.$keyCliente;
if(FormularioPrestamo.value.$key == null)
{
this.prestamoService.insertarPrestamo(FormularioPrestamo.value);
this.toastr.success('Operación Exitosa', 'Préstamo Agregado');
}
else
{
this.prestamoService.modificarPrestamo(FormularioPrestamo.value);
this.toastr.success('Operación Exitosa', 'Préstamo Modificado');
}
this.limpiarFormulario(FormularioPrestamo);
}
}
limpiarFormulario(FormularioPrestamo?: NgForm)
{
if(FormularioPrestamo != null)
FormularioPrestamo.reset();
this.prestamoService.prestamoSeleccionado = new Prestamo();
this.clienteService.clienteSeleccionado = new Cliente();
}
validar(FormularioPrestamo: NgForm) : boolean
{
if(FormularioPrestamo.value.$keyCliente == undefined)
{
this.toastr.warning('Debe seleccionar un Cliente', 'Atención');
return false;
}
return true;
}
}