-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathangular2-active-record.ts
More file actions
125 lines (125 loc) · 4.2 KB
/
Copy pathangular2-active-record.ts
File metadata and controls
125 lines (125 loc) · 4.2 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import {Injectable} from '@angular/core';
import {Response} from '@angular/http';
import {Headers, RequestOptions} from '@angular/http';
import 'rxjs/add/operator/toPromise';
export interface MethodHttp {
query: string;
update: string;
insert: string;
delete: string;
[method: string]: string;
}
export interface IBApiConfig {
urlAPI: string;
headers: any;
methods: MethodHttp;
}
export class ApiConfig {
config: any;
urlAPI: string;
headers: any;
methods: MethodHttp;
defaultMethods: MethodHttp = {
query: "get",
update: "put",
insert: "post",
delete: "delete"
};
constructor(config?: any) {
this.config = config || {};
this.urlAPI = this.config.urlAPI || "http://localhost:3456/api/"
this.headers = this.config.headers || {};
this.methods = this.config.methods || {};
let method: string;
for (method in this.defaultMethods) {
this.methods[method] = this.methods[method] || this.defaultMethods[method];
}
}
getConfig() {
return {
urlAPI: this.urlAPI,
headers: this.headers,
methods: this.methods
}
}
}
export class ActiveRecord<T> {
public api_url: string;
private _config: IBApiConfig;
constructor(public options: ApiConfig, public httpService: any, protected table_name: string) {
this._config = options.getConfig();
this.api_url = this._config.urlAPI + "" + table_name;
}
// Ex:[GET] /${table_name}?page=1&sort=title
findAll(params: any = { page: 1, sort: "" }): Promise<T[]> {
return this.httpService[this._config.methods.query](this.api_url + this.generateParam(params))
.toPromise()
.then((res: Response) => this.processData(res))
.catch(this.handleError);
}
// Ex:[GET] /${table_name}/search?title=abc&page=1&sort=title
search(data: any, api_search_name: string = ""): Promise<T[]> {
return this.httpService[this._config.methods.query](this.api_url + "/" + api_search_name + this.generateParam(data))
.toPromise()
.then((res: Response) => this.processData(res))
.catch(this.handleError);
}
// Ex:[GET] /${table_name}/${id}
find(id: any): Promise<T> {
return this.httpService[this._config.methods.query](this.api_url + "/" + id)
.toPromise()
.then((res: Response) => this.processData(res))
.catch(this.handleError);
}
// Ex:[GET] /${table_name}/${id}
update(id: any, data: any) {
let body = JSON.stringify(data);
if (!this._config.headers['Content-Type']) {
this._config.headers['Content-Type'] = 'application/json';
}
let headers = new Headers(this._config.headers);
let options = new RequestOptions({ headers: headers });
return this.httpService[this._config.methods.update](this.api_url + "/" + id, body, options)
.toPromise()
.then((res: Response) => this.processData(res))
.catch(this.handleError);
}
// Ex:[GET] /${table_name}
insert(data: any) {
let body = JSON.stringify(data);
if (!this._config.headers['Content-Type']) {
this._config.headers['Content-Type'] = 'application/json';
}
let headers = new Headers(this._config.headers);
let options = new RequestOptions({ headers: headers });
return this.httpService[this._config.methods.insert](this.api_url, body, options)
.toPromise()
.then((res: Response) => this.processData(res))
.catch(this.handleError);
}
// Ex:[GET] /${table_name}/${id}
delete(id: any) {
let headers = new Headers(this._config.headers);
let options = new RequestOptions({ headers: headers });
return this.httpService[this._config.methods.delete](this.api_url + "/" + id, options)
.toPromise()
.then((res: Response) => this.processData(res))
.catch(this.handleError);
}
protected generateParam(params: any = {}): string {
let params_arr: Array<string> = [];
for (let key in params) {
if (params[key]) {
params_arr.push(key + "=" + params[key]);
}
}
return "?" + params_arr.join("&");
}
protected processData(res: Response) {
return <T>res.json();
}
protected handleError(error: any): Promise<any>{
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}