vlad vor 6 Jahren
Ursprung
Commit
ffeefd191c

+ 24 - 1
app/app.config.js

@@ -2,4 +2,27 @@ app.run(function($rootScope, $location) {
 	$rootScope.$on('$routeChangeSuccess', function(){
 		$rootScope.currentMenuItem = $location.path() || '/home';
 	})
-})
+})
+// 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

@@ -10,5 +10,13 @@ app.config(function($routeProvider,$locationProvider) {
 			templateUrl: 'app/views/books-list.template.html',
 			controller: 'BooksList'
 		})
+		.when('/books/:id',{
+			templateUrl: 'app/views/books-details.template.html',
+			controller: 'BookDetails'
+		})
+		.when('/login',{
+			templateUrl: 'app/views/login.template.html',
+			controller: 'Login'
+		})
 		.otherwise('/')
 })

+ 3 - 0
app/constant/webApi.const.js

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

+ 8 - 0
app/controllers/bookDetails.js

@@ -0,0 +1,8 @@
+(function(){
+    'use strict';
+    app.controller('BookDetails', ['$scope','books.repository', '$routeParams',function ($scope, booksRepository, $routeParams) {
+       booksRepository.getBookById($routeParams.id).then(function(response){
+           
+       }) 
+    }])
+})()

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

@@ -1,6 +1,6 @@
 (function() {
 	'use strict';
-	app.controller('BooksList', function($scope){
+	app.controller('BooksList', ['$scope','books.repository',function($scope, booksRepository){
 		$scope.sortFild = 'title';
 		$scope.sortBy = function(field){
 			$scope.sortFild = ($scope.sortFild === field)? '-'+ field : field;
@@ -14,87 +14,11 @@
 				}
 			}
 		}
-		$scope.books = [
-			{
-				title: 'Harry Potter',
-				author: 'J. Rowling',
-				date: '1998-01-01',
-				cost: '300$',
-				rate: '50',
-				id: 1
-			},
-			{
-				title: 'Terra Ucrainica',
-				author: 'Д. Вортман',
-				date: '1991-12-11',
-				cost: '100$',
-				rate: '4',
-				id: 2
-			},
-			{
-				title: 'Аеропорт',
-				author: 'А. Гейлі',
-				date: '2000-03-03',
-				cost: '500$',
-				rate: '3',
-				id: 3
-			},
-			{
-				title: 'Бог завжди подорожує інкогніто',
-				author: 'Л. Гунель',
-				date: '2001-10-12',
-				cost: '200$',
-				rate: '4',
-				id: 4
-			},
-			{
-				title: 'Джерело. Клубне видання',
-				author: 'Д. Браун',
-				date: '1999-10-11',
-				cost: '500$',
-				rate: '5',
-				id: 5
-			},
-			{
-				title: 'Вежа блазнів',
-				author: 'А. Сапковський',
-				date: '1993-04-02',
-				cost: '400$',
-				rate: '8',
-				id: 6
-			},
-			{
-				title: 'Ненавижу, потому что люблю',
-				author: 'Н. Соболевская',
-				date: '2011-10-03',
-				cost: '100$',
-				rate: '10',
-				id: 7
-			},
-			{
-				title: 'Крейдяна Людина',
-				author: 'С. Дж. Тюдор',
-				date: '1990-03-08',
-				cost: '10$',
-				rate: '2',
-				id: 8
-			},
-			{
-				title: 'Джерело. Суперобкладинка',
-				author: 'Д. Браун',
-				date: '2000-03-05',
-				cost: '500$',
-				rate: '20',
-				id: 9
-			},
-			{
-				title: 'Великий атлас світу',
-				author: 'ред. О. Король',
-				date: '1997-10-11',
-				cost: '300$',
-				rate: '5',
-				id: 10
-			},
-		]
-	})
+		
+		booksRepository.getBooks().then(function(response){
+			$scope.books = response.data;
+		}, function(error){
+			
+		});
+	}])
 })()

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

@@ -0,0 +1,9 @@
+(function(){
+    app.controller('Login', ['$scope','account.repository', function($scope, accountRepository){
+        $scope.user = {
+            login: 'admin@gmail.com',
+            password: 'qQ1!1111'
+        }
+        $scope.login
+    }])
+})()

+ 15 - 0
app/services/account.factory.js

@@ -0,0 +1,15 @@
+(function(){
+    'use strict';
+    app.factory('account.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)
+        }
+    })
+})()

+ 15 - 0
app/services/books.factory.js

@@ -0,0 +1,15 @@
+(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>Her</h1>

+ 1 - 0
app/views/books-list.teamplate.html

@@ -21,6 +21,7 @@
 						<td>{{item.cost}}</td>
 						<td>{{item.rate | number : 1}}</td>
 						<td>
+							<a href="#/books/{{book.id}}" class="btn btn-info btn-xs"><i class="glyphicon glyphicon-eye-open"></i></a>
 							<button class="btn btn-danger" ng-click='deleteBook(item.id)'></button>
 						</td>
 					</tr>

+ 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>

+ 5 - 0
index.html

@@ -16,10 +16,15 @@
 	<script type="text/javascript" src="./node_modules/angular/angular.js"></script>
 	<script type="text/javascript" src="./node_modules/angular-route/angular-route.min.js"></script>
 	<script type="text/javascript" src="app/app.module.js"></script>
+	<script type="text/javascript" src="app/constant/webApi.const.js"></script>
+	<script type="text/javascript" src="app/services/books.factory.js"></script>
+	<script type="text/javascript" src="app/services/account.factory.js"></script>
 	<script type="text/javascript" src="app/app.routes.js"></script>
 	<script type="text/javascript" src="app/app.config.js"></script>
 	<script type="text/javascript" src="app/controllers/books-list.controller.js"></script>
 	<script type="text/javascript" src="app/controllers/main.controller.js"></script>
 	<script type="text/javascript" src="app/controllers/header.controller.js"></script>
+	<script type="text/javascript" src="app/controllers/bookDetails.js"></script>
+	<script type="text/javascript" src="app/controllers/login.controller.js"></script>
 </body>
 </html>