2018-03-07 11:56:50 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {AppUser} from '../../models/model-interfaces';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
2018-10-07 17:46:29 +02:00
|
|
|
import {EDIT, LOAD, REMOVE, Store} from '../stores/generic-store';
|
2018-03-07 11:56:50 +01:00
|
|
|
import {AppConfig} from '../../app.config';
|
2018-10-10 00:05:32 +02:00
|
|
|
import {HttpGateway} from '../http-gateway';
|
2017-06-08 19:46:36 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AppUserService {
|
|
|
|
|
|
|
|
users$: Observable<AppUser[]>;
|
|
|
|
|
2018-10-07 17:46:29 +02:00
|
|
|
private appUserStore = new Store<AppUser>();
|
|
|
|
|
2018-10-09 23:23:41 +02:00
|
|
|
constructor(private httpGateway: HttpGateway,
|
2017-06-08 19:46:36 +02:00
|
|
|
private config: AppConfig) {
|
2018-10-07 17:46:29 +02:00
|
|
|
this.users$ = this.appUserStore.items$;
|
2017-06-08 19:46:36 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 14:05:52 +01:00
|
|
|
getUsers(): Observable<AppUser[]> {
|
2018-10-14 15:56:52 +02:00
|
|
|
this.httpGateway.get<AppUser[]>(this.config.apiAppUserPath)
|
2018-02-26 09:04:27 +01:00
|
|
|
.do((users) => {
|
|
|
|
this.appUserStore.dispatch({type: LOAD, data: users});
|
|
|
|
}).subscribe(_ => {
|
2017-06-08 19:46:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return this.users$;
|
|
|
|
}
|
|
|
|
|
2018-11-24 14:05:52 +01:00
|
|
|
getAppUser(id: string): Observable<AppUser> {
|
|
|
|
return this.httpGateway.get<AppUser>(this.config.apiAppUserPath + id);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateUser(user: AppUser): Observable<AppUser> {
|
2018-10-14 15:56:52 +02:00
|
|
|
return this.httpGateway.patch<AppUser>(this.config.apiAppUserPath + user._id, user)
|
2018-02-26 09:04:27 +01:00
|
|
|
.do(savedUser => {
|
|
|
|
const action = {type: EDIT, data: savedUser};
|
|
|
|
this.appUserStore.dispatch(action);
|
|
|
|
});
|
2017-06-08 19:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteUser(user) {
|
2018-10-09 23:23:41 +02:00
|
|
|
return this.httpGateway.delete(this.config.apiAppUserPath + user._id)
|
2018-02-26 09:04:27 +01:00
|
|
|
.do(res => {
|
|
|
|
this.appUserStore.dispatch({type: REMOVE, data: user});
|
|
|
|
});
|
2017-06-08 19:46:36 +02:00
|
|
|
}
|
|
|
|
}
|