2019-06-13 19:02:34 +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';
|
|
|
|
import {Role} from './role';
|
|
|
|
|
|
|
|
const httpOptions = {
|
|
|
|
headers: new HttpHeaders({'Content-Type': 'application/json'})
|
|
|
|
};
|
|
|
|
const url = '/v1/roles';
|
|
|
|
const serviceName = 'RoleService';
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class RoleService {
|
|
|
|
constructor(private http: HttpClient, private log: ErrorLoggerService) {
|
|
|
|
}
|
|
|
|
|
|
|
|
get(id: string): Observable<Role> {
|
|
|
|
const getUrl: string = (id === null) ? `${url}/new` : `${url}/${id}`;
|
|
|
|
return <Observable<Role>>this.http.get<Role>(getUrl)
|
|
|
|
.pipe(
|
|
|
|
catchError(this.log.handleError(serviceName, `get id=${id}`))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
list(): Observable<Role[]> {
|
|
|
|
const options = {params: new HttpParams().set('l', '')};
|
2019-06-19 18:25:42 +00:00
|
|
|
return <Observable<Role[]>>this.http.get<Role[]>(url, options)
|
|
|
|
.pipe(
|
|
|
|
catchError(this.log.handleError(serviceName, 'list'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
listOfNames(): Observable<string[]> {
|
|
|
|
const options = {params: new HttpParams().set('n', '')};
|
|
|
|
return <Observable<string[]>>this.http.get<string[]>(url, options)
|
2019-06-13 19:02:34 +00:00
|
|
|
.pipe(
|
|
|
|
catchError(this.log.handleError(serviceName, 'list'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
save(role: Role): Observable<Role> {
|
2019-06-19 18:25:42 +00:00
|
|
|
return <Observable<Role>>this.http.post<Role>(`${url}/new`, role, httpOptions)
|
2019-06-13 19:02:34 +00:00
|
|
|
.pipe(
|
|
|
|
catchError(this.log.handleError(serviceName, 'save'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
update(role: Role): Observable<Role> {
|
2019-06-19 18:25:42 +00:00
|
|
|
return <Observable<Role>>this.http.put<Role>(`${url}/${role.id}`, role, httpOptions)
|
2019-06-13 19:02:34 +00:00
|
|
|
.pipe(
|
|
|
|
catchError(this.log.handleError(serviceName, 'update'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveOrUpdate(role: Role): Observable<Role> {
|
|
|
|
if (!role.id) {
|
|
|
|
return this.save(role);
|
|
|
|
} else {
|
|
|
|
return this.update(role);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(id: string): Observable<Role> {
|
|
|
|
return <Observable<Role>>this.http.delete<Role>(`${url}/${id}`, httpOptions)
|
|
|
|
.pipe(
|
|
|
|
catchError(this.log.handleError(serviceName, 'delete'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|