Optimize database structure

keep-around/dc735e80914fa50c9bca8f6c78c100b7ed19408f
Florian Hartwich 2017-03-21 03:34:47 +01:00
parent 3495586a3b
commit c7f137c48a
2 changed files with 3 additions and 39 deletions

View File

@ -1,9 +1,8 @@
import {Injectable, Inject, Optional} from '@angular/core'; import {Injectable, Inject, Optional} from '@angular/core';
import {User} from '../../models/model-interfaces'; import {User} from '../../models/model-interfaces';
import {Http, URLSearchParams, RequestMethod, RequestOptions} from '@angular/http'; import {Http} from '@angular/http';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {BehaviorSubject} from 'rxjs/BehaviorSubject'; import {UserStore} from '../stores/user.store';
import {LOAD, ADD, EDIT, REMOVE, UserStore} from '../stores/user.store';
import {SOCKET_IO, AUTH_ENABLED} from '../../app.tokens'; import {SOCKET_IO, AUTH_ENABLED} from '../../app.tokens';
const BASE_URL = `http://localhost:3000/api/users`; const BASE_URL = `http://localhost:3000/api/users`;
@ -25,10 +24,6 @@ export class LoginService {
@Inject(SOCKET_IO) socketIO) { @Inject(SOCKET_IO) socketIO) {
this.users$ = userStore.items$; this.users$ = userStore.items$;
this.socket = socketIO(WEB_SOCKET_URL); this.socket = socketIO(WEB_SOCKET_URL);
Observable.fromEvent(this.socket, 'user_saved')
.subscribe((action) => {
this.userStore.dispatch(action);
});
} }
getUser(name: string) : User[]{ getUser(name: string) : User[]{

View File

@ -1,39 +1,8 @@
import {BehaviorSubject} from 'rxjs/BehaviorSubject'; import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {User} from '../../models/model-interfaces'; import {User} from '../../models/model-interfaces';
export const LOAD = 'LOAD';
export const ADD = 'ADD';
export const EDIT = 'EDIT';
export const REMOVE = 'REMOVE';
export class UserStore { export class UserStore {
private users: User[] = [];
items$ = new BehaviorSubject<User[]>([]); items$ = new BehaviorSubject<User[]>([]);
dispatch(action) {
console.log("dispatched")
this.users = this._reduce(this.users, action);
this.items$.next(this.users);
}
_reduce(users: User[], action) {
switch (action.type) {
case LOAD:
return [...action.data];
case ADD:
return [...users, action.data];
case EDIT:
return users.map(user => {
const editedUser = action.data;
if (user.id !== editedUser.id){
return user;
}
return editedUser;
});
case REMOVE:
return users.filter(user => user.id !== action.data.id);
default:
return users;
}
}
} }