2022-07-15 07:54:25 +00:00
|
|
|
import { HttpClient } from '@angular/common/http';
|
2020-10-10 03:15:05 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
2020-06-30 06:02:09 +00:00
|
|
|
import { Observable } from 'rxjs/internal/Observable';
|
2020-10-10 03:15:05 +00:00
|
|
|
import { catchError } from 'rxjs/operators';
|
|
|
|
|
|
|
|
import { ErrorLoggerService } from '../core/error-logger.service';
|
|
|
|
|
2020-06-30 06:02:09 +00:00
|
|
|
import { Role } from './role';
|
2020-05-11 20:01:21 +00:00
|
|
|
|
|
|
|
const url = '/api/roles';
|
|
|
|
const serviceName = 'RoleService';
|
|
|
|
|
|
|
|
@Injectable({
|
2020-10-01 15:21:22 +00:00
|
|
|
providedIn: 'root',
|
2020-05-11 20:01:21 +00:00
|
|
|
})
|
|
|
|
export class RoleService {
|
2020-10-01 15:21:22 +00:00
|
|
|
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
|
2020-05-11 20:01:21 +00:00
|
|
|
|
2020-11-23 11:12:54 +00:00
|
|
|
get(id: string | null): Observable<Role> {
|
2020-10-01 15:21:22 +00:00
|
|
|
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
|
2020-12-08 06:39:19 +00:00
|
|
|
return this.http
|
|
|
|
.get<Role>(getUrl)
|
2020-12-08 07:29:26 +00:00
|
|
|
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<Role>;
|
2020-05-11 20:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list(): Observable<Role[]> {
|
2020-12-08 06:39:19 +00:00
|
|
|
return this.http
|
|
|
|
.get<Role[]>(`${url}/list`)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable<Role[]>;
|
2020-05-11 20:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
save(role: Role): Observable<Role> {
|
2020-12-08 06:39:19 +00:00
|
|
|
return this.http
|
|
|
|
.post<Role>(`${url}`, role)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<Role>;
|
2020-05-11 20:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update(role: Role): Observable<Role> {
|
2020-12-08 06:39:19 +00:00
|
|
|
return this.http
|
|
|
|
.put<Role>(`${url}/${role.id}`, role)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<Role>;
|
2020-05-11 20:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
saveOrUpdate(role: Role): Observable<Role> {
|
|
|
|
if (!role.id) {
|
|
|
|
return this.save(role);
|
|
|
|
}
|
2020-10-10 03:15:05 +00:00
|
|
|
return this.update(role);
|
2020-05-11 20:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(id: string): Observable<Role> {
|
2020-12-08 06:39:19 +00:00
|
|
|
return this.http
|
|
|
|
.delete<Role>(`${url}/${id}`)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<Role>;
|
2020-05-11 20:01:21 +00:00
|
|
|
}
|
|
|
|
}
|