-
-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathoperation.service.ts
73 lines (61 loc) · 2.65 KB
/
operation.service.ts
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
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import * as moment from 'moment';
import { SERVER_API_URL } from 'app/app.constants';
import { createRequestOption } from 'app/shared/util/request-util';
import { IOperation } from 'app/shared/model/operation.model';
type EntityResponseType = HttpResponse<IOperation>;
type EntityArrayResponseType = HttpResponse<IOperation[]>;
@Injectable({ providedIn: 'root' })
export class OperationService {
public resourceUrl = SERVER_API_URL + 'api/operations';
constructor(protected http: HttpClient) {}
create(operation: IOperation): Observable<EntityResponseType> {
const copy = this.convertDateFromClient(operation);
return this.http
.post<IOperation>(this.resourceUrl, copy, { observe: 'response' })
.pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
}
update(operation: IOperation): Observable<EntityResponseType> {
const copy = this.convertDateFromClient(operation);
return this.http
.put<IOperation>(this.resourceUrl, copy, { observe: 'response' })
.pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
}
find(id: number): Observable<EntityResponseType> {
return this.http
.get<IOperation>(`${this.resourceUrl}/${id}`, { observe: 'response' })
.pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
}
query(req?: any): Observable<EntityArrayResponseType> {
const options = createRequestOption(req);
return this.http
.get<IOperation[]>(this.resourceUrl, { params: options, observe: 'response' })
.pipe(map((res: EntityArrayResponseType) => this.convertDateArrayFromServer(res)));
}
delete(id: number): Observable<HttpResponse<{}>> {
return this.http.delete(`${this.resourceUrl}/${id}`, { observe: 'response' });
}
protected convertDateFromClient(operation: IOperation): IOperation {
const copy: IOperation = Object.assign({}, operation, {
date: operation.date && operation.date.isValid() ? operation.date.toJSON() : undefined,
});
return copy;
}
protected convertDateFromServer(res: EntityResponseType): EntityResponseType {
if (res.body) {
res.body.date = res.body.date ? moment(res.body.date) : undefined;
}
return res;
}
protected convertDateArrayFromServer(res: EntityArrayResponseType): EntityArrayResponseType {
if (res.body) {
res.body.forEach((operation: IOperation) => {
operation.date = operation.date ? moment(operation.date) : undefined;
});
}
return res;
}
}