Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Added an Admin feature to Articles. This change removes the ability of
Browse files Browse the repository at this point in the history
non-Admin user's to create & edit Articles. The Article list views have
been split into two views; one for Admin & non-Admin users (including
non-Authenticated users).

Added tests for admin feature for Articles. Removed unnecessary tests
for non Admin users when interacting with Articles.
  • Loading branch information
mleanos committed Aug 20, 2015
1 parent 72067fd commit 8ebf23c
Show file tree
Hide file tree
Showing 17 changed files with 656 additions and 323 deletions.
2 changes: 2 additions & 0 deletions modules/articles/client/articles.client.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

// Use Applicaion configuration module to register a new module
ApplicationConfiguration.registerModule('articles');
ApplicationConfiguration.registerModule('articles.admin', ['core.admin']);
ApplicationConfiguration.registerModule('articles.admin.routes', ['core.admin.routes']);
11 changes: 11 additions & 0 deletions modules/articles/client/config/articles-admin.client.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

// Configuring the Articles module
angular.module('articles.admin').run(['Menus',
function (Menus) {
Menus.addSubMenuItem('topbar', 'admin', {
title: 'Manage Articles',
state: 'admin.articles.list'
});
}
]);
37 changes: 37 additions & 0 deletions modules/articles/client/config/articles-admin.client.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

// Setting up route
angular.module('articles.admin.routes').config(['$stateProvider',
function ($stateProvider) {
$stateProvider
.state('admin.articles', {
abstract: true,
url: '/articles',
template: '<ui-view/>'
})
.state('admin.articles.list', {
url: '',
templateUrl: 'modules/articles/client/views/admin/list-articles.client.view.html',
controller: 'ArticlesAdminController',
data: {
roles: ['admin']
}
})
.state('admin.articles.create', {
url: '/create',
templateUrl: 'modules/articles/client/views/admin/create-article.client.view.html',
controller: 'ArticlesAdminController',
data: {
roles: ['admin']
}
})
.state('admin.articles.edit', {
url: '/:articleId/edit',
templateUrl: 'modules/articles/client/views/admin/edit-article.client.view.html',
controller: 'ArticlesAdminController',
data: {
roles: ['admin']
}
});
}
]);
10 changes: 2 additions & 8 deletions modules/articles/client/config/articles.client.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ angular.module('articles').run(['Menus',
// Add the dropdown list item
Menus.addSubMenuItem('topbar', 'articles', {
title: 'List Articles',
state: 'articles.list'
});

// Add the dropdown create item
Menus.addSubMenuItem('topbar', 'articles', {
title: 'Create Articles',
state: 'articles.create',
roles: ['user']
state: 'articles.list',
roles: ['*']
});
}
]);
40 changes: 13 additions & 27 deletions modules/articles/client/config/articles.client.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,18 @@ angular.module('articles').config(['$stateProvider',
function ($stateProvider) {
// Articles state routing
$stateProvider
.state('articles', {
abstract: true,
url: '/articles',
template: '<ui-view/>'
})
.state('articles.list', {
url: '',
templateUrl: 'modules/articles/client/views/list-articles.client.view.html'
})
.state('articles.create', {
url: '/create',
templateUrl: 'modules/articles/client/views/create-article.client.view.html',
data: {
roles: ['user', 'admin']
}
})
.state('articles.view', {
url: '/:articleId',
templateUrl: 'modules/articles/client/views/view-article.client.view.html'
})
.state('articles.edit', {
url: '/:articleId/edit',
templateUrl: 'modules/articles/client/views/edit-article.client.view.html',
data: {
roles: ['user', 'admin']
}
});
.state('articles', {
abstract: true,
url: '/articles',
template: '<ui-view/>'
})
.state('articles.list', {
url: '',
templateUrl: 'modules/articles/client/views/list-articles.client.view.html'
})
.state('articles.view', {
url: '/:articleId',
templateUrl: 'modules/articles/client/views/view-article.client.view.html'
});
}
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';

// Articles controller
angular.module('articles.admin').controller('ArticlesAdminController', ['$scope', '$stateParams', '$state', 'Authentication', 'Articles',
function ($scope, $stateParams, $state, Authentication, Articles) {
$scope.authentication = Authentication;

// Create new Article
$scope.create = function () {
// Create new Article object
var article = new Articles({
title: this.title,
content: this.content
});

// Redirect after save
article.$save(function (response) {
$state.go('admin.articles.list');

// Clear form fields
$scope.title = '';
$scope.content = '';
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
};

// Remove existing Article
$scope.remove = function (article) {
if (confirm('Are you sure you want to delete this article?')) {
if (article) {
article.$remove();

for (var i in $scope.articles) {
if ($scope.articles[i] === article) {
$scope.articles.splice(i, 1);
}
}
} else {
$scope.article.$remove(function () {
$state.go('admin.articles.list');
});
}
}
};

// Update existing Article
$scope.update = function () {
var article = $scope.article;

article.$update(function () {
$state.go('admin.articles.list');
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
};

// Find a list of Articles
$scope.find = function () {
$scope.articles = Articles.query();
};

// Find existing Article
$scope.findOne = function () {
$scope.article = Articles.get({
articleId: $stateParams.articleId
});
};
}
]);
52 changes: 2 additions & 50 deletions modules/articles/client/controllers/articles.client.controller.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,10 @@
'use strict';

// Articles controller
angular.module('articles').controller('ArticlesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Articles',
function ($scope, $stateParams, $location, Authentication, Articles) {
angular.module('articles').controller('ArticlesController', ['$scope', '$stateParams', 'Authentication', 'Articles',
function ($scope, $stateParams, Authentication, Articles) {
$scope.authentication = Authentication;

// Create new Article
$scope.create = function () {
// Create new Article object
var article = new Articles({
title: this.title,
content: this.content
});

// Redirect after save
article.$save(function (response) {
$location.path('articles/' + response._id);

// Clear form fields
$scope.title = '';
$scope.content = '';
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
};

// Remove existing Article
$scope.remove = function (article) {
if (article) {
article.$remove();

for (var i in $scope.articles) {
if ($scope.articles[i] === article) {
$scope.articles.splice(i, 1);
}
}
} else {
$scope.article.$remove(function () {
$location.path('articles');
});
}
};

// Update existing Article
$scope.update = function () {
var article = $scope.article;

article.$update(function () {
$location.path('articles/' + article._id);
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
};

// Find a list of Articles
$scope.find = function () {
$scope.articles = Articles.query();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<section data-ng-controller="ArticlesAdminController">
<div class="page-header">
<h1>New Article</h1>
</div>
<div class="col-md-12">
<form name="articleForm" class="form-horizontal" data-ng-submit="create()" novalidate>
<fieldset>
<div class="form-group">
<label class="control-label" for="title">Title</label>
<div class="controls">
<input name="title" type="text" data-ng-model="title" id="title" class="form-control" placeholder="Title">
</div>
</div>
<div class="form-group">
<label class="control-label" for="content">Content</label>
<div class="controls">
<textarea name="content" data-ng-model="content" id="content" class="form-control" cols="30" rows="10" placeholder="Content"></textarea>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-default">
</div>
<div data-ng-show="error" class="text-danger">
<strong data-ng-bind="error"></strong>
</div>
</fieldset>
</form>
</div>
</section>
34 changes: 34 additions & 0 deletions modules/articles/client/views/admin/edit-article.client.view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<section data-ng-controller="ArticlesAdminController" data-ng-init="findOne()">
<div class="page-header">
<h1>
Edit Article
<a class="btn btn-primary pull-right" data-ng-click="remove();">
<i class="glyphicon glyphicon-trash"></i>
</a>
</h1>
</div>
<div class="col-md-12">
<form name="articleForm" class="form-horizontal" data-ng-submit="update()" novalidate>
<fieldset>
<div class="form-group">
<label class="control-label" for="title">Title</label>
<div class="controls">
<input name="title" type="text" data-ng-model="article.title" id="title" class="form-control" placeholder="Title" required>
</div>
</div>
<div class="form-group">
<label class="control-label" for="content">Content</label>
<div class="controls">
<textarea name="content" data-ng-model="article.content" id="content" class="form-control" cols="30" rows="10" placeholder="Content"></textarea>
</div>
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-default">
</div>
<div data-ng-show="error" class="text-danger">
<strong data-ng-bind="error"></strong>
</div>
</fieldset>
</form>
</div>
</section>
25 changes: 25 additions & 0 deletions modules/articles/client/views/admin/list-articles.client.view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<section data-ng-controller="ArticlesAdminController" data-ng-init="find()">
<div class="page-header">
<h1>
Articles
<a class="btn btn-primary pull-right" data-ui-sref="admin.articles.create">
<i class="glyphicon glyphicon-plus"></i>
</a>
</h1>
</div>
<div class="list-group">
<a data-ng-repeat="article in articles" data-ui-sref="admin.articles.edit({articleId: article._id})" class="list-group-item">
<small class="list-group-item-text">
Posted on
<span data-ng-bind="article.created | date:'mediumDate'"></span>
by
<span data-ng-bind="article.user.displayName"></span>
</small>
<h4 class="list-group-item-heading" data-ng-bind="article.title"></h4>
<p class="list-group-item-text" data-ng-bind="article.content"></p>
</a>
</div>
<div class="alert alert-warning text-center" data-ng-if="articles.$resolved && !articles.length">
No articles yet, why don't you <a data-ui-sref="admin.articles.create">create one</a>?
</div>
</section>
3 changes: 0 additions & 3 deletions modules/articles/client/views/list-articles.client.view.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ <h4 class="list-group-item-heading" data-ng-bind="article.title"></h4>
<p class="list-group-item-text" data-ng-bind="article.content"></p>
</a>
</div>
<div class="alert alert-warning text-center" data-ng-if="articles.$resolved && !articles.length">
No articles yet, why don't you <a data-ui-sref="articles.create">create one</a>?
</div>
</section>
8 changes: 0 additions & 8 deletions modules/articles/client/views/view-article.client.view.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
<div class="page-header">
<h1 data-ng-bind="article.title"></h1>
</div>
<div class="pull-right" data-ng-show="authentication.user._id == article.user._id">
<a class="btn btn-primary" data-ui-sref="articles.edit({articleId: article._id})">
<i class="glyphicon glyphicon-edit"></i>
</a>
<a class="btn btn-primary" data-ng-click="remove();">
<i class="glyphicon glyphicon-trash"></i>
</a>
</div>
<small>
<em class="text-muted">
Posted on
Expand Down
2 changes: 1 addition & 1 deletion modules/articles/server/policies/articles.server.policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports.invokeRolesPolicies = function () {
roles: ['user'],
allows: [{
resources: '/api/articles',
permissions: ['get', 'post']
permissions: ['get']
}, {
resources: '/api/articles/:articleId',
permissions: ['get']
Expand Down
Loading

0 comments on commit 8ebf23c

Please sign in to comment.