Saving data from form
Change-Id: If78c7b7a8396a574edbc2cab3fd4150010f103b6
diff --git a/src/app/core/services/helpers/config.helpers.spec.ts b/src/app/core/services/helpers/config.helpers.spec.ts
index beec2e0..b39f444 100644
--- a/src/app/core/services/helpers/config.helpers.spec.ts
+++ b/src/app/core/services/helpers/config.helpers.spec.ts
@@ -2,8 +2,7 @@
import 'angular-mocks';
import 'angular-ui-router';
-import {IXosConfigHelpersService} from './config.helpers';
-import {xosCore} from '../../index';
+import {IXosConfigHelpersService, ConfigHelpers} from './config.helpers';
import {IModeldef} from '../../../datasources/rest/modeldefs.rest';
import {IXosTableCfg} from '../../table/table';
import {IXosFormInput, IXosFormConfig} from '../../form/form';
@@ -50,7 +49,17 @@
describe('The ConfigHelpers service', () => {
- beforeEach(angular.mock.module(xosCore));
+ beforeEach(() => {
+ angular
+ .module('test', ['toastr'])
+ .service('ConfigHelpers', ConfigHelpers)
+ .value('AuthService', {
+ getUser: () => {
+ return {id: 1};
+ }
+ });
+ angular.mock.module('test');
+ });
beforeEach(angular.mock.inject((
ConfigHelpers: IXosConfigHelpersService,
diff --git a/src/app/core/services/helpers/config.helpers.ts b/src/app/core/services/helpers/config.helpers.ts
index 6b89856..1df2662 100644
--- a/src/app/core/services/helpers/config.helpers.ts
+++ b/src/app/core/services/helpers/config.helpers.ts
@@ -3,6 +3,7 @@
import {IXosTableColumn, IXosTableCfg} from '../../table/table';
import {IModeldef} from '../../../datasources/rest/modeldefs.rest';
import {IXosFormConfig, IXosFormInput} from '../../form/form';
+import {IXosAuthService} from '../../../datasources/rest/auth.rest';
export interface IXosModelDefsField {
name: string;
@@ -44,7 +45,8 @@
];
constructor(
- private toastr: ng.toastr.IToastrService
+ private toastr: ng.toastr.IToastrService,
+ private AuthService: IXosAuthService
) {
pluralize.addIrregularRule('xos', 'xosses');
pluralize.addPluralRule(/slice$/i, 'slices');
@@ -165,18 +167,31 @@
public modelToFormCfg(model: IModeldef): IXosFormConfig {
return {
formName: `${model.name}Form`,
- exclude: ['backend_status'],
+ exclude: ['backend_status', 'creator'],
actions: [{
label: 'Save',
class: 'success',
icon: 'ok',
cb: (item, form) => {
+ const model = angular.copy(item);
+
+ // TODO remove ManyToMany relations and save them separately (how??)
+ delete item.networks;
+
+ // adding userId as creator
+ item.creator = this.AuthService.getUser().id;
+
item.$save()
- .then(res => {
- this.toastr.success(`${item.name} succesfully saved`);
+ .then((res) => {
+ if (res.status === 403 || res.status === 405 || res.status === 500) {
+ // TODO understand why 405 does not go directly in catch (it may be realted to ng-rest-gw)
+ throw new Error();
+ }
+ this.toastr.success(`${model.name} succesfully saved`);
})
.catch(err => {
- this.toastr.error(`Error while saving ${item.name}`);
+ // TODO keep the edited model
+ this.toastr.error(`Error while saving ${model.name}`);
});
}
}],
diff --git a/src/app/datasources/helpers/store.helpers.spec.ts b/src/app/datasources/helpers/store.helpers.spec.ts
index f0168b6..1793a7c 100644
--- a/src/app/datasources/helpers/store.helpers.spec.ts
+++ b/src/app/datasources/helpers/store.helpers.spec.ts
@@ -6,6 +6,7 @@
import {BehaviorSubject} from 'rxjs';
import {IWSEvent} from '../websocket/global';
import {ConfigHelpers} from '../../core/services/helpers/config.helpers';
+import {AuthService} from '../rest/auth.rest';
let service: IStoreHelpersService;
let subject: BehaviorSubject<any>;
@@ -16,10 +17,11 @@
beforeEach(() => {
angular
- .module('test', ['ngResource', 'toastr'])
+ .module('test', ['ngResource', 'toastr', 'ngCookies'])
.service('ConfigHelpers', ConfigHelpers) // NOTE evaluate mock
.service('ModelRest', ModelRest) // NOTE evaluate mock
- .service('StoreHelpers', StoreHelpers);
+ .service('StoreHelpers', StoreHelpers)
+ .service('AuthService', AuthService);
angular.mock.module('test');
});
diff --git a/src/app/datasources/rest/model.rest.ts b/src/app/datasources/rest/model.rest.ts
index 51e41d3..8bd2c1f 100644
--- a/src/app/datasources/rest/model.rest.ts
+++ b/src/app/datasources/rest/model.rest.ts
@@ -6,7 +6,6 @@
export class ModelRest implements IXosResourceService {
static $inject = ['$resource'];
- private resource: angular.resource.IResourceClass<any>;
/** @ngInject */
constructor(
@@ -16,6 +15,18 @@
}
public getResource(url: string): ng.resource.IResourceClass<ng.resource.IResource<any>> {
- return this.resource = this.$resource(`${AppConfig.apiEndpoint}${url}/:id/`, {id: '@id'});
+ const resource: angular.resource.IResourceClass<any> = this.$resource(`${AppConfig.apiEndpoint}${url}/:id/`, {id: '@id'}, {
+ update: { method: 'PUT' }
+ });
+
+ resource.prototype.$save = function() {
+ if (this.id) {
+ return this.$update();
+ } else {
+ return resource.save(this).$promise;
+ }
+ };
+
+ return resource;
}
}
diff --git a/src/app/datasources/stores/model.store.spec.ts b/src/app/datasources/stores/model.store.spec.ts
index dd19c27..5bdc498 100644
--- a/src/app/datasources/stores/model.store.spec.ts
+++ b/src/app/datasources/stores/model.store.spec.ts
@@ -8,6 +8,7 @@
import {ModelRest} from '../rest/model.rest';
import {AppConfig} from '../../config/app.config';
import {ConfigHelpers} from '../../core/services/helpers/config.helpers';
+import {AuthService} from '../rest/auth.rest';
let service: IModelStoreService;
let httpBackend: ng.IHttpBackendService;
@@ -37,12 +38,13 @@
beforeEach(() => {
angular
- .module('ModelStore', ['ngResource', 'toastr'])
+ .module('ModelStore', ['ngResource', 'toastr', 'ngCookies'])
.service('WebSocket', MockWs)
.service('StoreHelpers', StoreHelpers) // TODO mock
.service('ModelRest', ModelRest) // TODO mock
.service('ModelStore', ModelStore)
- .service('ConfigHelpers', ConfigHelpers); // TODO mock
+ .service('ConfigHelpers', ConfigHelpers) // TODO mock
+ .service('AuthService', AuthService);
angular.mock.module('ModelStore');
});
diff --git a/src/interceptors.ts b/src/interceptors.ts
index dc0dcc4..c5b5e7e 100644
--- a/src/interceptors.ts
+++ b/src/interceptors.ts
@@ -12,7 +12,8 @@
export function userStatusInterceptor($state: angular.ui.IStateService) {
const checkLogin = (res) => {
- if (res.status === 403 || res.status === -1) {
+ // NOTE why are returning 403 in place of 401??
+ if (res.status === 401 || res.status === -1) {
$state.go('login');
}
return res;