Skip to content

Commit

Permalink
add batch delete queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ThieryMichel committed Mar 30, 2015
1 parent deb8f57 commit 1362092
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
36 changes: 32 additions & 4 deletions src/javascripts/ng-admin/Crud/batchDelete/BatchDeleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
});
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/batchDelete/batchDelete.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h1 compile="batchDeleteController.title">
<div class="row">
<div class="col-lg-12">
<p>Are you sure ?</p>
<button class="btn btn-danger" ng-click="batchDeleteController.delete()">Yes</button>
<button class="btn btn-danger" ng-click="batchDeleteController.batchDelete()">Yes</button>
<button class="btn btn-default" ng-click="batchDeleteController.back()">No</button>
</div>
</div>
Expand Down
18 changes: 18 additions & 0 deletions src/javascripts/ng-admin/Crud/repository/DeleteQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/javascripts/test/unit/Crud/repository/DeleteQueriesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
});

0 comments on commit 1362092

Please sign in to comment.