barker/bookie/src/app/roles/role.service.ts
tanshu d677cfb1ea Blacked and isorted the python files
Prettied and eslinted the typescript/html files
2020-10-11 10:56:29 +05:30

70 lines
1.8 KiB
TypeScript

import { HttpClient, HttpHeaders } 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 httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
};
const url = '/api/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}` : `${url}/${id}`;
return <Observable<Role>>(
this.http
.get<Role>(getUrl)
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`)))
);
}
list(): Observable<Role[]> {
return <Observable<Role[]>>(
this.http
.get<Role[]>(`${url}/list`)
.pipe(catchError(this.log.handleError(serviceName, 'list')))
);
}
save(role: Role): Observable<Role> {
return <Observable<Role>>(
this.http
.post<Role>(`${url}`, role, httpOptions)
.pipe(catchError(this.log.handleError(serviceName, 'save')))
);
}
update(role: Role): Observable<Role> {
return <Observable<Role>>(
this.http
.put<Role>(`${url}/${role.id}`, role, httpOptions)
.pipe(catchError(this.log.handleError(serviceName, 'update')))
);
}
saveOrUpdate(role: Role): Observable<Role> {
if (!role.id) {
return this.save(role);
}
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')))
);
}
}