Create a model and a service for User

This commit is contained in:
Gergely Polonkai 2016-10-28 05:40:09 +02:00
parent 3e02eb4abb
commit e2a3332d82
4 changed files with 75 additions and 4 deletions

View File

@ -6,7 +6,7 @@ import { HttpModule } from "@angular/http";
import { AppRoutingModule } from "./app-routing.module";
import { InMemoryWebApiModule } from "angular-in-memory-web-api";
import { InMemoryDataService } from "./in-memory-data.service";
import { InMemoryDuckService } from "./in-memory-data.service";
import { AppComponent } from "./app.component";
import { DuckDetailComponent } from "./duck-detail.component";
@ -14,12 +14,13 @@ import { DashboardComponent } from "./dashboard.component";
import { DucksComponent } from "./ducks.component";
import { DuckService } from "./duck.service";
import { UserService } from "./user.service";
@NgModule({
imports: [
BrowserModule,
HttpModule,
InMemoryWebApiModule.forRoot(InMemoryDataService),
InMemoryWebApiModule.forRoot(InMemoryDuckService),
AppRoutingModule
],
declarations: [
@ -28,7 +29,10 @@ import { DuckService } from "./duck.service";
DuckDetailComponent,
DucksComponent
],
providers: [ DuckService ],
providers: [
DuckService,
UserService
],
bootstrap: [ AppComponent ]
})
export class AppModule {}

View File

@ -1,6 +1,6 @@
import { InMemoryDbService } from "angular-in-memory-web-api";
export class InMemoryDataService implements InMemoryDbService {
export class InMemoryDuckService implements InMemoryDbService {
createDb() {
let ducks = [
{
@ -18,3 +18,17 @@ export class InMemoryDataService implements InMemoryDbService {
return {ducks};
}
}
export class InMemoryUserService implements InMemoryDbService {
createDb() {
let users = [
{
id: 1,
username: 'epolger',
password: 'epolger'
}
];
return {users};
}
}

View File

@ -3,3 +3,9 @@ export class Duck {
name: string;
color: string;
}
export class User {
id: number;
username: string;
password: string;
}

47
app/user.service.ts Normal file
View File

@ -0,0 +1,47 @@
import { Injectable } from "@angular/core";
import { Headers, Http } from "@angular/http";
import "rxjs/add/operator/toPromise";
import { User } from "./models";
@Injectable()
export class UserService {
private headers = new Headers({"Content-Type": "application/json"});
private usersUrl = 'app/users';
currentUser: User = null;
constructor(private http: Http) {}
getUsers(): Promise<User[]> {
return this.http.get(this.usersUrl)
.toPromise()
.then(response => response.json().data as User[])
.catch(this.handleError);
}
getUser(id: number): Promise<User> {
return this.getUsers()
.then(users => users.find(user => user.id === id));
}
create(username: string, password: string): Promise<User> {
return this.http.post(this.usersUrl,
JSON.stringify(
{
username: name,
password: password
}),
{headers: this.headers})
.toPromise()
.then(res => res.json().data)
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error("An error occured", error);
return Promise.reject(error.message || error);
}
}