|
@@ -1,16 +1,60 @@
|
|
|
-(function ()
|
|
|
-{
|
|
|
- 'use strict';
|
|
|
-
|
|
|
- app.controller('BookDetails', ['$scope', 'books.repository', '$routeParams', function ($scope, booksRepository, $routeParams)
|
|
|
- {
|
|
|
- booksRepository.getBookById($routeParams.id)
|
|
|
- .then(function(response) {
|
|
|
+(function() {
|
|
|
+'use strict';
|
|
|
+app.controller('BookDetails', ['$scope', '$routeParams', 'books.repository', function($scope, $routeParams, booksRepository) {
|
|
|
+ var bookId = $routeParams.id,
|
|
|
+ bookModel = {};
|
|
|
+ $scope.isEditMode = false;
|
|
|
+
|
|
|
+ $scope.datepicker = {
|
|
|
+ format: 'yyyy-MM-dd',
|
|
|
+ opened: false,
|
|
|
+ options: {}
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ booksRepository.getBookById($routeParams.id).then(function(response) {
|
|
|
console.log(response.data);
|
|
|
- }, function(error) {
|
|
|
- alert(error)}
|
|
|
- )
|
|
|
+ }, function(error) {alert(error)});
|
|
|
+
|
|
|
+ booksRepository.getBookById(bookId).then(function(response) {
|
|
|
+ $scope.book = response.data;
|
|
|
+ $scope.book.date = new Date($scope.book.date);
|
|
|
+ }, function(error) {alert(error)});
|
|
|
+
|
|
|
+ booksRepository.getAuthors().then(function(response) {
|
|
|
+ $scope.authors = response.data.map(function(item) {
|
|
|
+ return {
|
|
|
+ id: item.id,
|
|
|
+ name: item.firstname + ' ' + item.lastname
|
|
|
}
|
|
|
- ]);
|
|
|
-}
|
|
|
-)();
|
|
|
+ });
|
|
|
+ }, function(error) {alert(error)});
|
|
|
+
|
|
|
+ $scope.getAuthorNameById = function(model, id) {
|
|
|
+ if (!model || !id) return;
|
|
|
+ return model.filter(function(item) {
|
|
|
+ return item.id === id;
|
|
|
+ console.log(item.id);
|
|
|
+ })[0].name;
|
|
|
+ };
|
|
|
+
|
|
|
+ $scope.setEditMode = function() {
|
|
|
+ $scope.isEditMode = true;
|
|
|
+ bookModel = angular.copy($scope.book);
|
|
|
+ };
|
|
|
+
|
|
|
+ $scope.updateBook = function() {
|
|
|
+ booksRepository.updateBookById($scope.book.id, $scope.book).then(function(response) {
|
|
|
+ if (response.data.errors.length) return;
|
|
|
+ $scope.isEditMode = false;
|
|
|
+ }, function(error) {});
|
|
|
+ };
|
|
|
+
|
|
|
+ $scope.cancel = function() {
|
|
|
+ $scope.isEditMode = false;
|
|
|
+ $scope.book = angular.copy(bookModel);
|
|
|
+ };
|
|
|
+}]);
|
|
|
+})();
|
|
|
+
|
|
|
+
|