2020-06-14 13:13:10 +00:00
|
|
|
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
2020-10-11 05:26:29 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
2020-06-14 13:13:10 +00:00
|
|
|
import { Observable } from 'rxjs/internal/Observable';
|
2020-10-11 05:26:29 +00:00
|
|
|
import { catchError } from 'rxjs/operators';
|
|
|
|
|
|
|
|
import { ErrorLoggerService } from '../core/error-logger.service';
|
|
|
|
|
2020-06-14 13:13:10 +00:00
|
|
|
import { Role } from './role';
|
2019-06-13 19:02:34 +00:00
|
|
|
|
|
|
|
const httpOptions = {
|
2020-10-11 05:26:29 +00:00
|
|
|
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
2019-06-13 19:02:34 +00:00
|
|
|
};
|
2020-06-14 13:13:10 +00:00
|
|
|
const url = '/api/roles';
|
2019-06-13 19:02:34 +00:00
|
|
|
const serviceName = 'RoleService';
|
|
|
|
|
|
|
|
@Injectable({
|
2020-10-11 05:26:29 +00:00
|
|
|
providedIn: 'root',
|
2019-06-13 19:02:34 +00:00
|
|
|
})
|
|
|
|
export class RoleService {
|
2020-10-11 05:26:29 +00:00
|
|
|
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
|
2019-06-13 19:02:34 +00:00
|
|
|
|
|
|
|
get(id: string): Observable<Role> {
|
2020-10-11 05:26:29 +00:00
|
|
|
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
|
|
|
|
return <Observable<Role>>(
|
|
|
|
this.http
|
|
|
|
.get<Role>(getUrl)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`)))
|
|
|
|
);
|
2019-06-13 19:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list(): Observable<Role[]> {
|
2020-10-11 05:26:29 +00:00
|
|
|
return <Observable<Role[]>>(
|
|
|
|
this.http
|
|
|
|
.get<Role[]>(`${url}/list`)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, 'list')))
|
|
|
|
);
|
2019-06-13 19:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
save(role: Role): Observable<Role> {
|
2020-10-11 05:26:29 +00:00
|
|
|
return <Observable<Role>>(
|
|
|
|
this.http
|
|
|
|
.post<Role>(`${url}`, role, httpOptions)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, 'save')))
|
|
|
|
);
|
2019-06-13 19:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update(role: Role): Observable<Role> {
|
2020-10-11 05:26:29 +00:00
|
|
|
return <Observable<Role>>(
|
|
|
|
this.http
|
|
|
|
.put<Role>(`${url}/${role.id}`, role, httpOptions)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, 'update')))
|
|
|
|
);
|
2019-06-13 19:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
saveOrUpdate(role: Role): Observable<Role> {
|
|
|
|
if (!role.id) {
|
|
|
|
return this.save(role);
|
|
|
|
}
|
2020-10-11 05:26:29 +00:00
|
|
|
return this.update(role);
|
2019-06-13 19:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(id: string): Observable<Role> {
|
2020-10-11 05:26:29 +00:00
|
|
|
return <Observable<Role>>(
|
|
|
|
this.http
|
|
|
|
.delete<Role>(`${url}/${id}`, httpOptions)
|
|
|
|
.pipe(catchError(this.log.handleError(serviceName, 'delete')))
|
|
|
|
);
|
2019-06-13 19:02:34 +00:00
|
|
|
}
|
|
|
|
}
|