showcase: REST web services
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 25 Aug 2015 06:31:14 +0000 (08:31 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 25 Aug 2015 06:31:14 +0000 (08:31 +0200)
12 files changed:
angularjs/showcase/src/showcase/app/app.module.js
angularjs/showcase/src/showcase/app/rest/rest.controller.js [new file with mode: 0644]
angularjs/showcase/src/showcase/app/rest/rest.html [new file with mode: 0644]
angularjs/showcase/src/showcase/app/rest/rest.module.js [new file with mode: 0644]
angularjs/showcase/src/showcase/app/rest/rest.route.js [new file with mode: 0644]
angularjs/showcase/src/showcase/app/rest/rest.route.spec.js [new file with mode: 0644]
angularjs/showcase/src/showcase/app/rest/rest.service.js [new file with mode: 0644]
angularjs/showcase/src/showcase/app/rest/rest.service.spec.js [new file with mode: 0644]
angularjs/showcase/src/showcase/app/welcome/welcome.html
angularjs/showcase/src/showcase/app/welcome/welcome.module.js
angularjs/showcase/src/showcase/app/welcome/welcome.route.spec.js
angularjs/showcase/src/showcase/index.html

index 4e68a30..e20f31d 100644 (file)
@@ -8,6 +8,7 @@
    *
    * @requires app.core
    * @requires app.welcome
+   * @requires app.rest
    *
    * @description
    * # app
@@ -20,7 +21,8 @@
     'app.core',
 
     /* Feature areas */
-    'app.welcome'
+    'app.welcome',
+    'app.rest'
   ]);
 
 }());
diff --git a/angularjs/showcase/src/showcase/app/rest/rest.controller.js b/angularjs/showcase/src/showcase/app/rest/rest.controller.js
new file mode 100644 (file)
index 0000000..66f6956
--- /dev/null
@@ -0,0 +1,32 @@
+(function () {
+  'use strict';
+
+  angular
+    .module('app.rest')
+    .controller('Rest', Rest);
+
+  /**
+   * @ngdoc controller
+   * @name app.rest.controller:Rest
+   *
+   * @requires $location
+   *
+   * <p>
+   * <br>
+   * {@link https://docs.angularjs.org/api/ng/service/$location $location}
+   * </p>
+   *
+   * @description
+   * Rest controller.
+   */
+  /* @ngInject */
+  function Rest($location) {
+    var vm = this;
+    vm.example = {
+      text: 'try to send data',
+      word: /^\s*\w*\s*$/,
+      singleModel: 1
+    };
+  }
+
+})();
diff --git a/angularjs/showcase/src/showcase/app/rest/rest.html b/angularjs/showcase/src/showcase/app/rest/rest.html
new file mode 100644 (file)
index 0000000..c34fdb4
--- /dev/null
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<div class="container">
+
+  <form name="myForm" ng-controller="Rest as vm">
+    <label>Single word:</label>
+    <input type="text" name="input" ng-model="vm.example.text"
+           ng-pattern="vm.example.word" required ng-trim="false">
+    <div role="alert">
+    <span class="error" ng-show="myForm.input.$error.required">
+      Required!</span>
+    <span class="error" ng-show="myForm.input.$error.pattern">
+      Single word only!</span>
+    </div>
+    <span>text = {{vm.example.text}}</span><br/>
+    <span>myForm.input.$valid = {{myForm.input.$valid}}</span><br/>
+    <span>myForm.input.$error = {{myForm.input.$error}}</span><br/>
+    <span>myForm.$valid = {{myForm.$valid}}</span><br/>
+    <span>myForm.$error.required = {{!!myForm.$error.required}}</span><br/>
+    <button type="button" class="btn btn-primary" ng-model="vm.singleModel" ng-disabled="myForm.input.$invalid">
+      Single Toggle
+    </button>
+  </form>
+
+
+
+
+</div>
\ No newline at end of file
diff --git a/angularjs/showcase/src/showcase/app/rest/rest.module.js b/angularjs/showcase/src/showcase/app/rest/rest.module.js
new file mode 100644 (file)
index 0000000..284a218
--- /dev/null
@@ -0,0 +1,20 @@
+(function() {
+  'use strict';
+
+  /**
+   * @ngdoc overview
+   * @name app.rest
+   *
+   * @requires app.core
+   *
+   * @description
+   * # app.welcome
+   *
+   * ## Module welcome.
+   * Module in charge of sending REST requests.
+   */
+  angular.module('app.rest', [
+    'app.core'
+  ]);
+
+})();
diff --git a/angularjs/showcase/src/showcase/app/rest/rest.route.js b/angularjs/showcase/src/showcase/app/rest/rest.route.js
new file mode 100644 (file)
index 0000000..5add5b9
--- /dev/null
@@ -0,0 +1,37 @@
+(function() {
+  'use strict';
+
+  angular
+    .module('app.rest')
+    .config(configure);
+
+  /**
+   * @ngdoc service
+   * @name app.rest.configure
+   *
+   * @requires $stateProvider
+   * @requires $urlRouterProvider
+   *
+   * <p>
+   * <br>
+   * {@link http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$stateProvider $stateProvider} <br>
+   * {@link http://angular-ui.github.io/ui-router/site/#/api/ui.router.router.$urlRouterProvider $urlRouterProvider}
+   * </p>
+   *
+   *
+   * @description
+   * Router configuration for rest application.
+   */
+  /* @ngInject */
+  function configure($stateProvider, $urlRouterProvider) {
+    var state = 'rest';
+    var config = {
+      abstract: false,
+      url: '/rest',
+      templateUrl: 'app/rest/rest.html'
+    };
+
+    $urlRouterProvider.otherwise(state);
+    $stateProvider.state(state, config);
+  }
+}());
diff --git a/angularjs/showcase/src/showcase/app/rest/rest.route.spec.js b/angularjs/showcase/src/showcase/app/rest/rest.route.spec.js
new file mode 100644 (file)
index 0000000..91459f7
--- /dev/null
@@ -0,0 +1,24 @@
+describe('app.rest', function() {
+  'use strict';
+
+  describe('state', function() {
+    var view = {
+      rest: 'app/rest/rest.html'
+    };
+    var $state;
+
+    beforeEach(function() {
+      module('app.rest');
+
+      inject(function(_$state_) {
+        $state = _$state_;
+      });
+    });
+
+    it('should map /rest route to rest View template', function() {
+      expect($state.get('rest').templateUrl). toEqual(view.rest);
+    });
+
+  });
+
+});
diff --git a/angularjs/showcase/src/showcase/app/rest/rest.service.js b/angularjs/showcase/src/showcase/app/rest/rest.service.js
new file mode 100644 (file)
index 0000000..e2eef11
--- /dev/null
@@ -0,0 +1,53 @@
+(function () {
+  'use strict';
+
+  angular
+    .module('app.rest')
+    .factory('cars', cars);
+
+  /**
+   * @ngdoc service
+   * @name app.rest.cars
+   *
+   * @requires $http
+   * @requires $log
+   *
+   * <p>
+   * <br>
+   * {@link https://docs.angularjs.org/api/ng/service/$http $http} <br>
+   * {@link https://docs.angularjs.org/api/ng/service/$log $log}
+   * </p>
+   *
+   * @description
+   * Rest service.
+   */
+  /* @ngInject */
+  function cars($http, $log) {
+    return {
+      getAll: getAll
+    };
+
+    /**
+     * @ngdoc method
+     * @name  getAll
+     * @methodOf app.rest.cars
+     *
+     * @description
+     * Get cars from API REST.
+     */
+    function getAll() {
+      return $http.get('/api/cars')
+        .then(getAllCompleted)
+        .catch(getAllFailed);
+
+      function getAllCompleted(response) {
+        return response.data.results;
+      }
+
+      function getAllFailed(error) {
+        $log.debug('XHR Failed for sendData.' + error.data);
+      }
+    }
+  }
+
+}());
diff --git a/angularjs/showcase/src/showcase/app/rest/rest.service.spec.js b/angularjs/showcase/src/showcase/app/rest/rest.service.spec.js
new file mode 100644 (file)
index 0000000..8fa8d70
--- /dev/null
@@ -0,0 +1,34 @@
+describe('app.rest', function() {
+  'use strict';
+
+  var $httpBackend;
+  var $log;
+  var cars;
+
+  beforeEach(function() {
+    module('app.rest');
+
+    inject(function(_$httpBackend_, _$log_, _cars_) {
+      $httpBackend = _$httpBackend_;
+      $log = _$log_;
+      cars = _cars_;
+    });
+  });
+
+  describe('cars service', function () {
+
+    it('should invoke GET all cars', function () {
+      $httpBackend.expectGET('/api/cars').respond({});
+
+      cars.getAll();
+
+      $httpBackend.flush();
+    });
+  });
+
+  afterEach(function() {
+    $httpBackend.verifyNoOutstandingExpectation();
+    $httpBackend.verifyNoOutstandingRequest();
+  });
+
+});
index 6c05370..f47e20f 100644 (file)
@@ -1,7 +1,7 @@
 <!DOCTYPE html>
-<div class="container" ng-controller="Welcome as welcome">
+<div class="container" ng-controller="Welcome as vm">
   <div class="row">
-    {{ welcome.hello }}
+    {{ vm.hello }}
 
     <div class="col-md-2">
     </div>
index 2115c9f..1498239 100644 (file)
@@ -11,7 +11,7 @@
    * # app.welcome
    *
    * ## Module welcome.
-   * Module in charge of displaying a nice welcome page. LOL ROLF
+   * Module in charge of displaying a nice welcome page.
    */
   angular.module('app.welcome', [
     'app.core'
index 4db3d48..7478589 100644 (file)
@@ -16,7 +16,7 @@ describe('app.welcome', function() {
     });
 
     it('should map /welcome route to welcome View template', function() {
-      expect($state.get('welcome').templateUrl). toEqual(view.welcome);
+      expect($state.get('welcome').templateUrl).toEqual(view.welcome);
     });
 
   });
index 1d74680..2b3c913 100644 (file)
     <script src="/src/showcase/app/welcome/welcome.module.js"></script>
     <script src="/src/showcase/app/welcome/welcome.route.js"></script>
     <script src="/src/showcase/app/welcome/welcome.controller.js"></script>
+    <script src="/src/showcase/app/rest/rest.module.js"></script>
+    <script src="/src/showcase/app/rest/rest.service.js"></script>
+    <script src="/src/showcase/app/rest/rest.route.js"></script>
+    <script src="/src/showcase/app/rest/rest.controller.js"></script>
     <script src="/src/showcase/app/core/core.module.js"></script>
     <script src="/src/showcase/app/app.module.js"></script>
     <!-- endinject -->