Skip to content

Instantly share code, notes, and snippets.

@feliperfranco
Created April 1, 2018 14:04
Show Gist options
  • Save feliperfranco/b46abc1db79e3aa36b8f1bbf75d6fd4d to your computer and use it in GitHub Desktop.
Save feliperfranco/b46abc1db79e3aa36b8f1bbf75d6fd4d to your computer and use it in GitHub Desktop.
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