import { Injectable } from '@angular/core' ; import { AngularFireDatabase , AngularFireList , AngularFireObject } from 'angularfire2/database' ; import { Observable } from 'rxjs/Observable' ; import { Card } from '../models/card' ; @ Injectable ( ) export class CardService { private basePath = '/items' ; cardsRef : AngularFireList < Card > ; cardRef : AngularFireObject < Card > ; constructor ( private db ) { this . cardsRef = db . list ( '/cards' ) ; } getCardsList ( ) { return this . cardsRef . snapshotChanges ( ) . map ( ( arr ) => { return arr . map ( ( snap ) => Object . assign ( snap . payload . val ( ) , { $key : snap . key } ) ) ; } ) ; } getCard ( key ) { const cardPath = `${ this . basePath }/${ key }` ; const card = this . db . object ( cardPath ) . valueChanges ( ) as Observable < Card | null > ; return card ; } createCard ( card ) { const promise = this . cardsRef . push ( card ) ; card . $key = promise . key ; return card ; } updateCard ( key , value ) { this . cardsRef . update ( key , value ) ; } deleteCard ( key ) { this . cardsRef . remove ( key ) ; } deleteAll ( ) { this . cardsRef . remove ( ) ; } private handleError ( error ) { console . error ( error ) ; } }