image.service.ts 2.97 KB
import { Injectable } from '@angular/core';
import { Http, Response, Headers, URLSearchParams } from '@angular/http';
import { Router } from '@angular/router';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

declare var jQuery: any;

@Injectable()
export class ImageService {

  constructor(private http: Http) {

  }
   
  public getRemianingFilesData(filePath: string): Observable<any> {
      return this.http.get(filePath).map(this.getData).catch(this.handleError);
        
  }
  public getBodyViewData(index: number): Observable<any> {
      console.log("getBodyViewData.index :" + index);
    return this.http.get('../assets/data/json/da/body-views/' + index + '/da_dat_layer_' + index + '.json');
  }

  public updateImageData(imageData: any): Observable<any> {
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('bodyViewIndex', imageData.bodyViewIndex);
    headers.append('layerNumber', imageData.layerNumber);
    headers.append('bodyRegion', imageData.bodyRegion);

    let payload: string = JSON.stringify(imageData);
    console.log('inside updateImageData.JSON.stringify(imageData)= ' + payload);
    return this.http.post('/API/api/ProcessImage', payload, { headers: headers })
      .map(this.extractData)  
      .catch(this.handleError);
  }

  //public updateImageData(imageData: any): any {
  //    const headers = new Headers();
  //    headers.append('Content-Type', 'application/json');
  //    let payload: string = JSON.stringify(imageData);
  //    console.log('inside updateImageData.JSON.stringify(imageData)= ' + payload);
  //    //return this.http.post('/API/api/ProcessImage', payload, { headers: headers })
  //    //  .map(this.extractData)  
  //    //  .catch(this.handleError);
  //    jQuery.ajax({
  //        headers: {
  //            'Accept': 'application/json',
  //            'Content-Type': 'application/json'
  //        },
  //        url: '/API/api/ProcessImage/Post',
  //        type: 'POST',
  //        data: { "value": payload },
  //        success: function (result) {
  //            return result;
  //        }
  //    });
  //}
private getData(res:Response){
    const body = res.json();
    console.log("inside getData.body: "+body)
    return body || {};
  }

  private extractData(res: Response) {
      const body = res.json();
      return body || {};
  }

  private handleError(error: Response | any) {
    // In a real world app, we might use a remote logging infrastructure
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }
}