import { Injectable } from '@angular/core' ; import { AngularFireDatabase } from '@angular/fire/database' ; import { Customer } from '../models/customer.model' ; import { of } from 'rxjs' ; import { AngularFireAuth } from '@angular/fire/auth' ; @ Injectable ( { providedIn : 'root' } ) export class CustomersService { constructor ( private db , private afAuth ) { } get userId ( ) { if ( this . afAuth . auth . currentUser ) { return this . afAuth . auth . currentUser . uid ; } } add ( customer , userId ) { const customers = this . db . list ( `customers/${ userId }` ) ; return customers . push ( customer ) ; } addCustomers ( customers ) { const userId = this . userId ; if ( userId ) { customers . forEach ( ( customer ) => { this . db . list ( `customers/${ userId }` ) . push ( customer ) ; } ) ; } } get ( userId ) { return this . db . list ( `customers/${ userId }` ) . snapshotChanges ( ) ; } update ( customer , userId ) { return of ( this . db . object ( `customers/${ userId }/` + customer . key ) . update ( { id : customer . id , name : customer . name , description : customer . description , } ) ) ; } delete ( customer , userId ) { return this . db . object ( `customers/${ userId }/` + customer . key ) . remove ( ) ; } }