68 lines
2.0 KiB
TypeScript
68 lines
2.0 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { Observable } from 'rxjs/internal/Observable';
|
|
import { catchError } from 'rxjs/operators';
|
|
|
|
import { ErrorLoggerService } from '../core/error-logger.service';
|
|
import { User } from '../core/user';
|
|
|
|
const url = '/api/users';
|
|
const serviceName = 'UserService';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class UserService {
|
|
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
|
|
|
|
get(id: string | null): Observable<User> {
|
|
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
|
|
return this.http
|
|
.get<User>(getUrl)
|
|
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<User>;
|
|
}
|
|
|
|
list(): Observable<User[]> {
|
|
return this.http
|
|
.get<User[]>(`${url}/list`)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable<User[]>;
|
|
}
|
|
|
|
listOfNames(): Observable<string[]> {
|
|
return this.http
|
|
.get<string[]>(`${url}/active`)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable<string[]>;
|
|
}
|
|
|
|
save(user: User): Observable<User> {
|
|
return this.http
|
|
.post<User>(`${url}`, user)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<User>;
|
|
}
|
|
|
|
update(user: User): Observable<User> {
|
|
return this.http
|
|
.put<User>(`${url}/${user.id}`, user)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<User>;
|
|
}
|
|
|
|
updateMe(user: User): Observable<User> {
|
|
return this.http
|
|
.put<User>(`${url}/me`, user)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<User>;
|
|
}
|
|
|
|
saveOrUpdate(user: User): Observable<User> {
|
|
if (!user.id) {
|
|
return this.save(user);
|
|
}
|
|
return this.update(user);
|
|
}
|
|
|
|
delete(id: string): Observable<User> {
|
|
return this.http
|
|
.delete<User>(`${url}/${id}`)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<User>;
|
|
}
|
|
}
|