slavailchenko35 6 năm trước cách đây
mục cha
commit
89fb2c926d

+ 26 - 1
app/app.config.js

@@ -2,4 +2,29 @@ app.run(function($rootScope, $location) {
 	$rootScope.$on('$routeChangeSuccess', function() {
 		$rootScope.currentMenuItem = $location.path() || '/';
 	});
-});
+});
+
+
+// check authorization
+app.config(['$httpProvider', function($httpProvider) {
+	$httpProvider.interceptors.push(['$q', '$location', function($q, $location) {
+		return {
+			request: function(config) {
+				config.headers = config.headers || {};
+
+				if (localStorage.getItem('authToken')) {
+					config.headers.Authorization = 'Bearer ' + localStorage.getItem('authToken');
+				}
+
+				return config;
+			},
+			responseError: function(response) {
+				if (response.status === 401) {
+					$location.path('/');
+				}
+
+				return $q.reject(response);
+			}
+		};
+	}]);
+}]);

+ 8 - 0
app/app.routes.js

@@ -8,6 +8,14 @@ app.config(function($routeProvider, $locationProvider) {
 	.when('/books', {
 		templateUrl: 'app/views/books-list.template.html',
 		controller: 'BooksList'
+	})
+		.when('/login', {
+		templateUrl: 'app/views/login.template.html',
+		controller: 'Login'
+	})
+		.when('/books/:id', {
+		templateUrl: 'app/views/books-details.template.html',
+		controller: 'BooksDetails'
 	})
 		.otherwise('/');
 	

+ 3 - 0
app/constants/webapi.constant.js

@@ -0,0 +1,3 @@
+app.constant('webApi', {
+	DOMAIN: 'http://helloworld.filonitta.fe4.a-level.com.ua'
+});

+ 38 - 0
app/controllers/books-details.controller.js

@@ -0,0 +1,38 @@
+(function () {
+'use strict';
+app.controller('BooksDetails', [
+	'$scope',           //зависимости
+	'books.repository', 
+	'$routeParams', 
+	function($scope, booksRepository, $routeParams) {
+	$scope.sortField = 'index';
+	$scope.sortBy = function(field) {
+	$scope.sortField = ($scope.sortField === field) ? '-' + field : field;
+}
+
+$scope.deleteBook = function(bookId) {
+ function findBookId(element, index, array) {
+     if (element.id === bookId) return true
+ }
+ var index = $scope.books.findIndex(findBookId);
+ $scope.books.splice(index, 1);
+	}
+	//запрос на сервер по книги)
+booksRepository.getBooks().then(function(responce) {
+$scope.books = responce.data;
+	console.log($scope.books);
+}, function(error) {
+	console.log(error);
+});
+//$routeParams.id  - взять id книги с адресной строки
+booksRepository.getBookById($routeParams.id).then(function(responce) {
+$scope.books = responce.data;
+	console.log(responce.data);
+}, function(error) {
+});
+
+}]);
+
+
+
+})();

+ 8 - 64
app/controllers/books-list.controller.js

@@ -1,7 +1,6 @@
 (function () {
 'use strict';
-app.controller('BooksList', function($scope) {
-	console.log("OK");
+app.controller('BooksList', ['$scope', 'books.repository', function($scope, booksRepository) {
 	$scope.sortField = 'index';
 	$scope.sortBy = function(field) {
 	$scope.sortField = ($scope.sortField === field) ? '-' + field : field;
@@ -13,70 +12,15 @@ $scope.deleteBook = function(bookId) {
  var index = $scope.books.findIndex(findBookId);
  $scope.books.splice(index, 1);
 	}
-
-	$scope.books = [
-		{
-			id: 0,
-			title: "Harry Potter",
-			author: "J. ROwling",
-			date: "1970-01-01",
-			cost: 100,
-			rate: 1.2
-	},
-	{
-		id: 1,
-		title: "Harry COMPUTER",
-		author: "J. ROwling",
-		date: "1970-01-01",
-		cost: 99,
-		rate: 2.4
-},
-{
-	id: 2,
-	title: "Harry LIGTER",
-	author: "Git. Bash",
-	date: "2026-05-15",
-	cost: 5,
-	rate: 1
-},
-{
-	id: 3,
-	title: "Berry Potter",
-	author: "M. Bower",
-	date: "1950-01-01",
-	cost: 88,
-	rate: 3
-},
-{
-	id: 4,
-	title: "Adam Potter",
-	author: "V. NPM",
-	date: "1870-01-01",
-	cost: 24,
-	rate: 3.2
-},
-{
-	id: 5,
-	title: "The Things We Don't Say",
-	author: "Carey, Ella",
-	date: "2010-15-01",
-	cost: 42,
-	rate: 1.1
-},
-{
-	id: 6,
-	title: "Невеста Мрачнейшего",
-	author: "Лилия Лисовская",
-	date: "2018-05-21",
-	cost: 55.6,
-	rate: 5
-}
-	];
-
-
+booksRepository.getBooks().then(function(responce) {
+				$scope.books = responce.data;
+									console.log($scope.books);
+								}, function(error) {
+									console.log(error);
+});
 
 
-});
+}]);
 
 
 

+ 11 - 3
app/controllers/header.controller.js

@@ -1,8 +1,16 @@
 (function () {
 'use strict';
-app.controller('Header', function($scope) {
-	console.log("Header");
+app.controller('Header', headerController);
 
-});
+function headerController($scope) {
+
+	$scope.isLogged = function() {
+		return localStorage.getItem('authToken') ? true : false;
+	};
+}
+
+headerController.$inject = [
+	'$scope'
+];
 
 })();

+ 23 - 0
app/controllers/login.controller.js

@@ -0,0 +1,23 @@
+(function () {
+'use strict';
+app.controller('Login', ['$scope', 'account.repository', '$location', function($scope, accountRepository, $location) {
+	$scope.user = {
+		login: "admin@gmail.com",
+		password: "qQ1!1111"
+	};
+
+	$scope.login = function() {
+accountRepository.login($scope.user).then(function(responce){
+	$location.path('/'); //адресс куда переходить после логина
+console.log('responce', responce.data);
+localStorage.setItem('authToken', responce.data.authToken);
+}, function(error){
+	
+})
+	};
+
+
+}]);
+
+
+})();

+ 18 - 0
app/services/account.factories.js

@@ -0,0 +1,18 @@
+(function() {
+'use strict';
+
+app.factory('account.repository', function(webApi, $http) {
+	return {
+		login: _login
+	};
+
+	function _login(data) {
+		return $http.post(webApi.DOMAIN + '/api/v2/login', data);
+	}
+	
+
+});
+
+
+
+})();

+ 21 - 0
app/services/books.factories.js

@@ -0,0 +1,21 @@
+(function() {
+'use strict';
+
+app.factory('books.repository', function(webApi, $http) {
+	return {
+		getBooks: _getBooks,
+		getBookById: _getBookById
+	};
+
+	function _getBooks() {
+		return $http.get(webApi.DOMAIN + '/api/v2/books');
+	}
+	function _getBookById(id) {
+		return $http.get(webApi.DOMAIN + '/api/v2/books/' + id);
+	}
+
+});
+
+
+
+})();

+ 1 - 0
app/views/books-details.template.html

@@ -0,0 +1 @@
+<h1>BOOK DETAILS</h1>

+ 4 - 1
app/views/books-list.template.html

@@ -21,7 +21,10 @@
 <td>{{book.date | date : 'longDate'}}</td>
 <td>{{book.cost | currency}}</td>
 <td>{{book.rate | number : 1}}</td>
-<td><button class="btn btn-danger btn-xs" ng-click="deleteBook(book.id)">x</button></td>
+<td>
+
+<a href="#/books/{{book.id}}" class="btn btn-info btn-xs">i</a>
+<button class="btn btn-danger btn-xs" ng-click="deleteBook(book.id)">x</button></td>
     </tr>
   </tbody>
 </table>

+ 2 - 1
app/views/header.template.html

@@ -7,9 +7,10 @@
             <ul class="nav navbar-nav">
  <li ng-class="{active: currentMenuItem == '/'}"><a href="#/">Home</a></li>
  <li ng-class="{active: currentMenuItem == '/books'}"><a href="#/books">Books</a></li>
+
             </ul>
             <ul class="nav navbar-nav navbar-right">
-               <li></li>
+                <li ng-if="!isLogged()" ng-class="{active: currentMenuItem == '/login'}"><a href="#/login" >Login</a></li>
             </ul>
         </div>
     </div>

+ 0 - 1
app/views/home.template.html

@@ -4,5 +4,4 @@ HOME</br>
 HOME</br>
 HOME</br>
 HOME</br>
-<h1>HOME</h1>
 </div>

+ 15 - 0
app/views/login.template.html

@@ -0,0 +1,15 @@
+<div class="row m-t-100">
+	<div class="col-xs-4 col-xs-offset-4">
+		<h3 class="text-center m-b-30">Authorization</h3>
+		<form class="well" ng-submit="login()">
+			<div class="form-group">
+				<input type="text" placeholder="Email" class="form-control" ng-model="user.login">
+			</div>
+			<div class="form-group">
+				<input type="password" placeholder="Password" class="form-control" ng-model="user.password">
+			</div>
+
+			<button class="btn btn-primary btn-block">Login</button>
+		</form>
+	</div>
+</div>

+ 9 - 2
index.html

@@ -2,12 +2,13 @@
 <html lang="en" ng-app='fea5'>
 <head>
   <meta charset="UTF-8">
-  <title>AngularJS</title>
+  <title>AngularJS test</title>
     <link rel="stylesheet" type="text/css" href="node_modules/bootstrap/dist/css/bootstrap.min.css"/>
     <link rel="stylesheet" type="text/css" href="css/style.css"/>
 </head>
 <body ng-controller="Main">
 <div class="container">
+<h2>{{$bookss[0].title}}</h2>
 <ng-include src="'app/views/header.template.html'"> </ng-include>
 <div ng-view></div>
 
@@ -15,17 +16,23 @@
 
 </div>
 
+
 <ng-include src="'app/views/footer.template.html'"> </ng-include>
 
   <script src="node_modules/angular/angular.js"></script>
   <script src="app/app.module.js"></script> 
   <script src="app/app.routes.js"></script>
   <script src="app/app.config.js"></script> 
+  <script src="app/controllers/header.controller.js"></script>
   <script src="app/controllers/main.controller.js"></script>
   <script src="app/controllers/books-list.controller.js"></script>
-  <script src="app/controllers/header.controller.js"></script>
+  <script src="app/controllers/books-details.controller.js"></script>
+  <script src="app/controllers/login.controller.js"></script>
   <script src="app/controllers/home.controller.js"></script>
   <script src="node_modules/angular-route/angular-route.min.js"></script>
+  <script src="app/constants/webapi.constant.js"></script>
+  <script src="app/services/books.factories.js"></script>
+  <script src="app/services/account.factories.js"></script>
 
 </body>
 </html>