managediscountcode.service.ts
2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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 ManageDiscountCodeService {
constructor(private http: Http, private commonService: GlobalService ) { }
GetDiscountCodes(obj: any) {
if(obj.startDate == ''){
obj.startDate = '1/1/1';
}
if(obj.endDate == ''){
obj.endDate = '1/1/9999';
}
return this.http.get(this.commonService.DiscountBaseUrl + "/GetDiscountCodes?discountCode="
+ obj.discountCode + "&startDate=" + obj.startDate + "&endDate=" + obj.endDate)
.map(this.extractData)
.catch((res: Response) => this.handleError(res));
}
InsertDiscountCode(obj: any) {
//let options = new RequestOptions({ headers: this.headers });
var jsonData = {'id': obj.discountId, 'discountCode': obj.discountCode, 'startDate': obj.startDate, 'endDate': obj.endDate, 'percentage': obj.percentage, 'isActive': obj.isActive };
console.log(obj);
var headers = new Headers({
'Content-Type': 'application/json'
});
return this.http.post(this.commonService.DiscountBaseUrl + "/InsertDiscountCode",
JSON.stringify(jsonData), {headers: headers})
.map(this.extractData)
.catch((res: Response) => this.handleError(res));
}
UpdateDiscountCode(obj: any) {
//let options = new RequestOptions({ headers: this.headers });
var jsonData = {'id': obj.discountId, 'discountCode': obj.discountCode, 'startDate': obj.startDate, 'endDate': obj.endDate, 'percentage': obj.percentage, 'isActive': obj.isActive };
console.log(obj);
var headers = new Headers({
'Content-Type': 'application/json'
});
return this.http.post(this.commonService.DiscountBaseUrl + "/UpdateDiscountCode",
JSON.stringify(jsonData), {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);
}
}