import { Injectable } from 'angular2/core' ; import { URLSearchParams , Headers , RequestOptions } from 'angular2/http' ; import { Group } from '../models/group' ; import * as constants from '../config/constants' ; import { BaseHttpService } from './base-http' ; import 'rxjs/add/operator/map' ; import 'rxjs/add/operator/catch' ; import 'rxjs/add/observable/throw' ; import { Observable } from 'rxjs/Observable' ; import { ContactService } from './contact' ; class ServerResponse { constructor ( public resource ) { } } ; @ Injectable ( ) export class GroupService { baseResourceUrl : string = constants . DREAMFACTORY_INSTANCE_URL + '/api/v2/db/_table/contact_group' ; contactGroupUrl : string = constants . DREAMFACTORY_INSTANCE_URL + '/api/v2/db/_table/contact_group_relationship' ; constructor ( private httpService , private contactService ) { } ; query ( params ? ) { var queryHeaders = new Headers ( ) ; queryHeaders . append ( 'Content-Type' , 'application/json' ) ; queryHeaders . append ( 'X-Dreamfactory-Session-Token' , localStorage . getItem ( 'session_token' ) ) ; queryHeaders . append ( 'X-Dreamfactory-API-Key' , constants . DREAMFACTORY_API_KEY ) ; return this . httpService . http . get ( this . baseResourceUrl , { search : params , headers : queryHeaders } ) . map ( ( response ) => { var result = response . json ( ) ; let groups = [ ] ; result . resource . forEach ( ( group ) => { groups . push ( Group . fromJson ( group ) ) ; } ) ; return groups ; } ) . catch ( this . handleError ) ; } ; private handleError ( error ) { let errMsg = ( error . message ) ? error . message : error . status ? `${ error . status } - ${ error . statusText }` : 'Server error' ; console . log ( errMsg ) ; localStorage . setItem ( 'session_token' , '' ) ; window . location . hash = '/login' ; return Observable . throw ( errMsg ) ; } ; get ( id ) { var queryHeaders = new Headers ( ) ; queryHeaders . append ( 'Content-Type' , 'application/json' ) ; queryHeaders . append ( 'X-Dreamfactory-Session-Token' , localStorage . getItem ( 'session_token' ) ) ; queryHeaders . append ( 'X-Dreamfactory-API-Key' , constants . DREAMFACTORY_API_KEY ) ; return this . httpService . http . get ( this . baseResourceUrl + '/' + id , { headers : queryHeaders } ) . map ( ( response ) => { var result = response . json ( ) ; let group = Group . fromJson ( result ) ; return group ; } ) . catch ( this . handleError ) ; } ; remove ( id ) { var queryHeaders = new Headers ( ) ; queryHeaders . append ( 'Content-Type' , 'application/json' ) ; queryHeaders . append ( 'X-Dreamfactory-Session-Token' , localStorage . getItem ( 'session_token' ) ) ; queryHeaders . append ( 'X-Dreamfactory-API-Key' , constants . DREAMFACTORY_API_KEY ) ; return this . httpService . http . delete ( this . baseResourceUrl + '/' + id , { headers : queryHeaders } ) . map ( ( response ) => { var result = response . json ( ) ; return result . id ; } ) . catch ( this . handleError ) ; } save ( group ) { var queryHeaders = new Headers ( ) ; queryHeaders . append ( 'Content-Type' , 'application/json' ) ; queryHeaders . append ( 'X-Dreamfactory-Session-Token' , localStorage . getItem ( 'session_token' ) ) ; queryHeaders . append ( 'X-Dreamfactory-API-Key' , constants . DREAMFACTORY_API_KEY ) ; let options = new RequestOptions ( { headers : queryHeaders } ) ; if ( group . id ) { return this . httpService . http . patch ( this . baseResourceUrl , group . toJson ( true ) , options ) . map ( ( response ) => { return response ; } ) . catch ( this . handleError ) ; } else { delete group . id ; return this . httpService . http . post ( this . baseResourceUrl , group . toJson ( true ) , options ) . map ( ( response ) => { return response ; } ) . catch ( this . handleError ) ; } } }