diff --git a/app.js b/app.js new file mode 100644 index 0000000..55977db --- /dev/null +++ b/app.js @@ -0,0 +1,66 @@ +var app = angular.module("superCrud", ["ngRoute", "ngResource"]); + +app.config(["$routeProvider", "$locationProvider", function($routeProvider, $locationProvider) { + $routeProvider + .when("/", { + templateUrl: "templates/books/index.html", + controller: "bookIndexCtrl" + }) + .when("/books/:id", { + templateUrl: "templates/books/show.html", + controller: "bookShowCtrl" + }); + $locationProvider + .html5Mode({ + enabled: true, + requireBase: false + }); +}]); + +//api call here +app.factory("Book", ["$resource", function($resource) { +return $resource("https://super-crud.herokuapp.com/books/:id", {id: "@id"}, { + query: { + isArray: true, + transformResponse: function(data) { return angular.fromJson(data).books; } + }, + update: {method: "PUT"} + }); +}]); + + +app.controller("bookIndexCtrl", ["$scope", "Book", function($scope, Book) { + $scope.allBooks = Book.query(); + + $scope.newBook = new Book(); + $scope.save = function(book) { + book.$save(function() { + $scope.newBook = new Book(); + $scope.allBooks.push($scope.newBook); + }); + }; + +}]); +app.controller("bookShowCtrl",["$scope", "$routeParams", "$location", "Book", function($scope, $routeParams, $location, Book) { + var bookId = $routeParams.id; + book = Book.get({id: bookId}, + function(data) { + $scope.book = data; + }, + function(error) { + console.log(error.statusText); + $location.path("/"); + }); + $scope.deleteBook = function() { + Book.delete({id: bookId}, function(data) { + $location.path("/"); + }); + }; + $scope.updateBook = function() { + Book.update({id: bookId}, $scope.book, function(data) { + $location.path("/"); + }); + }; + + +}]); \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..b5c4689 --- /dev/null +++ b/index.html @@ -0,0 +1,29 @@ + + +
+ +