subscriptionprice.service.ts 3.95 KB
import { Injectable, Inject } from '@angular/core';
//import { HttpClient, HttpParams, HttpRequest} from "@angular/common/http";
import { Http, Response, Headers, RequestOptions, HttpModule } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/do';
import { Observable } from 'rxjs/Observable';
import { GlobalService } from '../../Shared/global';

@Injectable()
export class SubscriptionPriceService {

  constructor(private http: Http, private commonService: GlobalService ) { }

    //public GetUserById(Id: any): Observable<User> {
    //  return this.http.request<User>(
    //    'GET',
    //    'http://192.168.86.13:92/API/Api/Users/' + Id);
    //}

    //GetUserByLoginIdPassword(LoginId: string, Password: string): Observable<User> {
    //  return this.http.request<User>(
    //    'GET',
    //    'http://192.168.86.13:92/API/Api/Users/{LoginId=' + LoginId + '&Password=' + Password + '}');
    //}

    //UpdateProfile(UserObj: User): Observable<any> {
    //  return this.http.request<any>(
    //      'POST',
    //      'http://192.168.86.13:92/API/Api/Users/UpdateProfile',
    //      {
    //        body: UserObj
    //      });
    //}

  GetSubscriptionPrices(obj: any) {
    return this.http.get(this.commonService.resourceBaseUrl + "/api/GetSubscriptionPrices?editionId=" 
      + obj.editionId)
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }

  InsertSubscriptionPrice(obj: any) {    
    //let options = new RequestOptions({ headers: this.headers });
    var jsonData = {'id': obj.subscriptionPriceId, 'title': obj.title, 'price': obj.price, 'duration': obj.duration, 'editionId': obj.editionId, 'isActive': obj.isActive };
    console.log(obj);
    var headers = new Headers({
      'Content-Type': 'application/json'
    });
    return this.http.post(this.commonService.resourceBaseUrl + "/api/InsertSubscriptionPrice",  
    JSON.stringify(jsonData), {headers: headers})
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }

  UpdateSubscriptionPrices(obj: any) {    
    //let options = new RequestOptions({ headers: this.headers });
    var jsonData = { obj };
    console.log(obj);
    var headers = new Headers({
      'Content-Type': 'application/json'
    });
    return this.http.post(this.commonService.resourceBaseUrl + "/api/UpdateSubscriptionPrices",  
    JSON.stringify(jsonData), {headers: headers})
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }

  DeleteSubscriptionPrices(obj: any) {    
    //let options = new RequestOptions({ headers: this.headers });
    var subscriptionPriceIds = obj;
    console.log(subscriptionPriceIds);
    var headers = new Headers({
      'Content-Type': 'application/json'
    });
    return this.http.post(this.commonService.resourceBaseUrl + "/api/DeleteSubscriptionPrices",  
    subscriptionPriceIds, {headers: headers})
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }

  extractData(res: Response) {
    //debugger; 
    let body = res.json();
    return body;
  }

  handleError(error: any) {
    // In a real world app, we might use a remote logging infrastructure
    // We'd also dig deeper into the error to get a better message
    let errMsg = (error.message) ? error.message :
      error.status ? `${error.status} - ${error.statusText}` : 'Server error';
    console.error(errMsg); // log to console instead
    return Observable.throw(errMsg);
  }

  ////public GetUserById(url: string): Observable<any> {
     
  ////  return this._http.get(url)
  ////    .map((response: Response) => <any>response.json())
  ////      .do(data => console.log("All: " + JSON.stringify(data)))
  ////      .catch(this.handleError);
  ////}
  ////  private handleError(error: Response) {
  ////    console.error(error);
  ////    return Observable.throw(error.json().error || 'Server error');
  ////  }
  
}