/
vehicle.service.ts
59 lines (45 loc) · 1.7 KB
/
vehicle.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
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Vehicle } from '../models/vehicle';
import { HandleError } from '../../shared/services/handleError';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
// 'Authorization': 'my-auth-token'
})
};
@Injectable({
providedIn: 'root'
})
export class VehiclesService {
constructor(private http: HttpClient) { }
baseUrl = 'http://localhost:8081/api/fleet-manage/';
getList(): Observable<Vehicle[]> {
const endPoint = 'vehicle';
return this.http.get<Vehicle[]>(this.baseUrl + endPoint)
.pipe(catchError(HandleError.handleError));
}
create(vehicle: Vehicle) {
const endPoint = 'vehicle';
return this.http.post<Vehicle>(this.baseUrl + endPoint, vehicle, httpOptions)
.pipe(catchError(HandleError.handleError));
}
// get a vehicle by their id
getById(id: string): Observable<Vehicle> {
const endPoint = 'vehicle';
return this.http.get<Vehicle>(`${this.baseUrl}${endPoint}/${id}`)
.pipe(catchError(HandleError.handleError));
}
update(vehicle: Vehicle) {
const endPoint = 'vehicle';
return this.http.put(`${this.baseUrl}${endPoint}/${vehicle._id}`, vehicle, httpOptions)
.pipe(catchError(HandleError.handleError));
}
delete(Id): Observable<void> {
const endPoint = 'vehicle';
return this.http.delete<void>(`${this.baseUrl}${endPoint}/${Id}`)
.pipe(catchError(HandleError.handleError));
}
}