如何在AngularFireDatabase上搜索特定的寄存器?

时间:2018-12-03 15:27:33

标签: angular typescript firebase firebase-realtime-database

我正在使用Angular + Firebase开发一个应用程序,并且我坚持如何获取记录以及如何通过特定属性查找记录。当注册N笔分期付款的贷款时,我需要用新贷款的相应$键来注册N笔分期付款。

  • 需要获得完整的注册记录(例如贷款)
  • 需要按任何属性搜索寄存器

//服务

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) { }

  //GET LOANS
  obtenerPrestamos()
  {
    return this.prestamoLista = this.firebase.list('prestamos');
  }

  //INSERT LOAN
  insertarPrestamo(prestamo: Prestamo)
  {
    this.prestamoLista.push(
      {
        codigo: prestamo.codigo,
        fechaAlta: prestamo.fechaAlta,
        idcliente: prestamo.idCliente,
        cantidadCuotas: prestamo.cantidadCuotas,
        montoCuota: prestamo.montoCuota
      }
    );

  }

  //MODIFY LOAN
  modificarPrestamo(prestamo: Prestamo)
  {
    this.prestamoLista.update(prestamo.$key,
      {
        codigo: prestamo.codigo,
        fechaAlta: prestamo.fechaAlta,
        idcliente: prestamo.idCliente,
        cantidadCuotas: prestamo.cantidadCuotas,
        montoCuota: prestamo.montoCuota

      });
  }

  //DELETE LOAN
  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;

  }



}

0 个答案:

没有答案