authors.controller.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. (function() {
  2. 'use strict';
  3. app.controller('Authors', ['$scope', 'books.repository', '$uibModal', 'utils', '$timeout', function($scope, booksRepository, $uibModal, utils, $timeout) {
  4. $scope.authors = [];
  5. booksRepository.getAuthors().then(function(response) {
  6. $scope.authors = response.data;
  7. }, function(error) {
  8. utils.notify({message: error.data.message, type: 'danger'});
  9. });
  10. $scope.deleteAuthor = function(authorId) {
  11. var modalInstance = $uibModal.open({
  12. templateUrl: 'app/modals/confirm/add-book.template.html',
  13. controller: 'Confirm',
  14. size: 'sm'
  15. });
  16. modalInstance.result.then(function(result) {
  17. if (!result) return;
  18. var author = $scope.authors.filter(function(author) { return author.id === authorId })[0],
  19. index = $scope.authors.indexOf(author);
  20. booksRepository.deleteAuthor(authorId).then(function(response) {
  21. $scope.authors.splice( index, 1 );
  22. utils.notify({message: 'Author has been successfully deleted', type: 'success'});
  23. }, function(error) {});
  24. }, function() {});
  25. };
  26. $scope.addAuthor = function() {
  27. $scope.inserted = {
  28. id: 0,
  29. firstname: 'Вася',
  30. lastname: 'Пупкин'
  31. };
  32. $scope.authors.push($scope.inserted);
  33. };
  34. $scope.saveAuthor = function(data, authorId) {
  35. if (authorId) {
  36. booksRepository.updateAuthorById(authorId, data).then(function(response) {
  37. utils.notify({message: 'Author successfully updated', type: 'success'});
  38. }, function(error) {});
  39. } else {
  40. booksRepository.addAuthor(data).then(function(response) {
  41. $scope.authors[$scope.authors.length - 1] = response.data;
  42. utils.notify({message: 'Author successfully added', type: 'success'});
  43. }, function(error) {});
  44. }
  45. };
  46. $scope.deleteFromArray = function(author, editableForm) {
  47. if (!author.id) {
  48. $scope.authors.splice($scope.authors.indexOf(author), 1);
  49. editableForm.$removeEditable();
  50. } else {
  51. editableForm.$cancel();
  52. }
  53. };
  54. }]);
  55. })();