CRUD (Create, Read, Update, Delete) Application Using Fire Store (Firebase)
CRUD (Create, Read, Update, Delete) Application Using Fire Store (Firebase)
CRUD (Create, Read, Update, Delete) Application Using Fire Store (Firebase)
firebaseConfig : {
apiKey: "AIzaSyDmYt52dYyTy1H0iuJ96fkiOY-qiWszm44",
authDomain: "firestorecrud-73178.firebaseapp.com",
databaseURL: "https://firestorecrud-73178.firebaseio.com",
projectId: "firestorecrud-73178",
storageBucket: "firestorecrud-73178.appspot.com",
messagingSenderId: "620422584742",
appId: "1:620422584742:web:b05429eea888e8d89b1767",
measurementId: "G-M31FYVZDHK"
}
@NgModule({
declarations: [
AppComponent,
EmployeesComponent,
EmployeeComponent,
EmployeeListComponent
],
imports: [
BrowserModule,
AngularFireModule.initializeApp(environment.firebaseConfig),
AngularFirestoreModule,
AngularFontAwesomeModule,
FormsModule,
BrowserAnimationsModule,
ToastrModule.forRoot()
],
@import '~bootstrap/dist/css/bootstrap.min.css';
@import '~font-awesome/css/font-awesome.css';
9. Create one parent component inside app folder.
ng g c employees --spec=false
10. Create two child components inside employees folder
ng g c employees/employee --spec=false
ng g c employees/employee-list --spec=false
16. Add property to employee class, inside employee.model.ts inside shared folder.
export class Employee {
id: string;
fullName: string;
empCode: string;
position: string;
mobile: string;
}
17. If the property is used in many components then add the property inside service.class inside
shared folder and inject the property to app.module.ts
File: (employee.service.ts)
export class EmployeeService {
formData: Employee;
constructor() { }
File: (app.module.ts)
import { EmployeeService } from './shared/employee.service';
providers: [EmployeeService],
File: (employee.component.ts)
import { EmployeeService } from 'src/app/shared/employee.service';
</form>
File: (employee.component.ts)
ngOnInit() {
this.resetForm();
}
input.ng-touched.ng-invalid{
border-color: #dc3545;
}
div.validation-error{
width: 100%;
margin-top: .25rem;
font-size: 80%;
color: #dc3545;
}
22. Function once user click Submit button
File: (employee.component.ts)
onSubmit(form: NgForm) {
let data = Object.assign({}, form.value);
delete data.id;
if (form.value.id == null)
{
this.firestore.collection('employees').add(data);
this.toastr.success('Submitted successfully', 'EMP. Register');
}
else
{
this.firestore.doc('employees/' + form.value.id).update(data);
this.toastr.warning('Updated successfully', 'EMP. Register');}
this.resetForm(form);
}
"styles": [
"src/styles.css",
"node_modules/ngx-toastr/toastr.css",
"node_modules/font-awesome/css/font-awesome.css"
],
imports: [
BrowserModule,
AngularFireModule.initializeApp(environment.firebaseConfig),
AngularFirestoreModule,
AngularFontAwesomeModule,
FormsModule,
BrowserAnimationsModule,
ToastrModule.forRoot()
],
getEmployees() {
return this.firestore.collection('employees').snapshotChanges();
}
35. Check the employee-list table and it should be show the correct data from firebase
36. Edit the data in the employee-list component table
onEdit(emp: Employee) {
this.service.formData = Object.assign({}, emp);
}
onDelete(id: string) {
if (confirm("Are you sure to delete this record?")) {
this.firestore.doc('employees/' + id).delete();
this.toastr.error('Deleted successfully','Employee. Delete', { ti
meOut: 950});
this.resetForm();
}
}
40.