Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to delete a file. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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');
19 changes: 17 additions & 2 deletions app/assets/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
17 changes: 16 additions & 1 deletion app/scripts/homePages.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/services.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 4 additions & 3 deletions app/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ <h1>Your files
<td>{{file.name}}</td>
<td>{{file.lastUpdate|date: 'medium'}}</td>
<td>
<a ng-href="{{urlFor(file)}}">
edit
</a>
<div class="btn-group">
<a ng-href="{{urlFor(file)}}" type="button" class="btn btn-primary btn-sm">edit</a>
<button ng-click="delete(file)" type="button" class="btn btn-danger btn-sm">delete</button>
</div>
</td>
</tr>
</tbody>
Expand Down
42 changes: 40 additions & 2 deletions test/unit/homePagesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe('Home Pages', function() {

var ctrl, scope, route;
var ctrl, scope, route, win={};

beforeEach(module('app.homePages'));

Expand All @@ -20,7 +20,8 @@ describe('Home Pages', function() {

ctrl = $controller('HomeCtrl', {
$scope: scope,
files: files
files: files,
$window: win
});
}));

Expand Down Expand Up @@ -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() {
Expand Down