diff --git a/src/javascripts/ng-admin/Crud/batchDelete/BatchDeleteController.js b/src/javascripts/ng-admin/Crud/batchDelete/BatchDeleteController.js index 0d265722..accad556 100644 --- a/src/javascripts/ng-admin/Crud/batchDelete/BatchDeleteController.js +++ b/src/javascripts/ng-admin/Crud/batchDelete/BatchDeleteController.js @@ -3,14 +3,19 @@ define(function () { 'use strict'; - var BatchDeleteController = function ($scope, $stateParams, $filter, $anchorScroll, progression, view) { + var BatchDeleteController = function ($scope, $stateParams, $filter, $location, DeleteQueries, notification, view) { this.$scope = $scope; this.$stateParams = $stateParams; this.$filter = $filter; - this.progression = progression; + this.$location = $location; + this.DeleteQueries = DeleteQueries; + this.notification = notification; this.view = view; this.entity = view.getEntity(); this.selection = $stateParams.selection; + this.entityIds = this.selection.map(function (entry) { + return entry.identifierValue; + }); this.title = view.title(); this.description = view.description(); this.actions = view.actions(); @@ -20,14 +25,37 @@ define(function () { $scope.$on('$destroy', this.destroy.bind(this)); }; + BatchDeleteController.prototype.batchDelete = function () { + var notification = this.notification, + $location = this.$location, + entityName = this.entity.name(); + + this.DeleteQueries.batchDelete(this.view, this.entityIds).then(function () { + $location.path('/list/' + entityName); + }, function (response) { + // @TODO: share this method when splitting controllers + var body = response.data; + if (typeof body === 'object') { + body = JSON.stringify(body); + } + + notification.log('Oops, an error occured : (code: ' + response.status + ') ' + body, {addnCls: 'humane-flatty-error'}); + }); + }; + + BatchDeleteController.prototype.back = function () { + this.$location.path('/list/' + this.entity.name()); + }; + BatchDeleteController.prototype.destroy = function () { this.$scope = undefined; this.$stateParams = undefined; this.$filter = undefined; - this.$anchorScroll = undefined; + this.$location = undefined; + this.DeleteQueries = undefined; }; - BatchDeleteController.$inject = ['$scope', '$stateParams', '$filter', '$anchorScroll', 'progression', 'view']; + BatchDeleteController.$inject = ['$scope', '$stateParams', '$filter', '$location', 'DeleteQueries', 'notification', 'view']; return BatchDeleteController; }); diff --git a/src/javascripts/ng-admin/Crud/batchDelete/batchDelete.html b/src/javascripts/ng-admin/Crud/batchDelete/batchDelete.html index 97b24ba8..7ae5664a 100644 --- a/src/javascripts/ng-admin/Crud/batchDelete/batchDelete.html +++ b/src/javascripts/ng-admin/Crud/batchDelete/batchDelete.html @@ -16,7 +16,7 @@

Are you sure ?

- +
diff --git a/src/javascripts/ng-admin/Crud/repository/DeleteQueries.js b/src/javascripts/ng-admin/Crud/repository/DeleteQueries.js index 1e1eddb1..164d8716 100644 --- a/src/javascripts/ng-admin/Crud/repository/DeleteQueries.js +++ b/src/javascripts/ng-admin/Crud/repository/DeleteQueries.js @@ -29,6 +29,24 @@ define(function (require) { .customDELETE(); }; + /** + * Delete a batch of entity + * Delete the data to the API + * + * @param {String} view the formView related to the entity + * @param {*} entityIds the entities ids + * + * @returns {promise} + */ + DeleteQueries.prototype.batchDelete = function (view, entityIds) { + var self = this; + var promises = entityIds.map(function (id) { + return self.deleteOne(view, id); + }); + + return this.$q.all(promises); + }; + DeleteQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration']; return DeleteQueries; diff --git a/src/javascripts/test/unit/Crud/repository/DeleteQueriesSpec.js b/src/javascripts/test/unit/Crud/repository/DeleteQueriesSpec.js index 0cc8b209..45672598 100644 --- a/src/javascripts/test/unit/Crud/repository/DeleteQueriesSpec.js +++ b/src/javascripts/test/unit/Crud/repository/DeleteQueriesSpec.js @@ -48,5 +48,22 @@ define(function (require) { .then(done, done.fail); }); }); + + describe("batchDelete", function () { + it('should DELETE entities when calling batchEntities', function () { + var deleteQueries = new DeleteQueries({all: function (promises) { + return promises; + }}, Restangular, config); + spyOn(Restangular, 'oneUrl').and.callThrough(); + spyOn(Restangular, 'customDELETE').and.callThrough(); + + var promises = deleteQueries.batchDelete(view, [1, 2]); + expect(promises.length).toBe(2); + expect(Restangular.oneUrl).toHaveBeenCalledWith('cat', 'http://localhost/cat/1'); + expect(Restangular.oneUrl).toHaveBeenCalledWith('cat', 'http://localhost/cat/2'); + expect(Restangular.customDELETE).toHaveBeenCalledWith(); + expect(Restangular.customDELETE).toHaveBeenCalledWith(); + }); + }); }); });