Skip to content

Commit 0b7aa25

Browse files
LiverpoolOwenMarkPieszak
authored andcommitted
Created User service (TrilonIO#185)
* Removed no longer used API * Pluralised API to "Users" and depluralised model to "User". All UsersAPI's now point to same URI. * Updated web API status codes * Created user service
1 parent b64fe65 commit 0b7aa25

File tree

5 files changed

+60
-31
lines changed

5 files changed

+60
-31
lines changed

Client/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { ChatComponent } from './containers/chat/chat.component';
1919
import { Ng2BootstrapComponent } from './containers/ng2-bootstrap-demo/ng2bootstrap.component';
2020

2121
import { LinkService } from './shared/link.service';
22+
import { UserService } from './shared/user.service';
2223
import { ConnectionResolver } from './shared/route.resolver';
2324
import { ORIGIN_URL } from './shared/constants/baseurl.constants';
2425
import { TransferHttpModule } from '../modules/transfer-http/transfer-http.module';
@@ -136,6 +137,7 @@ export function createTranslateLoader(http: Http, baseHref) {
136137
],
137138
providers: [
138139
LinkService,
140+
UserService,
139141
ConnectionResolver,
140142
TranslateModule
141143
]

Client/app/containers/users/users.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<input class="form-control" [(ngModel)]="user.name" #name="ngModel" />
2424
</td>
2525
<td>
26-
<button *ngIf="name.dirty" class="btn btn-success" (click)="editUser(user)">Save</button>
26+
<button *ngIf="name.dirty" class="btn btn-success" (click)="updateUser(user)">Save</button>
2727
</td>
2828
<td>
2929
<button class="btn btn-danger" (click)="deleteUser(user)">Delete</button>

Client/app/containers/users/users.component.ts

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
// animation imports
44
trigger, state, style, transition, animate, Inject
55
} from '@angular/core';
6-
import { APP_BASE_HREF } from '@angular/common';
7-
8-
import { Http, URLSearchParams } from '@angular/http';
9-
import { ORIGIN_URL } from '../../shared/constants/baseurl.constants';
10-
import { TransferHttp } from '../../../modules/transfer-http/transfer-http';
6+
import { IUser } from '../../models/User';
7+
import { UserService } from '../../shared/user.service';
118

129
@Component({
1310
selector: 'fetchdata',
@@ -33,32 +30,21 @@ export class UsersComponent implements OnInit {
3330
public users: IUser[];
3431

3532
// Use "constructor"s only for dependency injection
36-
constructor(
37-
private transferHttp: TransferHttp, // Use only for GETS that you want re-used between Server render -> Client render
38-
private http: Http, // Use for everything else
39-
@Inject(ORIGIN_URL) private baseUrl: string
40-
) { }
33+
constructor(private userService: UserService) { }
4134

4235
// Here you want to handle anything with @Input()'s @Output()'s
4336
// Data retrieval / etc - this is when the Component is "ready" and wired up
4437
ngOnInit() {
45-
4638
this.newUserName = '';
47-
48-
// ** TransferHttp example / concept **
49-
// - Here we make an Http call on the server, save the result on the window object and pass it down with the SSR,
50-
// The Client then re-uses this Http result instead of hitting the server again!
51-
52-
// NOTE : transferHttp also automatically does .map(res => res.json()) for you, so no need for these calls
53-
this.transferHttp.get(`${this.baseUrl}/api/users`).subscribe(result => {
39+
this.userService.getUsers().subscribe(result => {
5440
console.log('Get user result: ', result);
5541
console.log('TransferHttp [GET] /api/users/allresult', result);
5642
this.users = result as IUser[];
5743
});
5844
}
5945

6046
deleteUser(user) {
61-
this.http.delete(`${this.baseUrl}/api/users/` + user.id).subscribe(result => {
47+
this.userService.deleteUser(user).subscribe(result => {
6248
console.log('Delete user result: ', result);
6349
if (result.ok) {
6450
let position = this.users.indexOf(user);
@@ -69,19 +55,19 @@ export class UsersComponent implements OnInit {
6955
});
7056
}
7157

72-
editUser(user) {
73-
this.http.put(`${this.baseUrl}/api/users/` + user.id, user).subscribe(result => {
74-
console.log('Put user result: ', result);
75-
if (!result) {
58+
updateUser(user) {
59+
this.userService.updateUser(user).subscribe(result => {
60+
console.log('Put user result: ', result);
61+
if (!result.ok) {
7662
alert('There was an issue, Could not edit user');
7763
}
7864
});
7965
}
8066

8167
addUser(newUserName) {
82-
this.http.post(`${this.baseUrl}/api/users`, { name: newUserName }).subscribe(result => {
68+
this.userService.addUser(newUserName).subscribe(result => {
8369
console.log('Post user result: ', result);
84-
if (result) {
70+
if (result.ok) {
8571
this.users.push(result.json());
8672
this.newUserName = '';
8773
} else {
@@ -90,8 +76,3 @@ export class UsersComponent implements OnInit {
9076
});
9177
}
9278
}
93-
94-
interface IUser {
95-
id: number;
96-
name: string;
97-
}

Client/app/models/User.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface IUser {
2+
id: number;
3+
name: string;
4+
}

Client/app/shared/user.service.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Injectable, Inject } from '@angular/core';
2+
import { Http, URLSearchParams } from '@angular/http';
3+
import { APP_BASE_HREF } from '@angular/common';
4+
import { ORIGIN_URL } from './constants/baseurl.constants';
5+
import { IUser } from '../models/User';
6+
import { TransferHttp } from '../../modules/transfer-http/transfer-http';
7+
import { Observable } from 'rxjs/Observable';
8+
9+
@Injectable()
10+
export class UserService {
11+
constructor(
12+
private transferHttp: TransferHttp, // Use only for GETS that you want re-used between Server render -> Client render
13+
private http: Http, // Use for everything else
14+
@Inject(ORIGIN_URL) private baseUrl: string) {
15+
16+
}
17+
18+
getUsers(): Observable<IUser[]> {
19+
// ** TransferHttp example / concept **
20+
// - Here we make an Http call on the server, save the result on the window object and pass it down with the SSR,
21+
// The Client then re-uses this Http result instead of hitting the server again!
22+
23+
// NOTE : transferHttp also automatically does .map(res => res.json()) for you, so no need for these calls
24+
return this.transferHttp.get(`${this.baseUrl}/api/users`);
25+
}
26+
27+
getUser(user: IUser): Observable<IUser> {
28+
return this.transferHttp.get(`${this.baseUrl}/api/users/` + user.id);
29+
}
30+
31+
deleteUser(user: IUser): Observable<any> {
32+
return this.http.delete(`${this.baseUrl}/api/users/` + user.id);
33+
}
34+
35+
updateUser(user: IUser): Observable<any> {
36+
return this.http.put(`${this.baseUrl}/api/users/` + user.id, user);
37+
}
38+
39+
addUser(newUserName: string): Observable<any> {
40+
return this.http.post(`${this.baseUrl}/api/users`, { name: newUserName })
41+
}
42+
}

0 commit comments

Comments
 (0)