1
|
import {Injectable} from '@angular/core';
|
2
|
import {HttpClient} from '@angular/common/http';
|
3
|
import {Observable} from 'rxjs';
|
4
|
import {Page} from '../models/paging/page.interface';
|
5
|
import {environment} from '../../../environments/environment';
|
6
|
|
7
|
|
8
|
@Injectable({
|
9
|
providedIn: 'root'
|
10
|
})
|
11
|
export class GenericRestService<T> {
|
12
|
|
13
|
|
14
|
// tslint:disable-next-line:variable-name
|
15
|
private _baseUrl: string;
|
16
|
|
17
|
public get baseUrl(): string {
|
18
|
return this._baseUrl;
|
19
|
}
|
20
|
|
21
|
constructor(url: string, private httpService: HttpClient) {
|
22
|
// TODO when we unify the backend will be like that but for now
|
23
|
// if (url.endsWith('/')) {
|
24
|
// throw Error('Malformed url: ' + url + '\nEnds with slash');
|
25
|
// }
|
26
|
// if (url.startsWith('/')) {
|
27
|
// throw Error('Malformed url: ' + url + '\nStarts with slash');
|
28
|
// }
|
29
|
// this._baseUrl = environment.ApiUrl + '/' + url;
|
30
|
// will be like this
|
31
|
if (url.endsWith('/')) {
|
32
|
throw Error('Malformed url: ' + url + '\nEnds with slash');
|
33
|
}
|
34
|
if (url.startsWith('/')) {
|
35
|
throw Error('Malformed url: ' + url + '\nStarts with slash');
|
36
|
}
|
37
|
this._baseUrl = url;
|
38
|
// this remains unchanged
|
39
|
this._baseUrl = url;
|
40
|
}
|
41
|
|
42
|
|
43
|
public getAll(): Observable<T[]> {
|
44
|
return this.httpService.get<T[]>(this.baseUrl + '/all');
|
45
|
}
|
46
|
|
47
|
public getAllPaged(page: number, offset: number): Observable<Page<T>> {
|
48
|
if (!environment.production && offset !== 10) {
|
49
|
|
50
|
console.log('DEV MESSAGE: The deal is 10 results per page. Your offset became 10.I only show up in non-prod environment :)');
|
51
|
}
|
52
|
return this.httpService.get<Page<T>>(this.baseUrl + '/all', {
|
53
|
params: {
|
54
|
page: page.toString(),
|
55
|
offset: '10'
|
56
|
}
|
57
|
});
|
58
|
}
|
59
|
|
60
|
public getById(id: string | number): Observable<T> {
|
61
|
return this.httpService.get<T>(this.baseUrl + '/' + id.toString());
|
62
|
}
|
63
|
|
64
|
public update(data: T): Observable<T> {
|
65
|
return this.httpService.put<T>(this.baseUrl, data);
|
66
|
}
|
67
|
|
68
|
public create(data: T): Observable<T> {
|
69
|
return this.httpService.post<T>(this.baseUrl, data);
|
70
|
}
|
71
|
|
72
|
public delete(id: string | number): Observable<T> {
|
73
|
return this.httpService.delete<T>(this.baseUrl + '/' + id.toString());
|
74
|
}
|
75
|
|
76
|
public deleteById(data: T): Observable<T> {
|
77
|
return this.httpService.delete<T>(this.baseUrl, data);
|
78
|
}
|
79
|
|
80
|
|
81
|
public searchByCriteriaPaged(page: number, offset: number, criteria: any, urlExtension: string): Observable<Page<T>> {
|
82
|
// TODO make this better
|
83
|
if (!environment.production && offset !== 10) {
|
84
|
|
85
|
console.log('DEV MESSAGE: The deal is 10 results per page. Your offset became 10.I only show up in non-prod environment :)');
|
86
|
}
|
87
|
if (urlExtension) {
|
88
|
if (urlExtension.endsWith('/')) {
|
89
|
throw Error('Malformed url: ' + urlExtension + '\nEnds with slash');
|
90
|
|
91
|
}
|
92
|
if (urlExtension.startsWith('/')) {
|
93
|
throw Error('Malformed url: ' + urlExtension + '\nStarts with slash');
|
94
|
}
|
95
|
urlExtension = '/' + urlExtension;
|
96
|
} else {
|
97
|
urlExtension = '';
|
98
|
}
|
99
|
|
100
|
return this.httpService.post<Page<T>>(this.baseUrl + urlExtension, criteria, {
|
101
|
params: {
|
102
|
page: page.toString(),
|
103
|
offset: '10'
|
104
|
}
|
105
|
});
|
106
|
}
|
107
|
}
|