diff --git a/src/packages/notification/service/notification.service.spec.ts b/src/packages/notification/service/notification.service.spec.ts index 872e93f..44bc1ec 100644 --- a/src/packages/notification/service/notification.service.spec.ts +++ b/src/packages/notification/service/notification.service.spec.ts @@ -2,7 +2,7 @@ import { TestBed, inject } from '@angular/core/testing'; import { NotificationService } from './notification.service'; -describe('MemberService', () => { +describe('NotificationService', () => { beforeEach(() => { TestBed.configureTestingModule({ providers: [NotificationService] diff --git a/src/packages/sensor/sensor-store.module.ts b/src/packages/sensor/sensor-store.module.ts new file mode 100644 index 0000000..a977917 --- /dev/null +++ b/src/packages/sensor/sensor-store.module.ts @@ -0,0 +1,24 @@ +import { NgModule } from '@angular/core'; +import { StoreModule } from '@ngrx/store'; +import { StoreDevtoolsModule } from '@ngrx/store-devtools'; +import { + StoreRouterConnectingModule, + RouterStateSerializer, +} from '@ngrx/router-store'; +import { EffectsModule } from '@ngrx/effects'; +import { combineReducers, ActionReducer, ActionReducerMap, MetaReducer } from '@ngrx/store'; + +import { + REDUCERS, + EFFECTS, +} from './store'; + +import { MODULE } from './sensor.constant'; + +@NgModule({ + imports: [ + StoreModule.forFeature(MODULE.name, REDUCERS), + EffectsModule.forFeature(EFFECTS), + ], +}) +export class SensorStoreModule { } diff --git a/src/packages/sensor/sensor.constant.ts b/src/packages/sensor/sensor.constant.ts new file mode 100644 index 0000000..16657cf --- /dev/null +++ b/src/packages/sensor/sensor.constant.ts @@ -0,0 +1,3 @@ +export const MODULE = { + name: 'Sensor' + }; diff --git a/src/packages/sensor/sensor.module.ts b/src/packages/sensor/sensor.module.ts index 9afe9fa..45ceb75 100644 --- a/src/packages/sensor/sensor.module.ts +++ b/src/packages/sensor/sensor.module.ts @@ -5,12 +5,15 @@ import {FormsModule} from '@angular/forms'; import { MaterialModule } from 'app/commons/ui/material/material.module'; import { COMPONENTS } from './component'; +import { SERVICES } from './service'; +import { SensorStoreModule } from './sensor-store.module'; @NgModule({ imports: [ CommonModule, MaterialModule, - FormsModule + FormsModule, + SensorStoreModule ], declarations: [ COMPONENTS, @@ -18,5 +21,8 @@ import { COMPONENTS } from './component'; exports: [ COMPONENTS, ], + providers: [ + SERVICES, + ], }) export class SensorModule { } diff --git a/src/packages/sensor/service/index.ts b/src/packages/sensor/service/index.ts new file mode 100644 index 0000000..f9663e9 --- /dev/null +++ b/src/packages/sensor/service/index.ts @@ -0,0 +1,5 @@ +import { SensorService } from './sensor.service'; + +export const SERVICES = [ + SensorService, +]; diff --git a/src/packages/sensor/service/sensor.service.spec.ts b/src/packages/sensor/service/sensor.service.spec.ts new file mode 100644 index 0000000..0090e8a --- /dev/null +++ b/src/packages/sensor/service/sensor.service.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { SensorService } from './sensor.service'; + +describe('SensorService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [SensorService] + }); + }); + + it('should be created', inject([SensorService], (service: SensorService) => { + expect(service).toBeTruthy(); + })); +}); diff --git a/src/packages/sensor/service/sensor.service.ts b/src/packages/sensor/service/sensor.service.ts new file mode 100644 index 0000000..8c35a9f --- /dev/null +++ b/src/packages/sensor/service/sensor.service.ts @@ -0,0 +1,30 @@ +import { Injectable } from '@angular/core'; +import { Observable } from 'rxjs/Observable'; + +import 'rxjs/add/operator/map'; + +import { RPCClient } from 'packages/core/rpc/client/RPCClient'; + +import { Sensor } from '../model'; +import { Domain } from '../../domain/model'; + + +@Injectable() +export class SensorService { + + public constructor( + private rpcClient: RPCClient, + ) { + + } + + public readAllByDomain(domain: Domain): Observable { + const body = { + domain: domain, + }; + + return this.rpcClient.call('SensorService.readAllByDomain', domain); + } + + +} diff --git a/src/packages/sensor/store/index.ts b/src/packages/sensor/store/index.ts new file mode 100644 index 0000000..31c02fc --- /dev/null +++ b/src/packages/sensor/store/index.ts @@ -0,0 +1,30 @@ +import { + createSelector, + createFeatureSelector, + ActionReducerMap, + } from '@ngrx/store'; + + import { StateSelector } from 'packages/commons/util/ngrx/store'; + + import { MODULE } from '../sensor.constant'; + + import * as ReadAllByDomainStore from './readallbydomain'; + + export interface State { + readallbydomain: ReadAllByDomainStore.State; + } + + export const REDUCERS = { + readallbymember: ReadAllByDomainStore.reducer, + }; + + export const EFFECTS = [ + ReadAllByDomainStore.Effects, + ]; + + export const selectNotificationState = createFeatureSelector(MODULE.name); + + export const ReadAllByMemberSelector = new StateSelector(createSelector( + selectNotificationState, + (state: State) => state.readallbydomain + )); diff --git a/src/packages/sensor/store/readallbydomain/index.ts b/src/packages/sensor/store/readallbydomain/index.ts new file mode 100644 index 0000000..72f1cb0 --- /dev/null +++ b/src/packages/sensor/store/readallbydomain/index.ts @@ -0,0 +1,4 @@ +export * from './readallbydomain.action'; +export * from './readallbydomain.effect'; +export * from './readallbydomain.reducer'; +export * from './readallbydomain.state'; diff --git a/src/packages/sensor/store/readallbydomain/readallbydomain.action.ts b/src/packages/sensor/store/readallbydomain/readallbydomain.action.ts new file mode 100644 index 0000000..a9d61cd --- /dev/null +++ b/src/packages/sensor/store/readallbydomain/readallbydomain.action.ts @@ -0,0 +1,36 @@ +import { Action } from '@ngrx/store'; + +import { RPCError } from 'packages/core/rpc/error'; + +import { Sensor } from '../../model'; +import { Domain } from '../../../domain/model'; + +export enum ActionType { + ReadAllByDomain = '[Sensor.ReadAllByDomain] ReadAllByDomain', + ReadAllByDomainSuccess = '[Sensor.ReadAllByDomainSuccess] ReadAllByDomainSuccess', + ReadAllByDomainFailure = '[Sensor.ReadAllByDomainFailure] ReadAllByDomainFailure', +} + +export class ReadAllByDomain implements Action { + readonly type = ActionType.ReadAllByDomain; + + constructor(public payload: Domain) {} +} + +export class ReadAllByDomainSuccess implements Action { + readonly type = ActionType.ReadAllByDomainSuccess; + + constructor(public payload: Sensor[]) {} +} + +export class ReadAllByDomainFailure implements Action { + readonly type = ActionType.ReadAllByDomainFailure; + + constructor(public payload: RPCError) {} +} + +export type Actions = + | ReadAllByDomain + | ReadAllByDomainSuccess + | ReadAllByDomainFailure +; diff --git a/src/packages/sensor/store/readallbydomain/readallbydomain.effect.spec.ts b/src/packages/sensor/store/readallbydomain/readallbydomain.effect.spec.ts new file mode 100644 index 0000000..1e346b7 --- /dev/null +++ b/src/packages/sensor/store/readallbydomain/readallbydomain.effect.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { Effects } from './readallbydomain.effect'; + +describe('ReadAllByDomain.Effects', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [Effects] + }); + }); + + it('should be created', inject([Effects], (effects: Effects) => { + expect(effects).toBeTruthy(); + })); +}); diff --git a/src/packages/sensor/store/readallbydomain/readallbydomain.effect.ts b/src/packages/sensor/store/readallbydomain/readallbydomain.effect.ts new file mode 100644 index 0000000..d6d7eb6 --- /dev/null +++ b/src/packages/sensor/store/readallbydomain/readallbydomain.effect.ts @@ -0,0 +1,48 @@ +import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; + +import { Effect, Actions, ofType } from '@ngrx/effects'; +import { Action } from '@ngrx/store'; + +import { Observable } from 'rxjs/Observable'; +import { of } from 'rxjs/observable/of'; + +import 'rxjs/add/operator/catch'; +import 'rxjs/add/operator/do'; +import 'rxjs/add/operator/exhaustMap'; +import 'rxjs/add/operator/map'; +import 'rxjs/add/operator/take'; + +import { RPCError } from 'packages/core/rpc/error'; + +import { Sensor } from '../../model'; +import { SensorService } from '../../service/sensor.service'; + +import { + ReadAllByDomain, + ReadAllByDomainSuccess, + ReadAllByDomainFailure, + ActionType, +} from './readallbydomain.action'; + +@Injectable() +export class Effects { + + constructor( + private actions$: Actions, + private sensorService: SensorService, + private router: Router + ) { } + + @Effect() + readAllByMember$: Observable = this.actions$ + .ofType(ActionType.ReadAllByDomain) + .map((action: ReadAllByDomain) => action.payload) + .exhaustMap(domain => + this.sensorService + .readAllByDomain(domain) + .map(sensorList => new ReadAllByDomainSuccess(sensorList)) + .catch(error => of(new ReadAllByDomainFailure(error))) + ); + +} diff --git a/src/packages/sensor/store/readallbydomain/readallbydomain.reducer.ts b/src/packages/sensor/store/readallbydomain/readallbydomain.reducer.ts new file mode 100644 index 0000000..1b72e67 --- /dev/null +++ b/src/packages/sensor/store/readallbydomain/readallbydomain.reducer.ts @@ -0,0 +1,45 @@ +import { + Actions, + ActionType, + } from './readallbydomain.action'; + + import { + State, + initialState, + } from './readallbydomain.state'; + + import { Sensor } from '../../model'; + + export function reducer(state = initialState, action: Actions): State { + switch (action.type) { + case ActionType.ReadAllByDomain: { + return { + ...state, + error: null, + pending: true, + }; + } + + case ActionType.ReadAllByDomainSuccess: { + return { + ...state, + error: null, + pending: false, + sensorList: action.payload + }; + } + + case ActionType.ReadAllByDomainFailure: { + return { + ...state, + error: action.payload, + pending: false, + sensorList: null, + }; + } + + default: { + return state; + } + } + } diff --git a/src/packages/sensor/store/readallbydomain/readallbydomain.state.ts b/src/packages/sensor/store/readallbydomain/readallbydomain.state.ts new file mode 100644 index 0000000..8e44587 --- /dev/null +++ b/src/packages/sensor/store/readallbydomain/readallbydomain.state.ts @@ -0,0 +1,15 @@ +import { RPCError } from 'packages/core/rpc/error'; + +import { Sensor } from '../../model'; + +export interface State { + error: RPCError | null; + pending: boolean; + sensorList: Sensor[] | null; +} + +export const initialState: State = { + error: null, + pending: false, + sensorList: null, +};