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