subscriptionprice.service.ts 3.38 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, public commonService: GlobalService ) { }

    
  GetSubscriptionPrices(obj: any) {
    return this.http.get(this.commonService.resourceBaseUrl + "SubscriptionPrice/GetSubscriptionPrices?editionId="
      + obj.editionId 
	  + "&sortColumn=" + obj.sortColumn
	  + "&sortOrder=" + obj.sortOrder
	  + "&pageNo=" + obj.pageNo 
	  + "&pageLength=" + obj.pageLength
	  )
      .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 + "SubscriptionPrice/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 + "SubscriptionPrice/UpdateSubscriptionPrices",  
    JSON.stringify(jsonData), {headers: headers})
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }

  DeleteSubscriptionPrice(obj: any) {
    //let options = new RequestOptions({ headers: this.headers });
    console.log(obj);
    let subscriptionPriceId = obj;
    let headers = new Headers({
      'Content-Type': 'application/json'
    });
    return this.http.get(this.commonService.resourceBaseUrl + "SubscriptionPrice/DeleteSubscriptionPrice?subscriptionPriceId=" + subscriptionPriceId)
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }
  CheckSubscriptionPlanForLicense(obj: any) {
    //let options = new RequestOptions({ headers: this.headers });
    console.log(obj);
    return this.http.get(this.commonService.resourceBaseUrl + "SubscriptionPrice/CheckSubscriptionPlanForLicense?subscriptionPriceId=" + obj)
      .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);
  }

  
}