blob: 83c3b9a02e3d06d7d7d1809567bdfca6ab9a88e8 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolo035c5932016-12-14 09:55:15 -080019import {BehaviorSubject} from 'rxjs';
20import * as _ from 'lodash';
Matteo Scandolo04964232017-01-07 12:53:46 -080021import * as pluralize from 'pluralize';
Matteo Scandolo035c5932016-12-14 09:55:15 -080022import {IWSEvent} from '../websocket/global';
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080023import {IXosResourceService} from '../rest/model.rest';
Matteo Scandolo035c5932016-12-14 09:55:15 -080024
25export interface IStoreHelpersService {
Matteo Scandolo04964232017-01-07 12:53:46 -080026 urlFromCoreModel(name: string): string;
Matteo Scandolo035c5932016-12-14 09:55:15 -080027 updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any>;
28}
29
Matteo Scandolo1aee1982017-02-17 08:33:23 -080030export class StoreHelpers implements IStoreHelpersService {
Matteo Scandolo04964232017-01-07 12:53:46 -080031 static $inject = ['ModelRest'];
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080032
33 constructor (
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080034 private modelRest: IXosResourceService
35 ) {
36 }
37
Matteo Scandolo04964232017-01-07 12:53:46 -080038 public urlFromCoreModel(name: string): string {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080039 return `/core/${pluralize(name.toLowerCase())}`;
Matteo Scandolo04964232017-01-07 12:53:46 -080040 }
41
Matteo Scandolo035c5932016-12-14 09:55:15 -080042 public updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any> {
43 const collection: any[] = subject.value;
44 const index: number = _.findIndex(collection, (i) => {
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080045 // NOTE evaluate to use event.msg.pk
Matteo Scandolo035c5932016-12-14 09:55:15 -080046 return i.id === event.msg.object.id;
47 });
48 const exist: boolean = index > -1;
49 const isDeleted: boolean = _.includes(event.msg.changed_fields, 'deleted');
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080050
51 // generate a resource for the model
Matteo Scandolo04964232017-01-07 12:53:46 -080052 const endpoint = this.urlFromCoreModel(event.model);
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080053 const resource = this.modelRest.getResource(endpoint);
54 const model = new resource(event.msg.object);
55
56 // remove
Matteo Scandolo035c5932016-12-14 09:55:15 -080057 if (exist && isDeleted) {
58 _.remove(collection, {id: event.msg.object.id});
59 }
60 // Replace item at index using native splice
61 else if (exist && !isDeleted) {
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080062 collection.splice(index, 1, model);
Matteo Scandolo035c5932016-12-14 09:55:15 -080063 }
64 // if the element is not deleted add it
65 else if (!exist && !isDeleted) {
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080066 collection.push(model);
Matteo Scandolo035c5932016-12-14 09:55:15 -080067 }
68
69 subject.next(collection);
70
71 return subject;
72 }
73}