49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
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<AppUser[]>;
|
|
|
|
private appUserStore = new Store<AppUser>();
|
|
|
|
constructor(private httpGateway: HttpGateway,
|
|
private config: AppConfig) {
|
|
this.users$ = this.appUserStore.items$;
|
|
}
|
|
|
|
getUsers(): Observable<AppUser[]> {
|
|
this.httpGateway.get<AppUser[]>(this.config.apiAppUserPath)
|
|
.do((users) => {
|
|
this.appUserStore.dispatch({type: LOAD, data: users});
|
|
}).subscribe(_ => {
|
|
});
|
|
|
|
return this.users$;
|
|
}
|
|
|
|
getAppUser(id: string): Observable<AppUser> {
|
|
return this.httpGateway.get<AppUser>(this.config.apiAppUserPath + id);
|
|
}
|
|
|
|
updateUser(user: AppUser): Observable<AppUser> {
|
|
return this.httpGateway.patch<AppUser>(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});
|
|
});
|
|
}
|
|
}
|