import {Injectable} from '@angular/core'; import {AppUser} from '../../models/model-interfaces'; import {Observable} from 'rxjs/Observable'; import {EDIT, LOAD, REMOVE, Store} from '../stores/generic-store'; import {AppConfig} from '../../app.config'; import {HttpGateway} from '../http-gateway'; @Injectable() export class AppUserService { users$: Observable; private appUserStore = new Store(); constructor(private httpGateway: HttpGateway, private config: AppConfig) { this.users$ = this.appUserStore.items$; } getUsers(): Observable { this.httpGateway.get(this.config.apiAppUserPath) .do((users) => { this.appUserStore.dispatch({type: LOAD, data: users}); }).subscribe(_ => { }); return this.users$; } getAppUser(id: string): Observable { return this.httpGateway.get(this.config.apiAppUserPath + id); } updateUser(user: AppUser): Observable { return this.httpGateway.patch(this.config.apiAppUserPath + user._id, user) .do(savedUser => { const action = {type: EDIT, data: savedUser}; this.appUserStore.dispatch(action); }); } deleteUser(user) { return this.httpGateway.delete(this.config.apiAppUserPath + user._id) .do(res => { this.appUserStore.dispatch({type: REMOVE, data: user}); }); } }