Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic solution #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -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("/");
});
};


}]);
29 changes: 29 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en" ng-app="superCrud">
<head>
<meta charset="UTF-8">
<base href="/">
<!-- bootstrap cdn -->
<link type="text/css" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- angular -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
<!-- angular resource -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-resource.min.js">
</script>
<!-- angular routing -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.min.js"></script>
<!-- custom script angular app -->
<script type="text/javascript" src="app.js"></script>
<title>Super Crud</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div ng-view></div>
</div>
</div>
</div>

</body>
</html>
Binary file added templates/.DS_Store
Binary file not shown.
31 changes: 31 additions & 0 deletions templates/books/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<h1>All the Books</h1>
<div class="row" ng-repeat="book in allBooks">
<div class="col-xs-4">
<img src="{{book.image}}" class="img-responsive" alt="book image">
</div>
<div class="col-xs-8">
<h3><a href="/books/{{book._id}}">{{book.title}}</a></h3>
<h4>Author: {{book.author}}</h4>
<h4>Date released: {{book.releaseDate}}</h4>
</div>
</div>
<div class="row">
<div class="col-md-12">
<form>
<h2>create new book</h2>
<div class="form-group">
Title: <input type="text" class="form-control" ng-model="newBook.title">
</div>
<div class="form-group">
Author: <input type="text" class="form-control" ng-model="newBook.author">
</div>
<div class="form-group">
Release Date: <input type="text" class="form-control" ng-model="newBook.releaseDate">
</div>
<div class="form-group">
Image Url: <input type="text" class="form-control" ng-model="newBook.image">
</div>
<input type="submit" value="Add book" ng-click="save(newBook)">
</form>
</div>
</div>
43 changes: 43 additions & 0 deletions templates/books/show.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<div class="container">
<div class="row">
<h1>{{book.title}}</h1>
</div>
<div class="row">
<div class="col-md-4">
<img src="{{book.image}}" class="img-responsive" alt="book image">
</div>
<div class="col-md-8">
<h4>Author: {{book.author}}</h4>
<h4>Release Date: {{book.releaseDate}}</h4>
<br><br>
<div class="col-xs-3 ">
<a href="/" class="btn btn-sm btn-warning">Back </a>
<button class="btn btn-sm btn-primary pull-right" ng-hide="editBookForm" ng-click="editBookForm=true"><i class="glyphicon glyphicon-edit"></i></button>
<button class="btn btn-sm btn-danger pull-right" ng-click="deleteBook()"><i class="glyphicon glyphicon-trash"></i></button>
</div>
<div ng-show="editBookForm">
<form ng-submit="updateBook()" style="width: 50%">
<div class="form-group">
<label for="title">Title</label>
<input type="text" ng-model="book.title" class="form-control">
</div>
<div class="form-group">
<label for="author">Author</label>
<input type="text" ng-model="book.author" class="form-control">
</div>
<div class="form-group">
<label for="release">Release Date</label>
<input type="text" ng-model="book.releaseDate" class="form-control">
</div>
<div class="form-group">
<label for="image">Image Url</label>
<input type="text" ng-model="book.image" class="form-control">
</div>
<div class="form-group">
<input type="submit"value="Update" class="form-control btn btn-sm btn-primary">
</div>
</form>
</div>
</div>
</div>
</div>