Commit 10daeabb authored by Michal-MK's avatar Michal-MK
Browse files

Initial Commit for Solution

parent 4539d4c4
......@@ -14,6 +14,11 @@ pa165eshopApp.config(['$routeProvider',
when('/category/:categoryId', {templateUrl: 'partials/category_detail.html', controller: 'CategoryDetailCtrl'}).
when('/admin/products', {templateUrl: 'partials/admin_products.html', controller: 'AdminProductsCtrl'}).
when('/admin/newproduct', {templateUrl: 'partials/admin_new_product.html', controller: 'AdminNewProductCtrl'}).
when('/admin/categories', {templateUrl: 'partials/admin_categories.html', controller: 'AdminCategoriesCtrl'}).
when('/admin/newcategory', {
templateUrl: 'partials/admin_new_category.html',
controller: 'AdminNewCategoryCtrl'
}).
otherwise({redirectTo: '/shopping'});
}
]);
......@@ -210,3 +215,53 @@ eshopControllers.directive('convertToInt', function () {
};
});
/*
* Admin categories
*/
eshopControllers.controller('AdminCategoriesCtrl',
function ($scope, $rootScope, $routeParams, $http) {
//initial load of all categories
$http.get('/eshop/api/v1/categories').then(function (response) {
$scope.categories = response.data['_embedded']['categoryDTOList'];
});
});
/*
* Page with form for creating new category
*/
eshopControllers.controller('AdminNewCategoryCtrl', function ($scope, $routeParams, $http, $location, $rootScope) {
// Set object bound to form fields
$scope.category = {
'name': ''
};
// Function called when submit button is clicked, creates category on server
$scope.create = function (category) {
$http({
method: 'POST',
url: '/eshop/api/v1/categories/create',
data: category
}).then(function success(response) {
const createdCategory = response.data;
// Display confirmation alert
$rootScope.successAlert = 'A new category "' + createdCategory.name + '" was created';
// Change view to list of categories
$location.path("/admin/categories");
}, function error(response) {
// Display error
console.log("error when creating category");
console.log(response);
switch (response.data.code) {
case 'PersistenceException':
$rootScope.errorAlert = 'Category with the same name already exists ! ';
break;
case 'InvalidRequestException':
$rootScope.errorAlert = 'Sent data were found to be invalid by server ! ';
break;
default:
$rootScope.errorAlert = 'Cannot create category ! Reason given by the server: ' + response.data.message;
break;
}
});
};
});
<h1>Administration of categories</h1>
<!-- button for adding a new category -->
<a href="#!/admin/newcategory" class="btn btn-primary">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
New category
</a>
<table class="table">
<thead>
<tr>
<th>id</th>
<th>category name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="category in categories track by $index">
<td>{{category.id}}</td>
<td>{{category.name}}</td>
</tr>
</tbody>
</table>
<form name="form" novalidate class="form-horizontal">
<div class="form-group" ng-class="{'has-error': !(form.name.$valid)}">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input placeholder="name" id="name" type="text" ng-minlength="3" name="name" class="form-control"
ng-model="category.name" required/>
<p class="help-block" ng-show="form.name.$error.required">name is required</p>
<p class="help-block" ng-show="form.name.$error.minlength">name must be at least 3 characters</p>
</div>
</div>
<!-- show disabled button if form is not valid -->
<button ng-disabled="!form.$valid" ng-click="create(category)" class="btn btn-primary" type="submit" >Create category</button>
</form>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment