Create, Read, Update and Delete angular Firestore | Angular 6





import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection,
AngularFirestoreDocument } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';
import { Employee} from '../model/Employee';
@Injectable()
export class EmployeeService {
userscollection: AngularFirestoreCollection<Employee>;
users: Observable<Employee[]>;
userDoc: AngularFirestoreDocument<Employee>;
constructor(public _afs: AngularFirestore) {

}
getData() {
this.userscollection = this._afs.collection('employees', x => x.orderBy('firstName', 'asc'));
this.users = this.userscollection.snapshotChanges().map(
changes => {
return changes.map(
a => {
const data = a.payload.doc.data() as Employee;
data.id = a.payload.doc.id;
return data;
});

});
return this.users;
}
insertEmployee(employee : Employee) {
this._afs.collection('employees').add({
firstName: employee.firstName ,
secondName: employee.secondName ,
gender: employee.gender ,
dateOfBirthday: employee.dateOfBirthday ,
category: employee.category ,
membership: employee.membership ,
address1: employee.address1 ,
state: employee.state ,
address2: employee.address2 ,
postcode: employee.postcode ,
city: employee.city ,
country: employee.country
});
}
deleteEmployee(id) {
this.userDoc = this._afs.doc(`employees/${id}`);
this.userDoc.delete();
}
getDataById(id){
this.userDoc = this._afs.doc(`employees/${id}`); 
return this.userDoc;
}
updateEmployee(employee : Employee){
this.userDoc.update(
{     
firstName: employee.firstName ,
secondName: employee.secondName ,
gender: employee.gender ,
dateOfBirthday: employee.dateOfBirthday ,
category: employee.category ,
membership: employee.membership ,
address1: employee.address1 ,
state: employee.state ,
address2: employee.address2 ,
postcode: employee.postcode ,
city: employee.city ,
country: employee.country
}
);
}


}



export class Employee {
id:string;
firstName: string;
secondName: string;
gender: string;
dateOfBirthday: string;
category: string;
membership: string;
address1: string;
state: string;
address2: string;
postcode: string;
city: string;
country: string;
power: string;
alterEgo?: string
}



Comments

Popular posts from this blog

Easy Ui Jquery easyui-textbox change onChange event

Provision AWS EC2 Instance and RDS with Terraform, and Deploy Spring Boot App to EC2 Instance via GitHub Action Pipeline

npm install gets stuck at fetchMetadata