From 552857c8e477d403d257e1e41f1cf2d00b811734 Mon Sep 17 00:00:00 2001 From: Damien Lebrun Date: Mon, 13 Jan 2014 18:01:25 +0000 Subject: [PATCH] Allow users to delete a file. --- api/server.js | 23 +++++++++++++++++++++ app/assets/app.js | 19 +++++++++++++++-- app/scripts/homePages.js | 17 ++++++++++++++- app/scripts/services.js | 2 +- app/templates/home.html | 7 ++++--- test/unit/homePagesSpec.js | 42 ++++++++++++++++++++++++++++++++++++-- 6 files changed, 101 insertions(+), 9 deletions(-) diff --git a/api/server.js b/api/server.js index a21273b..fbd9687 100644 --- a/api/server.js +++ b/api/server.js @@ -68,6 +68,10 @@ app.get('/file', function(req, res) { return -f.lastModification; }); + files = _.remove(files, function(e){ + return e; + }); + if (files.length > 50) { files = files.slice(0, 50); } @@ -241,6 +245,25 @@ app.post('/file/:key(\\d+)', function(req, res) { }, DELAY); }); +/** + * Delete the file. + * + */ +app.delete('/file/:key(\\d+)', function(req, res) { + var key = decodeURIComponent(req.params.key); + + if (!FILE_DB[key]) { + return res.send(404, 'Sorry, we cannot find that!'); + } + + FILE_DB[key] = false; + + setTimeout(function() { + res.send({}); + }, DELAY); + +}); + app.listen(9090); console.log('Listening on port 9090'); diff --git a/app/assets/app.js b/app/assets/app.js index 2882e5b..10fb1d3 100644 --- a/app/assets/app.js +++ b/app/assets/app.js @@ -41055,7 +41055,7 @@ var JSEncrypt = JSEncryptExports.JSEncrypt; ;angular.module('app.services', ['app.config']). factory('files', function(API_BASE, $resource, $http){ - var res = $resource(API_BASE + '/file/:key'); + var res = $resource(API_BASE + '/file/:key', {key:'@key'}); return { all: function() { return res.query().$promise; @@ -41076,13 +41076,28 @@ var JSEncrypt = JSEncryptExports.JSEncrypt; ;;angular.module('app.homePages', ['app.config', 'app.services', 'ngResource', 'ngAnimate', 'angularSpinkit']). - controller('HomeCtrl', function($scope, files) { + controller('HomeCtrl', function($scope, $window, files) { $scope.files = []; $scope.loading = true; $scope.urlFor = function(file) { + if (!file || $window._.isUndefined(file.key)) { + return; + } + return '/#/file/' + encodeURIComponent(file.key); }; + + $scope.delete = function(file) { + var index; + if ($window.confirm("Are you sure you want to delete " + file.name)) { + index = $scope.files.indexOf(file); + file.$delete().then(function(){ + $scope.files.splice(index, index+1); + }); + } + + }; files.all().then(function(data) { $scope.loading = false; diff --git a/app/scripts/homePages.js b/app/scripts/homePages.js index 10008e4..76d8c89 100644 --- a/app/scripts/homePages.js +++ b/app/scripts/homePages.js @@ -1,12 +1,27 @@ angular.module('app.homePages', ['app.config', 'app.services', 'ngResource', 'ngAnimate', 'angularSpinkit']). - controller('HomeCtrl', function($scope, files) { + controller('HomeCtrl', function($scope, $window, files) { $scope.files = []; $scope.loading = true; $scope.urlFor = function(file) { + if (!file || $window._.isUndefined(file.key)) { + return; + } + return '/#/file/' + encodeURIComponent(file.key); }; + + $scope.delete = function(file) { + var index; + if ($window.confirm("Are you sure you want to delete " + file.name)) { + index = $scope.files.indexOf(file); + file.$delete().then(function(){ + $scope.files.splice(index, index+1); + }); + } + + }; files.all().then(function(data) { $scope.loading = false; diff --git a/app/scripts/services.js b/app/scripts/services.js index 4853133..eeba281 100644 --- a/app/scripts/services.js +++ b/app/scripts/services.js @@ -1,7 +1,7 @@ angular.module('app.services', ['app.config']). factory('files', function(API_BASE, $resource, $http){ - var res = $resource(API_BASE + '/file/:key'); + var res = $resource(API_BASE + '/file/:key', {key:'@key'}); return { all: function() { return res.query().$promise; diff --git a/app/templates/home.html b/app/templates/home.html index 7fa9768..3a45b70 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -25,9 +25,10 @@

Your files {{file.name}} {{file.lastUpdate|date: 'medium'}} - - edit - +
+ edit + +
diff --git a/test/unit/homePagesSpec.js b/test/unit/homePagesSpec.js index e565bb6..74d6cc1 100644 --- a/test/unit/homePagesSpec.js +++ b/test/unit/homePagesSpec.js @@ -2,7 +2,7 @@ describe('Home Pages', function() { - var ctrl, scope, route; + var ctrl, scope, route, win={}; beforeEach(module('app.homePages')); @@ -20,7 +20,8 @@ describe('Home Pages', function() { ctrl = $controller('HomeCtrl', { $scope: scope, - files: files + files: files, + $window: win }); })); @@ -49,6 +50,43 @@ describe('Home Pages', function() { }); + describe('scope delete method ', function(){ + beforeEach(function(){ + win.confirm = function() { + return true; + }; + }); + + it('should called the file resource $delete method', inject(function($q) { + var called=false, mockFile = { + $delete: function(){ + var d = $q.defer(); + called = true; + d.resolve({}); + return d.promise; + } + }; + + scope.delete(mockFile); + expect(called).toBe(true); + })); + + it('should remove the file for the files list', inject(function($q) { + var mockFile = { + $delete: function(){ + var d = $q.defer(); + d.resolve({}); + return d.promise; + } + }; + scope.files = [mockFile]; + scope.delete(mockFile); + + scope.$apply(); + expect(scope.files).toEqual([]); + })); + }); + }); describe('Menu Controller', function() {