2018-05-25 13:49:00 +00:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
|
|
|
|
import {ErrorLoggerService} from '../core/error-logger.service';
|
|
|
|
import {catchError} from 'rxjs/operators';
|
|
|
|
import {Observable} from 'rxjs/internal/Observable';
|
2019-06-12 14:04:50 +00:00
|
|
|
import {User} from '../core/user';
|
2018-05-25 13:49:00 +00:00
|
|
|
|
|
|
|
const httpOptions = {
|
|
|
|
headers: new HttpHeaders({'Content-Type': 'application/json'})
|
|
|
|
};
|
2019-06-12 14:04:50 +00:00
|
|
|
const url = '/api/users';
|
2018-05-25 13:49:00 +00:00
|
|
|
const serviceName = 'UserService';
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class UserService {
|
|
|
|
constructor(private http: HttpClient, private log: ErrorLoggerService) {
|
|
|
|
}
|
|
|
|
|
|
|
|
get(id: string): Observable<User> {
|
2020-05-11 20:01:21 +00:00
|
|
|
const getUrl: string = (id === null) ? `${url}` : `${url}/${id}`;
|
2018-05-25 13:49:00 +00:00
|
|
|
return <Observable<User>>this.http.get<User>(getUrl)
|
|
|
|
.pipe(
|
|
|
|
catchError(this.log.handleError(serviceName, `get id=${id}`))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
list(): Observable<User[]> {
|
2020-05-11 20:01:21 +00:00
|
|
|
return <Observable<User[]>>this.http.get<User[]>(`${url}/list`)
|
2018-05-25 13:49:00 +00:00
|
|
|
.pipe(
|
|
|
|
catchError(this.log.handleError(serviceName, 'list'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
listOfNames(): Observable<string[]> {
|
2020-05-11 20:01:21 +00:00
|
|
|
return <Observable<string[]>>this.http.get<string[]>(`${url}/active`)
|
2018-05-25 13:49:00 +00:00
|
|
|
.pipe(
|
|
|
|
catchError(this.log.handleError(serviceName, 'list'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
save(user: User): Observable<User> {
|
2020-05-11 20:01:21 +00:00
|
|
|
return <Observable<User>>this.http.post<User>(`${url}`, user, httpOptions)
|
2018-05-25 13:49:00 +00:00
|
|
|
.pipe(
|
|
|
|
catchError(this.log.handleError(serviceName, 'save'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
update(user: User): Observable<User> {
|
|
|
|
return <Observable<User>>this.http.put<User>(`${url}/${user.id}`, user, httpOptions)
|
|
|
|
.pipe(
|
|
|
|
catchError(this.log.handleError(serviceName, 'update'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveOrUpdate(user: User): Observable<User> {
|
|
|
|
if (!user.id) {
|
|
|
|
return this.save(user);
|
|
|
|
} else {
|
|
|
|
return this.update(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(id: string): Observable<User> {
|
|
|
|
return <Observable<User>>this.http.delete<User>(`${url}/${id}`, httpOptions)
|
|
|
|
.pipe(
|
|
|
|
catchError(this.log.handleError(serviceName, 'delete'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|