user.service.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3. import { environment } from '@environments/environment';
  4. import { User } from '@app/models';
  5. import { Observable } from 'rxjs/internal/Observable';
  6. import { throwError } from 'rxjs/internal/observable/throwError';
  7. import { map, catchError } from 'rxjs/operators';
  8. @Injectable({ providedIn: 'root' })
  9. export class UserService {
  10. constructor(private http: HttpClient) { }
  11. getAllUsers() {
  12. return this.http.get<User[]>(`${environment.productionApiUrl}/users`);
  13. }
  14. getById(id: number) {
  15. return this.http.get<User>(`${environment.apiUrl}/users/${id}`);
  16. }
  17. createUser(user: { first_name :string, last_name :string,
  18. email :string, role :number }): Observable<boolean> {
  19. return this.http.post<any>(`${environment.productionApiUrl}/users`, user)
  20. .pipe(
  21. map(response => {
  22. return response;
  23. }),
  24. catchError(this.errorHandl)
  25. )
  26. }
  27. validateUserToken(user: {token :string}): Observable<boolean> {
  28. return this.http.post<any>(`${environment.productionApiUrl}/user/tokenvalidation`, user)
  29. .pipe(
  30. map(response => {
  31. return response;
  32. }),
  33. catchError(this.errorHandl)
  34. )
  35. }
  36. activateUser(user: { first_name :string, last_name :string,
  37. email :string, password: string, confirm_password :string }): Observable<boolean> {
  38. return this.http.post<any>(`${environment.productionApiUrl}/user/activate`, user)
  39. .pipe(
  40. map(response => {
  41. return response;
  42. }),
  43. catchError(this.errorHandl)
  44. )
  45. }
  46. errorHandl(error) {
  47. let errorMessage = '';
  48. if(error.error) {
  49. // Get client-side error
  50. errorMessage = error.error;
  51. } else {
  52. // Get server-side error
  53. errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
  54. }
  55. return throwError(errorMessage);
  56. }
  57. }