From aefd04ffb52ec2452678ca6b507ff4ef1bc770fc Mon Sep 17 00:00:00 2001 From: torekhan Date: Tue, 31 Oct 2017 16:09:34 +0000 Subject: [PATCH] Angular Restangular --- package.json | 1 + src/app/app.module.ts | 6 +++++- src/app/dishdetail/dishdetail.component.ts | 2 ++ src/app/services/dish.service.ts | 22 +++++++++++----------- src/app/shared/restConfig.ts | 5 +++++ 5 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 src/app/shared/restConfig.ts diff --git a/package.json b/package.json index 382d3e0..4098c29 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "core-js": "^2.4.1", "font-awesome": "^4.7.0", "hammerjs": "^2.0.8", + "ngx-restangular": "^1.0.13", "rxjs": "^5.4.2", "zone.js": "^0.8.14" }, diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 07ed2af..5689e04 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -26,6 +26,9 @@ import { ContactComponent } from './contact/contact.component'; import { LoginComponent } from './login/login.component'; import { baseURL } from './shared/baseurl'; +import { RestangularConfigFactory } from './shared/restConfig'; + +import { RestangularModule, Restangular } from 'ngx-restangular'; @NgModule({ declarations: [ @@ -47,7 +50,8 @@ import { baseURL } from './shared/baseurl'; MaterialModule, FlexLayoutModule, AppRoutingModule, - BrowserAnimationsModule + BrowserAnimationsModule, + RestangularModule.forRoot(RestangularConfigFactory) ], providers: [ DishService, diff --git a/src/app/dishdetail/dishdetail.component.ts b/src/app/dishdetail/dishdetail.component.ts index 17b8cc1..17157fd 100644 --- a/src/app/dishdetail/dishdetail.component.ts +++ b/src/app/dishdetail/dishdetail.component.ts @@ -15,6 +15,7 @@ import 'rxjs/add/operator/switchMap'; }) export class DishdetailComponent implements OnInit { dish: Dish; + dishcopy = null; dishIds: number[]; prev: number; next: number; @@ -33,6 +34,7 @@ export class DishdetailComponent implements OnInit { .switchMap((params: Params) => this.dishservice.getDish(+params['id'])) .subscribe(result => { this.dish = result; + this.dishcopy = result; this.setPrevNext(result.id); }, errMess => this.errMess = errMess); diff --git a/src/app/services/dish.service.ts b/src/app/services/dish.service.ts index 752d126..2c22ee5 100644 --- a/src/app/services/dish.service.ts +++ b/src/app/services/dish.service.ts @@ -13,28 +13,28 @@ import 'rxjs/add/operator/catch'; import 'rxjs/add/observable/of'; import 'rxjs/add/operator/map'; +import { RestangularModule, Restangular } from 'ngx-restangular'; + @Injectable() export class DishService { - constructor(private http: Http, - private processHttpmsgService: ProcessHttpmsgService) { } + constructor( + private restangular: Restangular, + private http: Http, + private processHttpmsgService: ProcessHttpmsgService + ) { } getDishes(): Observable { - return this.http.get(baseURL + 'dishes') - .map(res => this.processHttpmsgService.extractData(res) ) - .catch(error => this.processHttpmsgService.handleError(error)); + return this.restangular.all('dishes').getList(); } getDish(id: number): Observable { - return this.http.get(baseURL + 'dishes/' + 'id') - .map(res => this.processHttpmsgService.extractData(res) ) - .catch(error => this.processHttpmsgService.handleError(error)); + return this.restangular.one('dishes', id); } getFeaturedDish(): Observable { - return this.http.get(baseURL + 'dishes?featured=true') - .map(res => this.processHttpmsgService.extractData(res) )[0] - .catch(error => this.processHttpmsgService.handleError(error)); + return this.restangular.all('dishes').getList( {featured: true} ) + .map(dishes => dishes[0]); } getDishIds(): Observable { diff --git a/src/app/shared/restConfig.ts b/src/app/shared/restConfig.ts new file mode 100644 index 0000000..eb04803 --- /dev/null +++ b/src/app/shared/restConfig.ts @@ -0,0 +1,5 @@ +import { baseURL } from './baseurl'; + +export function RestangularConfigFactory (RestangularProvider) { + RestangularProvider.setBaseUrl(baseURL); +}