Created
April 1, 2018 14:04
-
-
Save feliperfranco/b46abc1db79e3aa36b8f1bbf75d6fd4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AngularFireAuth } from 'angularfire2/auth'; | |
import { AngularFireDatabase } from 'angularfire2/database'; | |
import { Injectable } from '@angular/core'; | |
@Injectable() | |
export class ContatoProvider { | |
private PATH = 'contatos/'; | |
constructor(private db: AngularFireDatabase, public auth: AngularFireAuth) { | |
this.PATH += this.auth.auth.currentUser.uid; | |
} | |
getAll() { | |
return this.db.list(this.PATH) | |
.snapshotChanges() | |
.map(changes => { | |
return changes.map(m => ({ key: m.key, ...m.payload.val() })); | |
}); | |
} | |
save(item: any) { | |
return new Promise((resolve, reject) => { | |
if (item.key) { | |
this.db.list(this.PATH).update(item.key, item) | |
.then(() => resolve()) | |
.catch((e) => reject(e)); | |
} else { | |
this.db.list(this.PATH).push(item) | |
.then(() => resolve()); | |
} | |
}); | |
} | |
remove(key: string) { | |
return this.db.list(this.PATH).remove(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment