authors.controller.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. (function(){
  2. app.controller('Authors', ['$scope', 'books.repository', '$routeParams','utils', '$uibModal', function ($scope, booksRepository, $routeParams, utils, $uibModal){
  3. booksRepository.getAuthors()
  4. .then(function(respons){
  5. $scope.authors = respons.data
  6. },function(error){
  7. utils.notify({
  8. message: error.statusText,
  9. type: 'danger'
  10. })
  11. });
  12. $scope.deleteAuthor = function(id){
  13. var modal = $uibModal.open({
  14. templateUrl: 'app/modals/confirm/confirm.template.html',
  15. controller: 'Confirm',
  16. size: 'sm'
  17. })
  18. modal.result.then(function(result){
  19. if(!result) return;
  20. booksRepository.deleteAuthorById(id).then(function(respons){
  21. $scope.authors.splice($scope.authors.indexOf(id), 1);
  22. })
  23. })
  24. };
  25. $scope.user = {
  26. name: 'awesome user'
  27. };
  28. $scope.updateUser = function(){
  29. console.log($scope.user)
  30. };
  31. $scope.saveAuthor = function(data, author){
  32. if(author){
  33. booksRepository.updateAuthorsById(data, author).then(function(respons){
  34. utils.notify({
  35. message: 'Authors edite',
  36. type: 'succes'
  37. })
  38. })
  39. }else {
  40. booksRepository.addAuthor(data, author).then(function(respons){
  41. utils.notify({
  42. message: 'Authors Add',
  43. type: 'succes'
  44. })
  45. })
  46. }
  47. };
  48. $scope.addAuthors = function(){
  49. $scope.inserted = {
  50. id: 0,
  51. firstname: '',
  52. lastname: '',
  53. };
  54. $scope.authors.push($scope.inserted)
  55. }
  56. }])
  57. })();