Skip to content

Commit

Permalink
Angular Restangular
Browse files Browse the repository at this point in the history
  • Loading branch information
PvlSport committed Oct 31, 2017
1 parent 10adb25 commit aefd04f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
6 changes: 5 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -47,7 +50,8 @@ import { baseURL } from './shared/baseurl';
MaterialModule,
FlexLayoutModule,
AppRoutingModule,
BrowserAnimationsModule
BrowserAnimationsModule,
RestangularModule.forRoot(RestangularConfigFactory)
],
providers: [
DishService,
Expand Down
2 changes: 2 additions & 0 deletions src/app/dishdetail/dishdetail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'rxjs/add/operator/switchMap';
})
export class DishdetailComponent implements OnInit {
dish: Dish;
dishcopy = null;
dishIds: number[];
prev: number;
next: number;
Expand All @@ -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 = <any>errMess);
Expand Down
22 changes: 11 additions & 11 deletions src/app/services/dish.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dish[]> {
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<Dish> {
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<Dish> {
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<number[]> {
Expand Down
5 changes: 5 additions & 0 deletions src/app/shared/restConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { baseURL } from './baseurl';

export function RestangularConfigFactory (RestangularProvider) {
RestangularProvider.setBaseUrl(baseURL);
}

0 comments on commit aefd04f

Please sign in to comment.