Skip to content

Commit

Permalink
Article route / controller refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
trainerbill committed Aug 13, 2015
1 parent 820355c commit dced51b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 43 deletions.
11 changes: 8 additions & 3 deletions modules/articles/client/config/articles.client.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,31 @@ angular.module('articles').config(['$stateProvider',
.state('articles', {
abstract: true,
url: '/articles',
template: '<ui-view/>'
template: '<ui-view/>',
controller: 'ArticlesController'
})
.state('articles.list', {
url: '',
templateUrl: 'modules/articles/views/list-articles.client.view.html'
templateUrl: 'modules/articles/views/list-articles.client.view.html',
controller: 'ArticlesListController'
})
.state('articles.create', {
url: '/create',
templateUrl: 'modules/articles/views/create-article.client.view.html',
controller: 'ArticlesCreateController',
data: {
roles: ['user', 'admin']
}
})
.state('articles.view', {
url: '/:articleId',
templateUrl: 'modules/articles/views/view-article.client.view.html'
templateUrl: 'modules/articles/views/view-article.client.view.html',
controller: 'ArticlesViewController'
})
.state('articles.edit', {
url: '/:articleId/edit',
templateUrl: 'modules/articles/views/edit-article.client.view.html',
controller: 'ArticlesEditController',
data: {
roles: ['user', 'admin']
}
Expand Down
110 changes: 74 additions & 36 deletions modules/articles/client/controllers/articles.client.controller.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,58 @@
'use strict';

// Articles controller
angular.module('articles').controller('ArticlesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Articles',
function ($scope, $stateParams, $location, Authentication, Articles) {
$scope.authentication = Authentication;
angular.module('articles')
.controller('ArticlesListController', ['$scope', '$state', 'Articles',
function ($scope, $stateParams, Articles) {
//Query Articles
$scope.articles = Articles.query();
}
])
.controller('ArticlesViewController', ['$scope', '$state', '$stateParams', 'Articles',
function ($scope, $state, $stateParams, Articles) {
//Check if article is loaded if not load it
if ($scope.article === undefined || typeof $scope.article !== 'object' || $scope.article._id === undefined || $scope.article._id !== $stateParams.articleId) {
$scope.loadArticleById($stateParams.articleId);
}

// Remove existing Article
$scope.remove = function () {
$scope.authCheck();
if (confirm('Are you sure you want to delete?')) {
$scope.article.$remove(function (article) {
if ($scope.articles !== undefined && $scope.articles.length > 0) {
$scope.articles.splice($scope.articles.indexOf($scope.article), 1);
}
$state.go('articles.list');
});
}
};
}
])
.controller('ArticlesEditController', ['$scope', '$state', '$stateParams', 'Authentication', 'Articles',
function ($scope, $state, $stateParams, Authentication, Articles) {

//Check if article is loaded if not load it
if ($scope.article === undefined || typeof $scope.article !== 'object' || $scope.article._id === undefined || $scope.article._id !== $stateParams.articleId) {
$scope.loadArticleById($stateParams.articleId);
}

//Auth Check on Edit Route
$scope.authCheck();

$scope.update = function () {
var article = $scope.article;

article.$update(function () {
$state.go('articles.view',{articleId: article._id});
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
};
}
])
.controller('ArticlesCreateController', ['$scope', '$state', '$stateParams', 'Authentication', 'Articles',
function ($scope, $state, $stateParams, Authentication, Articles) {
// Create new Article
$scope.create = function () {
// Create new Article object
Expand All @@ -15,7 +63,7 @@ angular.module('articles').controller('ArticlesController', ['$scope', '$statePa

// Redirect after save
article.$save(function (response) {
$location.path('articles/' + response._id);
$state.go('articles.list', {articleId: response._id});

// Clear form fields
$scope.title = '';
Expand All @@ -24,45 +72,35 @@ angular.module('articles').controller('ArticlesController', ['$scope', '$statePa
$scope.error = errorResponse.data.message;
});
};
}
])
.controller('ArticlesController', ['$scope', '$stateParams', '$state', 'Authentication', 'Articles',
function ($scope, $stateParams, $state, Authentication, Articles) {

// Remove existing Article
$scope.remove = function (article) {
if (article) {
article.$remove();
$scope.loadArticleById = function (id) {
$scope.article = Articles.get({
articleId: id
});

for (var i in $scope.articles) {
if ($scope.articles[i] === article) {
$scope.articles.splice(i, 1);
}
$scope.article.$promise.then(function (article) {
if (article === undefined || typeof article !== 'object' || article._id === undefined) {
//Article not found
$state.go('not-found');
}
} 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();
};

// Find existing Article
$scope.findOne = function () {
$scope.article = Articles.get({
articleId: $stateParams.articleId
$scope.authCheck = function () {
$scope.article.$promise.then(function (article) {
//Make sure user is authorized if not on the view state
if ($scope.article.user._id !== Authentication.user._id) {
//TODO Change to unauthorized when that PR is merged
$state.go('articles.list');
}
});
};

//Not sure if this is needed anymore?
$scope.authentication = Authentication;
}
]);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<section data-ng-controller="ArticlesController">
<section>
<div class="page-header">
<h1>New Article</h1>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<section data-ng-controller="ArticlesController" data-ng-init="findOne()">
<section>
<div class="page-header">
<h1>Edit Article</h1>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<section data-ng-controller="ArticlesController" data-ng-init="find()">
<section>
<div class="page-header">
<h1>Articles</h1>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<section data-ng-controller="ArticlesController" data-ng-init="findOne()">
<section>
<div class="page-header">
<h1 data-ng-bind="article.title"></h1>
</div>
Expand Down

0 comments on commit dced51b

Please sign in to comment.