Created
May 11, 2020 16:53
-
-
Save coderkan/426325c1b6433ae2184a2467ad76c5ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable({ | |
providedIn: "root", | |
}) | |
export class EmployeeService { | |
constructor(private http: HttpClient) {} | |
/** | |
* Get All Employee request. | |
*/ | |
getAllEmployes(): Observable<any> { | |
return this.http | |
.get<any>("http://localhost:4200/employes", { observe: "response" }) | |
.pipe( | |
retry(3), // retry a failed request up to 3 times | |
catchError(this.handleError) // then handle the error | |
); | |
} | |
/** | |
* Add a new employee post requts. | |
* @param employee a new employee to add. | |
*/ | |
addEmployee(employee: any): Observable<any> { | |
return this.http | |
.post<any>("http://localhost:4200/employes", employee, { | |
observe: "response", | |
}) | |
.pipe(catchError(this.handleError)); | |
} | |
/** | |
* Delete an employee request method. | |
* @param empId employee unique id | |
*/ | |
deleteEmployee(empId: any) { | |
return this.http | |
.delete<any>("http://localhost:4200/employes/" + empId, { | |
observe: "response", | |
}) | |
.pipe(catchError(this.handleError)); | |
} | |
private handleError(error: HttpErrorResponse) { | |
if (error.error instanceof ErrorEvent) { | |
// A client-side or network error occurred. Handle it accordingly. | |
console.error("An error occurred:", error.error.message); | |
} else { | |
// The backend returned an unsuccessful response code. | |
// The response body may contain clues as to what went wrong, | |
console.error( | |
`Backend returned code ${error.status}, ` + `body was: ${error.error}` | |
); | |
} | |
// return an observable with a user-facing error message | |
return throwError("Something bad happened; please try again later."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment