Skip to content

Commit 909bafb

Browse files
pavankjaddaPavan Jadda
authored andcommitted
Add Edit and Update to Employee View component
1 parent 85a148d commit 909bafb

File tree

7 files changed

+73
-26
lines changed

7 files changed

+73
-26
lines changed

src/main/java/com/pj/keycloak/service/EmployeeService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface EmployeeService
1111

1212
Optional<Employee> findById(Long id);
1313

14-
void updateProfile(Employee employee);
14+
Employee updateProfile(Employee employee);
1515

1616
void saveAndFlush(Employee employee);
1717

src/main/java/com/pj/keycloak/service/EmployeeServiceImpl.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,9 @@ public Optional<Employee> findById(Long id)
3030
}
3131

3232
@Override
33-
public void updateProfile(Employee employee)
33+
public Employee updateProfile(Employee employee)
3434
{
35-
employee.setFirstName("John2");
36-
employee.setLastName("Doe2");
37-
employee.setPhone("54541151515");
38-
employee.setEmail("[email protected]");
39-
employee.setLocation("Fairfax");
40-
41-
employeeRepository.saveAndFlush(employee);
35+
return employeeRepository.saveAndFlush(employee);
4236
}
4337

4438
@Override

src/main/java/com/pj/keycloak/web/EmployeeController.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ public Optional<Employee> findById(@PathVariable Long id)
4141
return employeeService.findById(id);
4242
}
4343

44-
@GetMapping(path = "/update/{id}")
45-
public List<Employee> update(@PathVariable Long id)
44+
@PostMapping(path = "/update")
45+
public Employee update(@RequestBody Employee employee)
4646
{
47-
employeeService.updateProfile(findById(id).get());
48-
return employeeService.findAll();
47+
return employeeService.updateProfile(employee);
4948
}
5049

5150
@PostMapping(path = "/create")

src/main/webapp/spring-data-ui/src/app/app.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ <h2>
1111
</div>
1212

1313
<router-outlet></router-outlet>
14+
15+
16+
<br/> <br/>
1417
<div class="container container-fluid">
1518
<h5><a [routerLink]="['/logout']">Logout</a></h5>
1619
</div>

src/main/webapp/spring-data-ui/src/app/employee-list/employee.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ export class EmployeeService {
1818
{
1919
return this.httpClient.get<Employee>(url);
2020
}
21-
updateEmployee(url: string)
21+
22+
updateEmployee(url: string, employee: Employee)
2223
{
23-
return this.httpClient.get<Employee[]>(url);
24+
return this.httpClient.post<Employee>(url,employee);
2425
}
2526

2627
createEmployee(url: string, employee: Employee)

src/main/webapp/spring-data-ui/src/app/employee-view/employee-view.component.html

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<div class="form-group row">
1010
<label class="col-sm-2 col-form-label" for="id">Id</label>
1111
<div class="col-sm-10">
12-
<input class="form-control-plaintext" formControlName="id" id="id" readonly type="text">
12+
<input [ngClass]="(editMode==true)?'form-control':'form-control-plaintext'" formControlName="id" id="id" [readOnly]="!editMode" type="text">
1313
</div>
1414
</div>
1515
<div class="form-group row">
@@ -22,42 +22,42 @@
2222
<div class="form-group row">
2323
<label class="col-sm-2 col-form-label" for="firstName">firstName</label>
2424
<div class="col-sm-10">
25-
<input class="form-control-plaintext" formControlName="firstName" id="firstName" readonly type="text">
25+
<input [ngClass]="(editMode==true)?'form-control':'form-control-plaintext'" formControlName="firstName" id="firstName" [readOnly]="!editMode" type="text">
2626
</div>
2727
</div>
2828

2929
<div class="form-group row">
3030
<label class="col-sm-2 col-form-label" for="lastName">lastName</label>
3131
<div class="col-sm-10">
32-
<input class="form-control-plaintext" formControlName="lastName" id="lastName" readonly type="text">
32+
<input [ngClass]="(editMode==true)?'form-control':'form-control-plaintext'" formControlName="lastName" id="lastName" [readOnly]="!editMode" type="text">
3333
</div>
3434
</div>
3535

3636
<div class="form-group row">
3737
<label class="col-sm-2 col-form-label" for="email">Email</label>
3838
<div class="col-sm-10">
39-
<input class="form-control-plaintext" id="email" formControlName="email" readonly type="text">
39+
<input [ngClass]="(editMode==true)?'form-control':'form-control-plaintext'" id="email" formControlName="email" [readOnly]="!editMode" type="text">
4040
</div>
4141
</div>
4242

4343
<div class="form-group row">
4444
<label class="col-sm-2 col-form-label" for="phone">phone</label>
4545
<div class="col-sm-10">
46-
<input class="form-control-plaintext" id="phone" formControlName="phone" readonly type="text">
46+
<input [ngClass]="(editMode==true)?'form-control':'form-control-plaintext'" id="phone" formControlName="phone" [readOnly]="!editMode" type="text">
4747
</div>
4848
</div>
4949

5050
<div class="form-group row">
5151
<label class="col-sm-2 col-form-label" for="salary">salary</label>
5252
<div class="col-sm-10">
53-
<input class="form-control-plaintext" formControlName="salary" id="salary" readonly type="text">
53+
<input [ngClass]="(editMode==true)?'form-control':'form-control-plaintext'" formControlName="salary" id="salary" [readOnly]="!editMode" type="text">
5454
</div>
5555
</div>
5656

5757
<div class="form-group row">
5858
<label class="col-sm-2 col-form-label" for="location">location</label>
5959
<div class="col-sm-10">
60-
<input class="form-control-plaintext" formControlName="location" id="location" readonly type="text">
60+
<input [ngClass]="(editMode==true)?'form-control':'form-control-plaintext'" formControlName="location" id="location" [readOnly]="!editMode" type="text">
6161
</div>
6262
</div>
6363

@@ -66,7 +66,10 @@
6666

6767
<div class="container container-fluid">
6868
<h5>
69-
<button [routerLink]="['/']" class="btn btn-primary">Back</button>
69+
<button (click)="updateEmployee()" *ngIf="editMode" class="btn btn-primary" style="margin-right: 10px">Update</button>
70+
<button (click)="cancelUpdate()" *ngIf="editMode" class="btn btn-outline-danger" style="margin-right: 10px">Cancel</button>
71+
<button (click)="editEmployee()" *ngIf="!editMode" class="btn btn-primary" style="margin-right: 10px">Edit</button>
72+
<button routerLink="/employee/list" *ngIf="!editMode"class="btn btn-primary">Back</button>
7073
</h5>
7174
</div>
7275
</div>

src/main/webapp/spring-data-ui/src/app/employee-view/employee-view.component.ts

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Component, OnInit} from "@angular/core";
22
import {FormControl, FormGroup} from "@angular/forms";
3-
import {ActivatedRoute} from "@angular/router";
3+
import {ActivatedRoute, Router} from "@angular/router";
44
import {NgxSpinnerService} from "ngx-spinner";
55
import {Employee} from "src/app/employee-list/employee";
66
import {EmployeeService} from "src/app/employee-list/employee.service";
@@ -13,9 +13,11 @@ import {EmployeeService} from "src/app/employee-list/employee.service";
1313
export class EmployeeViewComponent implements OnInit
1414
{
1515
employee: Employee;
16+
editMode: boolean;
17+
1618
employeeForm = new FormGroup({
1719
id: new FormControl({value: '', disabled: true}),
18-
employeeId: new FormControl(''),
20+
employeeId: new FormControl({value: '', disabled: true}),
1921
firstName: new FormControl(''),
2022
lastName: new FormControl(''),
2123
email: new FormControl(''),
@@ -26,15 +28,16 @@ export class EmployeeViewComponent implements OnInit
2628

2729
constructor(private employeeService:EmployeeService,
2830
private activatedRoute:ActivatedRoute,
31+
private router:Router,
2932
private ngxSpinnerService:NgxSpinnerService)
3033
{ }
3134

3235
ngOnInit()
3336
{
37+
this.editMode=false;
3438
this.getEmployeeDetails();
3539
}
3640

37-
3841
private getEmployeeDetails()
3942
{
4043
let id=this.activatedRoute.snapshot.params.id;
@@ -62,4 +65,48 @@ export class EmployeeViewComponent implements OnInit
6265
}
6366
);
6467
}
68+
69+
editEmployee()
70+
{
71+
this.editMode=true;
72+
}
73+
74+
updateEmployee()
75+
{
76+
this.ngxSpinnerService.show();
77+
78+
console.error(this.employeeForm.value);
79+
let employee=this.employeeForm.value;
80+
81+
this.employeeService.updateEmployee('http://localhost:8081/api/v1/employee/update', employee).subscribe(
82+
data=>
83+
{
84+
this.employee=data;
85+
this.employeeForm.patchValue(
86+
{
87+
id: data.id,
88+
employeeId: data.employeeId,
89+
firstName: data.firstName,
90+
lastName: data.lastName,
91+
email: data.email,
92+
phone: data.phone,
93+
salary: data.salary,
94+
location: data.location,
95+
});
96+
this.editMode=false;
97+
98+
this.ngxSpinnerService.hide();
99+
},
100+
error1 =>
101+
{
102+
this.ngxSpinnerService.hide();
103+
}
104+
);
105+
106+
}
107+
108+
cancelUpdate()
109+
{
110+
this.editMode=false;
111+
}
65112
}

0 commit comments

Comments
 (0)