|
@@ -0,0 +1,64 @@
|
|
|
+(function() {
|
|
|
+'use strict';
|
|
|
+app.controller('BooksList', [
|
|
|
+'$scope', 'books.repository', '$uibModal', 'utils', '$rootScope',
|
|
|
+
|
|
|
+function($scope, booksRepository, $uibModal, utils, $rootScope) {
|
|
|
+ $scope.sortField = 'id';
|
|
|
+
|
|
|
+ var searchResult = sessionStorage.getItem('searchResult');
|
|
|
+
|
|
|
+ if (searchResult) {
|
|
|
+ searchResult = JSON.parse(searchResult);
|
|
|
+ $scope.books = searchResult;
|
|
|
+ sessionStorage.removeItem('searchResult');
|
|
|
+ } else {
|
|
|
+ booksRepository.getBooks().then(function(response) {
|
|
|
+ $scope.books = response.data;
|
|
|
+ }, function(error) {});
|
|
|
+ }
|
|
|
+
|
|
|
+ $scope.sortBy = function(field) {
|
|
|
+ $scope.sortField = $scope.sortField === field ? '-' + $scope.sortField : field;
|
|
|
+ };
|
|
|
+
|
|
|
+ $scope.deleteBook = function(bookId) {
|
|
|
+ var modalInstance = $uibModal.open({
|
|
|
+ templateUrl: 'app/modals/confirm/confirm.template.html',
|
|
|
+ controller: 'Confirm',
|
|
|
+ size: 'sm'
|
|
|
+ });
|
|
|
+
|
|
|
+ modalInstance.result.then(function(result) {
|
|
|
+ if (!result) return;
|
|
|
+ var book = $scope.books.filter(function(book) { return book.id === bookId })[0],
|
|
|
+ index = $scope.books.indexOf(book);
|
|
|
+ booksRepository.deleteBook(bookId).then(function(response) {
|
|
|
+ $scope.books.splice( index, 1 );
|
|
|
+ utils.notify({message: 'Book has been successfully deleted', type: 'success'});
|
|
|
+ }, function(error) {});
|
|
|
+ }, function() {});
|
|
|
+ };
|
|
|
+
|
|
|
+ $scope.addBook = function() {
|
|
|
+ var modalInstance = $uibModal.open({
|
|
|
+ templateUrl: 'app/modals/add-book/add-book.template.html',
|
|
|
+ controller: 'AddBook',
|
|
|
+ size: 'lg'
|
|
|
+ });
|
|
|
+
|
|
|
+ modalInstance.result.then(function(data) {
|
|
|
+ booksRepository.addBook(data).then(function(response) {
|
|
|
+ $scope.books.push(response.data);
|
|
|
+ utils.notify({message: 'Book has been successfully added', type: 'success'});
|
|
|
+ }, function(error) {});
|
|
|
+ }, function() {});
|
|
|
+ };
|
|
|
+
|
|
|
+ $scope.$on('searchEvent', function(event, data) {
|
|
|
+ $scope.books = data;
|
|
|
+ sessionStorage.removeItem('searchResult');
|
|
|
+ });
|
|
|
+
|
|
|
+}]);
|
|
|
+})();
|