-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathusers.service.ts
More file actions
52 lines (45 loc) · 1.5 KB
/
users.service.ts
File metadata and controls
52 lines (45 loc) · 1.5 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
import { catchError } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { Injectable } from '@angular/core';
import { environment } from '@env/environment';
import { User } from '@app/shared/models/user.interface';
@Injectable({
providedIn: 'root',
})
export class UsersService {
constructor(private http: HttpClient) {}
getAll(): Observable<User[]> {
return this.http
.get<User[]>(`${environment.API_URL}/users`)
.pipe(catchError(this.handlerError));
}
getById(userId: number): Observable<User> {
return this.http
.get<any>(`${environment.API_URL}/users/${userId}`)
.pipe(catchError(this.handlerError));
}
new(user: User): Observable<User> {
return this.http
.post<User>(`${environment.API_URL}/users/`, user)
.pipe(catchError(this.handlerError));
}
update(userId: number, user: User): Observable<User> {
return this.http
.patch<User>(`${environment.API_URL}/users/${userId}`, user)
.pipe(catchError(this.handlerError));
}
delete(userId: number): Observable<{}> {
return this.http
.delete<User>(`${environment.API_URL}/users/${userId}`)
.pipe(catchError(this.handlerError));
}
handlerError(error): Observable<never> {
let errorMessage = 'Error unknown';
if (error) {
errorMessage = `Error ::: ${error.message}`;
}
//////window.alert(errorMessage); /// Devuelve un Error !
return throwError(errorMessage);
}
}