You can find all of my .NET Core posts
here and the source code of my sample application
here.
This post is Part Two of the articles on Angular 4 + Core 2.0 CRUD operations.
In my previous post (Part I), I explained how to do Database first in .NET Core 2.0 and we created Employee Web API using Database First approach. This, you can find here.
In this post, we will add different Angular component classes to perform the CRUD operation using an API that we created in this post and will show the details on UI as shown below.
Let us start.
Set routes for API methods
Our first task is to change the routes in API methods so that it would be easier for us to give the path to Angular component pages (Whole class here). For example:
- [Route("~/api/GetAllEmployees")]
- [HttpGet]
- public IEnumerable GetEmployees() {
- return _context.Employees;
- }
Add Employee Folder
Add Employee folder on the ClientApp folder, as shown below.
Get Employees
Let us first add the code to get all employees and later, we will add the code for Add, Update, Delete.
For this, we will follow these steps.
- Add EmployeeService TypeScript file
- Add Employee HTML file
- Add Employee component class
- Add EmployeeComponent in the ApModule
- Add Employee Management in the Navigation Menu
Add EmployeeService TypeScript file
We will add EmployeeService TypeScript file under Employee folder. Add the below code in the EmployeeService.ts class:
-
- import {
- Injectable
- } from "@angular/core";
- import {
- Http,
- Response,
- Headers
- } from "@angular/http";
- import "rxjs/add/operator/map";
- import 'rxjs/add/operator/do';
- import {
- Observable
- } from "rxjs/Observable";
- import {
- BehaviorSubject
- } from 'rxjs/BehaviorSubject';
-
- @Injectable()
- export class EmployeeService {
- public employeeList: Observable < Employee[] > ;
- private _employeeList: BehaviorSubject < Employee[] > ;
- private baseUrl: string;
- private dataStore: {
- employeeList: Employee[];
- };
-
- constructor(private http: Http) {
-
- this.baseUrl = '/api/';
- this.dataStore = {
- employeeList: []
- };
- this._employeeList = < BehaviorSubject < Employee[] >> new BehaviorSubject([]);
- this.employeeList = this._employeeList.asObservable();
- }
-
- getAll() {
- this.http.get(`${this.baseUrl}GetAllEmployees`).map(response => response.json()).subscribe(data => {
- this.dataStore.employeeList = data;
-
- this._employeeList.next(Object.assign({}, this.dataStore).employeeList);
- }, error => console.log('Could not load employee.'));
- }
- }
As you can see in the above code, we are getting all the employees by calling /api/GetAllEmployees.
Add Employee HTML file
Add the below HTML code to the file.
- <div id="summary" class="section panel panel-primary">
- <div class="panel-heading"> Employee Summary</div>
- <div class="container">
- <!--{{employeeList | json}}-->
- <table class="table table-striped table-condensed">
- <thead>
- <tr>
- <th>Name</th>
- <th>Age</th>
- <th>City</th>
- <th>Country</th>
- <th></th>
- </tr>
- <tr>
- <th></th>
- <th></th>
- <th></th>
- <th></th>
- <th></th>
- <th></th>
- </tr>
- </thead>
- <tbody *ngFor="let item of employeeList | async">
- <tr>
- <td><input type="text" [(ngModel)]='item.empName' /></td>
- <td><input type="text" [(ngModel)]='item.empAge' /></td>
- <td><input type="text" [(ngModel)]='item.empCity' /></td>
- <td><input type="text" [(ngModel)]='item.empCountry' /></td>
- <td></td>
- <td>
- </tr>
- </tbody>
- </table>
- </div>
- </div>
Here, we have used,
- ngFor to use for loop into the Employee list
- Bound the Employee model from the component
Add Employee component class
Add the below code in the component class.
- import {
- Component,
- OnInit
- } from '@angular/core';
- import 'rxjs/add/operator/catch';
- import {
- EmployeeService
- } from "./employeeService";
- import {
- Observable
- } from "rxjs/Observable";
- import {
- Employee
- } from "./employeeService"
-
- @Component({
- selector: 'employee',
- providers: [EmployeeService],
- templateUrl: './employee.component.html'
- })
-
- export class EmployeeComponent implements OnInit {
- public employeeList: Observable < Employee[] > ;
- showEditor = true;
- myName: string;
- employee: Employee;
- constructor(private dataService: EmployeeService) {
- this.employee = new Employee();
- }
- ngOnInit() {
-
-
- this.employeeList = this.dataService.employeeList;
- this.dataService.getAll();
- }
- }
As you can see in the above code, we have specified that the application should call GetAll method of the service on init.
Add EmployeeComponent in the ApModule
Add EmployeeComponent to the class app.shared.module.ts, as shown below.
- import {
- NgModule
- } from '@angular/core';
- import {
- CommonModule
- } from '@angular/common';
- import {
- FormsModule
- } from '@angular/forms';
- import {
- HttpModule
- } from '@angular/http';
- import {
- RouterModule
- } from '@angular/router';
- import {
- AppComponent
- } from './components/app/app.component';
- import {
- NavMenuComponent
- } from './components/navmenu/navmenu.component';
- import {
- HomeComponent
- } from './components/home/home.component';
- import {
- FetchDataComponent
- } from './components/fetchdata/fetchdata.component';
- import {
- CounterComponent
- } from './components/counter/counter.component';
- import {
- EmployeeComponent
- } from './components/Employee/employee.component';
- @NgModule({
- declarations: [
- AppComponent,
- NavMenuComponent,
- CounterComponent,
- FetchDataComponent,
- HomeComponent,
- EmployeeComponent
- ],
- imports: [
- CommonModule,
- HttpModule,
- FormsModule,
- RouterModule.forRoot([{
- path: '',
- redirectTo: 'home',
- pathMatch: 'full'
- }, {
- path: 'employee',
- component: EmployeeComponent
- },
- {
- path: 'home',
- component: HomeComponent
- }, {
- path: 'counter',
- component: CounterComponent
- }, {
- path: 'fetch-data',
- component: FetchDataComponent
- }, {
- path: '**',
- redirectTo: 'home'
- }
- ])
- ]
- })
- export class AppModuleShared {}
Add Employee Management in the Navigation Menu
Update Navigation menu and add the Employee link as below.
- <div class='main-nav'>
- <div class='navbar navbar-inverse'>
- <div class='navbar-header'> <button type='button' class='navbar-toggle' data-toggle='collapse' data-target='.navbar-collapse'>
-
- <span class='sr-only'>Toggle navigation</span>
-
- <span class='icon-bar'></span>
-
- <span class='icon-bar'></span>
-
- <span class='icon-bar'></span>
-
- </button> <a class='navbar-brand' [routerLink]="['/home']">NeelAngular4CRUD</a></div>
- <div class='clearfix'></div>
- <div class='navbar-collapse collapse'>
- <ul class='nav navbar-nav'>
- <li [routerLinkActive]="['link-active']"> <a [routerLink]="['/employee']">
-
- <span class='glyphicon glyphicon-employee'></span> Employee Management
-
- </a></li>
- <li [routerLinkActive]="['link-active']"> <a [routerLink]="['/home']">
-
- <span class='glyphicon glyphicon-home'></span> Home
-
- </a></li>
- <li [routerLinkActive]="['link-active']"> <a [routerLink]="['/counter']">
-
- <span class='glyphicon glyphicon-education'></span> Counter
-
- </a></li>
- <li [routerLinkActive]="['link-active']"> <a [routerLink]="['/fetch-data']">
-
- <span class='glyphicon glyphicon-th-list'></span> Fetch data
-
- </a></li>
- </ul>
- </div>
- </div>
- </div>
Let us test our changes
Run the application and click on Employee Management.
As you can see, all the data in the database, is displayed here.
Add, Update and Delete Employees
In the list of Employees, it is time to add and update them. First of all, we will update the HTML and will add below lines:
- <button class="btn btn-xs btn-primary">Update</button>
- <button class="btn btn-xs btn-primary">Remove</button>
as shown below,
Changes in Employee Service for Add, Update, Delete
For adding:
- public addEmployee(employee: Employee) {
- console.log("add Employee");
- var headers = new Headers();
- headers.append('Content-Type', 'application/json; charset=utf-8');
- console.log('add Employee : ' + JSON.stringify(employee));
- this.http.post(`${this.baseUrl}AddEmployee/`, JSON.stringify(employee), {
- headers: headers
- }).map(response => response.json()).subscribe(data => {
- this.dataStore.employeeList.push(data);
- this._employeeList.next(Object.assign({}, this.dataStore).employeeList);
- }, error => console.log('Could not create todo.'));
- };
For updating:
- public updateEmployee(newEmployee: Employee) {
- console.log("update Employee");
- var headers = new Headers();
- headers.append('Content-Type', 'application/json; charset=utf-8');
- console.log('Update Employee : ' + JSON.stringify(newEmployee));
- this.http.put(`${this.baseUrl}UpdateEmployee/`, JSON.stringify(newEmployee), {
- headers: headers
- }).map(response => response.json()).subscribe(data => {
- alert("hi");
- this.dataStore.employeeList.forEach((t, i) => {
- if (t.studentId === data.id) {
- this.dataStore.employeeList[i] = data;
- }
- });
- }, error => console.log('Could not update todo.'));
- };
For deleting:
- removeItem(employeeId: number) {
- var headers = new Headers();
- headers.append('Content-Type', 'application/json; charset=utf-8');
- console.log("removeItem:" + employeeId);
- this.http.delete(`${this.baseUrl}DeleteEmployee/${employeeId}`, {
- headers: headers
- }).subscribe(response => {
- this.dataStore.employeeList.forEach((t, i) => {
- if (t.studentId === employeeId) {
- this.dataStore.employeeList.splice(i, 1);
- }
- });
- this._employeeList.next(Object.assign({}, this.dataStore).employeeList);
- }, error => console.log('Could not delete Employee.'));
- }
Changes in Employee Component for Add, Update, Delete
For adding:
- public addEmployee(item: Employee) {
- let todoId = this.dataService.addEmployee(this.employee);
- }
For updating:
- public updateEmployee(item: Employee) {
- this.dataService.updateEmployee(item);
- }
For deleting:
- public deleteEmployee(employeeId: number) {
- this.dataService.removeItem(employeeId);
- }
This is it!
Let us first test Add methods
Add the details as shown below and click on Add:
The value will be added at the end and also will be added to the database once you click on Add:
Same with Update, once you update something Inline and click on Update, the row would be updated along with the Database:
The database would also be reflected as shown below:
And to remove, just click on Remove button of the row which you want to delete:
The row would be removed from UI and the database would also get reflected:
Note
You can find the source code of my sample application here.
You can find all of my .NET Core posts here.
Hope it helps.