import { Injectable } from '@angular/core' ; import { Http , Response , Headers } from '@angular/http' ; import { Observable } from 'rxjs/Observable' ; import { Subject } from 'rxjs/Subject' ; import { User } from '../models/user' ; @ Injectable ( ) export class UserService { private usersUrl : string = 'https://reqres.in/api/users' ; private userCreatedSource = new Subject < User > ( ) ; private userDeletedSource = new Subject ( ) ; userCreated$ = this . userCreatedSource . asObservable ( ) ; userDeleted$ = this . userDeletedSource . asObservable ( ) ; constructor ( private http ) { } /**
     * Get all users
     */ getUsers ( ) { return this . http . get ( this . usersUrl ) . map ( res => res . json ( ) . data ) . map ( users => users . map ( this . toUser ) ) . catch ( this . handleError ) ; } /**
     * Get a single user
     */ getUser ( id ) { let headers = new Headers ( ) ; let token = localStorage . getItem ( 'auth_token' ) ; headers . append ( 'Content-Type' , 'application/json' ) ; headers . append ( 'Authorization' , `Bearer ${ token }` ) ; return this . http . get ( `${ this . usersUrl }/${ id }` , { headers } ) . map ( res => res . json ( ) . data ) . map ( this . toUser ) . catch ( this . handleError ) ; } /**
     * Create the user
     */ createUser ( user ) { return this . http . post ( this . usersUrl , user ) . map ( res => res . json ( ) ) . do ( user => this . userCreated ( user ) ) . catch ( this . handleError ) ; } /**
     * Update the user
     */ updateUser ( user ) { return this . http . put ( `${ this . usersUrl }/${ user . id }` , user ) . map ( res => res . json ( ) ) . catch ( this . handleError ) ; } /**
     * Delete the user
     */ deleteUser ( id ) { return this . http . delete ( `${ this . usersUrl }/${ id }` ) . do ( res => this . userDeleted ( ) ) . catch ( this . handleError ) ; } /**
     * The user was created. Add this info to our stream
     */ userCreated ( user ) { this . userCreatedSource . next ( user ) ; } /**
     * The user was deleted. Add this info to our stream
     */ userDeleted ( ) { this . userDeletedSource . next ( ) ; } /**
     * Convert user info from the API to our standard/format
     */ private toUser ( user ) { return { id : user . id , name : `${ user . first_name } ${ user . last_name }` , username : user . first_name , avatar : user . avatar } ; } /**
     * Handle any errors from the API
     */ private handleError ( err ) { let errMessage ; if ( err instanceof Response ) { let body = err . json ( ) || '' ; let error = body . error || JSON . stringify ( body ) ; errMessage = `${ err . status } - ${ err . statusText || '' } ${ error }` ; } else { errMessage = err . message ? err . message : err . toString ( ) ; } return Observable . throw ( errMessage ) ; } }