brewman/overlord/src/app/role/role.service.ts

57 lines
1.6 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 { Role } from './role';
const url = '/api/roles';
const serviceName = 'RoleService';
@Injectable({
providedIn: 'root',
})
export class RoleService {
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
get(id: string | null): Observable<Role> {
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
return this.http
.get<Role>(getUrl)
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<Role>;
}
list(): Observable<Role[]> {
return this.http
.get<Role[]>(`${url}/list`)
.pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable<Role[]>;
}
save(role: Role): Observable<Role> {
return this.http
.post<Role>(`${url}`, role)
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<Role>;
}
update(role: Role): Observable<Role> {
return this.http
.put<Role>(`${url}/${role.id}`, role)
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<Role>;
}
saveOrUpdate(role: Role): Observable<Role> {
if (!role.id) {
return this.save(role);
}
return this.update(role);
}
delete(id: string): Observable<Role> {
return this.http
.delete<Role>(`${url}/${id}`)
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<Role>;
}
}