Skip to content

Commit 0ad0f60

Browse files
committed
Update to REST Api to better handle PUT/DELETE
1 parent c8f3a1e commit 0ad0f60

File tree

4 files changed

+75
-62
lines changed

4 files changed

+75
-62
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input } from '@angular/core';
1+
import { Component, Input, Output, EventEmitter } from '@angular/core';
22
import { IUser } from '../../models/User';
33
import { UserService } from '../../shared/user.service';
44

@@ -8,7 +8,7 @@ import { UserService } from '../../shared/user.service';
88
})
99
export class UserDetailComponent {
1010
@Input() user: IUser;
11-
11+
@Output() userUpdate: EventEmitter<any> = new EventEmitter();
1212
constructor(private userService: UserService) { }
1313

1414

@@ -17,6 +17,7 @@ export class UserDetailComponent {
1717
console.log('Put user result: ', result);
1818
}, error => {
1919
console.log(`There was an issue. ${error._body}.`);
20-
});
20+
});
21+
this.userUpdate.emit(null);
2122
}
2223
}

ClientApp/app/containers/users/users.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h1>This is a RestAPI Example (hitting WebAPI in our case)</h1>
1+
<h1>This is a RestAPI Example (hitting WebAPI in our case)</h1>
22

33
<blockquote>
44
Let's get some fake users from Rest:<br>
@@ -26,4 +26,4 @@ <h2>Users</h2>
2626
</button>
2727
</li>
2828
</ul>
29-
<app-user-detail [user]="selectedUser"></app-user-detail>
29+
<app-user-detail (userUpdate)="onUserUpdate($event)" [user]="selectedUser"></app-user-detail>
Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,81 @@
1-
import {
2-
Component, OnInit,
3-
// animation imports
4-
trigger, state, style, transition, animate, Inject
1+
import {
2+
Component, OnInit,
3+
// animation imports
4+
trigger, state, style, transition, animate, Inject
55
} from '@angular/core';
66
import { IUser } from '../../models/User';
77
import { UserService } from '../../shared/user.service';
88

99
@Component({
10-
selector: 'app-users',
11-
templateUrl: './users.component.html',
12-
styleUrls: ['./users.component.css'],
13-
animations: [
14-
// Animation example
15-
// Triggered in the ngFor with [@flyInOut]
16-
trigger('flyInOut', [
17-
state('in', style({ transform: 'translateY(0)' })),
18-
transition('void => *', [
19-
style({ transform: 'translateY(-100%)' }),
20-
animate(1000)
21-
]),
22-
transition('* => void', [
23-
animate(1000, style({ transform: 'translateY(100%)' }))
24-
])
25-
])
26-
]
10+
selector: 'app-users',
11+
templateUrl: './users.component.html',
12+
styleUrls: ['./users.component.css'],
13+
animations: [
14+
// Animation example
15+
// Triggered in the ngFor with [@flyInOut]
16+
trigger('flyInOut', [
17+
state('in', style({ transform: 'translateY(0)' })),
18+
transition('void => *', [
19+
style({ transform: 'translateY(-100%)' }),
20+
animate(1000)
21+
]),
22+
transition('* => void', [
23+
animate(1000, style({ transform: 'translateY(100%)' }))
24+
])
25+
])
26+
]
2727
})
2828
export class UsersComponent implements OnInit {
2929

30-
users: IUser[];
31-
selectedUser: IUser;
30+
users: IUser[];
31+
selectedUser: IUser;
3232

33-
// Use "constructor"s only for dependency injection
34-
constructor(
35-
private userService: UserService
36-
) { }
33+
// Use "constructor"s only for dependency injection
34+
constructor(
35+
private userService: UserService
36+
) { }
3737

38-
// Here you want to handle anything with @Input()'s @Output()'s
39-
// Data retrieval / etc - this is when the Component is "ready" and wired up
40-
ngOnInit() {
41-
this.userService.getUsers().subscribe(result => {
42-
console.log('HttpClient [GET] /api/users/allresult', result);
43-
this.users = result;
44-
});
45-
}
38+
// Here you want to handle anything with @Input()'s @Output()'s
39+
// Data retrieval / etc - this is when the Component is "ready" and wired up
40+
ngOnInit() {
41+
this.userService.getUsers().subscribe(result => {
42+
console.log('HttpClient [GET] /api/users/allresult', result);
43+
this.users = result;
44+
});
45+
}
4646

47-
onSelect(user: IUser): void {
48-
this.selectedUser = user;
49-
}
47+
onSelect(user: IUser): void {
48+
this.selectedUser = user;
49+
}
5050

51-
deleteUser(user) {
52-
this.userService.deleteUser(user).subscribe(result => {
53-
console.log('Delete user result: ', result);
54-
let position = this.users.indexOf(user);
55-
this.users.splice(position, 1);
56-
}, error => {
57-
console.log(`There was an issue. ${error._body}.`);
58-
});
59-
}
51+
deleteUser(user) {
52+
this.clearUser();
53+
this.userService.deleteUser(user).subscribe(result => {
54+
console.log('Delete user result: ', result);
55+
let position = this.users.indexOf(user);
56+
this.users.splice(position, 1);
57+
}, error => {
58+
console.log(`There was an issue. ${error._body}.`);
59+
});
60+
}
61+
62+
onUserUpdate(event) {
63+
this.clearUser();
64+
}
65+
66+
addUser(newUserName) {
67+
this.clearUser();
68+
this.userService.addUser(newUserName).subscribe(result => {
69+
console.log('Post user result: ', result);
70+
this.users.push(result);
71+
}, error => {
72+
console.log(`There was an issue. ${error._body}.`);
73+
});
74+
}
6075

61-
addUser(newUserName) {
62-
this.userService.addUser(newUserName).subscribe(result => {
63-
console.log('Post user result: ', result);
64-
this.users.push(result);
65-
}, error => {
66-
console.log(`There was an issue. ${error._body}.`);
67-
});
76+
clearUser() {
77+
if (this.selectedUser) {
78+
this.selectedUser = null;
6879
}
80+
}
6981
}

Server/RestAPI/UsersController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using AspCoreServer.Data;
1+
using AspCoreServer.Data;
22
using AspCoreServer.Models;
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.EntityFrameworkCore;
@@ -89,7 +89,7 @@ public async Task<IActionResult> Put(int id, [FromBody]User userUpdateValue)
8989
{
9090
_context.Update(userUpdateValue);
9191
await _context.SaveChangesAsync();
92-
return Ok("Updated user - " + userUpdateValue.Name);
92+
return Json("Updated user - " + userUpdateValue.Name);
9393
}
9494
}
9595
catch (DbUpdateException)
@@ -116,7 +116,7 @@ public async Task<IActionResult> Delete(int id)
116116
{
117117
_context.User.Remove(userToRemove);
118118
await _context.SaveChangesAsync();
119-
return Ok("Deleted user - " + userToRemove.Name);
119+
return Json("Deleted user - " + userToRemove.Name);
120120
}
121121
}
122122
}

0 commit comments

Comments
 (0)