Merge "Creating resource on the fly when we want to delete model created by WS events"
diff --git a/src/app/core/index.ts b/src/app/core/index.ts
index a9aa9a6..fd87e38 100644
--- a/src/app/core/index.ts
+++ b/src/app/core/index.ts
@@ -13,6 +13,7 @@
 import {xosForm} from './form/form';
 import {xosField} from './field/field';
 import 'angular-toastr';
+import {ModelHelpers} from './services/helpers/model.helper';
 
 export const xosCore = 'xosCore';
 
@@ -24,6 +25,7 @@
   .service('PageTitle', PageTitle)
   .service('XosFormHelpers', XosFormHelpers)
   .service('ConfigHelpers', ConfigHelpers)
+  .service('ModelHelpers', ModelHelpers)
   .directive('xosLinkWrapper', xosLinkWrapper)
   .component('xosHeader', xosHeader)
   .component('xosFooter', xosFooter)
diff --git a/src/app/core/services/helpers/model.helper.ts b/src/app/core/services/helpers/model.helper.ts
new file mode 100644
index 0000000..81825d3
--- /dev/null
+++ b/src/app/core/services/helpers/model.helper.ts
@@ -0,0 +1,9 @@
+export interface IXosModelHelpersService {
+  urlFromCoreModel(name: string): string;
+}
+
+export class ModelHelpers {
+  urlFromCoreModel(name: string): string {
+    return `/core/${name.toLowerCase()}s`;
+  }
+}
diff --git a/src/app/core/services/helpers/model.helpers.spec.ts b/src/app/core/services/helpers/model.helpers.spec.ts
new file mode 100644
index 0000000..8456738
--- /dev/null
+++ b/src/app/core/services/helpers/model.helpers.spec.ts
@@ -0,0 +1,28 @@
+import * as angular from 'angular';
+import 'angular-mocks';
+import 'angular-ui-router';
+
+import {IXosModelHelpersService, ModelHelpers} from './model.helper';
+
+let service: IXosModelHelpersService;
+describe('The ConfigHelpers service', () => {
+
+  beforeEach(() => {
+    angular
+      .module('test', [])
+      .service('ModelHelpers', ModelHelpers);
+
+    angular.mock.module('test');
+  });
+
+  beforeEach(angular.mock.inject((
+    ModelHelpers: IXosModelHelpersService,
+  ) => {
+    service = ModelHelpers;
+  }));
+
+  it('should convert a core model name in an URL', () => {
+    expect(service.urlFromCoreModel('Slice')).toBe('/core/slices');
+  });
+
+});
diff --git a/src/app/datasources/index.ts b/src/app/datasources/index.ts
index c82f317..7a14a9a 100644
--- a/src/app/datasources/index.ts
+++ b/src/app/datasources/index.ts
@@ -5,11 +5,12 @@
 import {StoreHelpers} from './helpers/store.helpers';
 import {SynchronizerStore} from './stores/synchronizer.store';
 import {ModeldefsService} from './rest/modeldefs.rest';
+import {xosCore} from '../core/index';
 
 export const xosDataSources = 'xosDataSources';
 
 angular
-  .module('xosDataSources', ['ngCookies', 'ngResource'])
+  .module('xosDataSources', ['ngCookies', 'ngResource', xosCore])
   .service('ModelRest', ModelRest)
   .service('AuthService', AuthService)
   .service('WebSocket', WebSocketEvent);
diff --git a/src/app/datasources/rest/auth.rest.ts b/src/app/datasources/rest/auth.rest.ts
index 520952d..f963799 100644
--- a/src/app/datasources/rest/auth.rest.ts
+++ b/src/app/datasources/rest/auth.rest.ts
@@ -37,7 +37,6 @@
     const d = this.$q.defer();
     this.$http.post(`${AppConfig.apiEndpoint}/utility/login/`, data)
       .then((res: IAuthResponseData) => {
-        console.log(res.data);
         this.$cookies.put('xoscsrftoken', res.data.xoscsrftoken);
         this.$cookies.put('xossessionid', res.data.xossessionid);
         this.$cookies.put('xosuser', res.data.user);
diff --git a/src/app/datasources/stores/model.store.spec.ts b/src/app/datasources/stores/model.store.spec.ts
index dfe0d24..a6edcb9 100644
--- a/src/app/datasources/stores/model.store.spec.ts
+++ b/src/app/datasources/stores/model.store.spec.ts
@@ -7,6 +7,7 @@
 import {StoreHelpers} from '../helpers/store.helpers';
 import {ModelRest} from '../rest/model.rest';
 import {AppConfig} from '../../config/app.config';
+import {ModelHelpers} from '../../core/services/helpers/model.helper';
 
 let service: IModelStoreService;
 let httpBackend: ng.IHttpBackendService;
@@ -40,7 +41,8 @@
       .service('WebSocket', MockWs)
       .service('StoreHelpers', StoreHelpers) // TODO mock
       .service('ModelRest', ModelRest) // TODO mock
-      .service('ModelStore', ModelStore);
+      .service('ModelStore', ModelStore)
+      .service('ModelHelpers', ModelHelpers); // TODO mock
 
     angular.mock.module('ModelStore');
   });
diff --git a/src/app/datasources/stores/model.store.ts b/src/app/datasources/stores/model.store.ts
index 903bd46..015d729 100644
--- a/src/app/datasources/stores/model.store.ts
+++ b/src/app/datasources/stores/model.store.ts
@@ -4,18 +4,20 @@
 import {IWSEvent, IWSEventService} from '../websocket/global';
 import {IXosResourceService} from '../rest/model.rest';
 import {IStoreHelpersService} from '../helpers/store.helpers';
+import {IXosModelHelpersService} from '../../core/services/helpers/model.helper';
 
 export interface  IModelStoreService {
   query(model: string): Observable<any>;
 }
 
 export class ModelStore {
-  static $inject = ['WebSocket', 'StoreHelpers', 'ModelRest'];
+  static $inject = ['WebSocket', 'StoreHelpers', 'ModelRest', 'ModelHelpers'];
   private _collections: any; // NOTE contains a map of {model: BehaviourSubject}
   constructor(
     private webSocket: IWSEventService,
     private storeHelpers: IStoreHelpersService,
-    private ModelRest: IXosResourceService
+    private ModelRest: IXosResourceService,
+    private ModelHelpers: IXosModelHelpersService
   ) {
     this._collections = {};
   }
@@ -42,7 +44,7 @@
 
   private loadInitialData(model: string) {
     // NOTE check what is the correct pattern to pluralize this
-    const endpoint = `/core/${model.toLowerCase()}s`;
+    const endpoint = this.ModelHelpers.urlFromCoreModel(model);
     this.ModelRest.getResource(endpoint).query().$promise
       .then(
         res => {
diff --git a/src/index.ts b/src/index.ts
index b03f4b4..88c2250 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -28,6 +28,8 @@
 import {IXosPageTitleService} from './app/core/services/page-title';
 import {IXosConfigHelpersService} from './app/core/services/helpers/config.helpers';
 import {StyleConfig} from './app/config/style.config';
+import {IXosResourceService} from './app/datasources/rest/model.rest';
+import {IXosModelHelpersService} from './app/core/services/helpers/model.helper';
 
 export interface IXosState extends angular.ui.IState {
   data: IXosCrudData;
@@ -63,9 +65,12 @@
     $location: ng.ILocationService,
     $state: ng.ui.IStateService,
     ModelDefs: IModeldefsService,
+    ModelRest: IXosResourceService,
     RuntimeStates: IRuntimeStatesService,
     NavigationService: IXosNavigationService,
     ConfigHelpers: IXosConfigHelpersService,
+    ModelHelpers: IXosModelHelpersService,
+    toastr: ng.toastr.IToastrService,
     PageTitle: IXosPageTitleService
   ) => {
 
@@ -99,13 +104,27 @@
                     icon: 'remove',
                     color: 'red',
                     cb: (item) => {
+                      let obj = angular.copy(item);
+                      // check that item in an angular resource
+                      if (!item.$delete) {
+                        const endpoint = ModelHelpers.urlFromCoreModel(m.name);
+                        const resource = ModelRest.getResource(endpoint);
+                        // create a resource with that item
+                        item = new resource(item);
+
+                      }
                       console.log(item);
                       item.$delete()
-                        .then(res => {
-                          console.log(res);
+                        .then((res) => {
+                          if (res.status === 404) {
+                            // TODO understand why it does not go directly in catch
+                            throw new Error();
+                          }
+                          toastr.info(`${m.name} ${obj.name} succesfully deleted`);
                         })
-                        .catch(err => {
-                          console.error(err);
+                        .catch(() => {
+                          console.log(obj);
+                          toastr.error(`Error while deleting ${obj.name}`);
                         });
                     }
                   }
diff --git a/src/interceptors.ts b/src/interceptors.ts
index 9174942..dc0dcc4 100644
--- a/src/interceptors.ts
+++ b/src/interceptors.ts
@@ -12,7 +12,7 @@
 export function userStatusInterceptor($state: angular.ui.IStateService) {
 
   const checkLogin = (res) => {
-    if (res.status === 403) {
+    if (res.status === 403 || res.status === -1) {
       $state.go('login');
     }
     return res;
diff --git a/tslint.json b/tslint.json
index ed95a35..9f4f2c7 100644
--- a/tslint.json
+++ b/tslint.json
@@ -1,5 +1,6 @@
 {
   "rules": {
+    "no-console": [true],
     "ban": [true,
       ["_", "extend"],
       ["_", "isNull"],