AngularJS And ASP.NET MVC Movie Library Application - Integration Of IMDB Movie API - Part Fiv
- ANGULARJS AND ASP.NET MVC MOVIE LIBRARY APPLICATION - PART ONE
- AngularJS And ASP.NET MVC Movie Library Application - Part Two Module,Controller,$Scope and Twitter Bootstrap
- AngularJS And ASP.NET MVC Movie Library Application - Part Three- EventBinding with UI Elements
- AngularJS And ASP.NET MVC Movie Library Application - AngularJS Routing with partial templates - Part Four
Moving ahead, in this article, we’ll focus on the integration of live movie API within an existing application.
Kindly open the given URL and register yourself. After registering, you will get a token which we’ll be using in this article to retrieve the information/data from API.
http://api.myapifilms.com/imdb
Once you register yourself, you should receive an email like the given image, with some token value.
Open the website with the link, shared above, and fill in the details, as shown below in the following images.
As soon as you click on the submit button, you should receive a response in JSON format, as given below:
You may also get XML response for just making amendments either in request or in format dropdown, as shown below:
So far so good. We are now able to use this API URL within our application.
Open you solution and find routing.js file to add a new Controller into this, as give below:
- routingApp.controller("ApiMovieController", ['$scope', '$http', '$timeout', function ($scope, $http, $timeout) {
- $scope.SearchMovieByTitle = function () {
- var title = $scope.searchByTitle;
- alert(title);
- $http.jsonp("http://api.myapifilms.com/imdb/idIMDB?title=" + title + "&token=5bf98789787hghjg4&format=json&language=en-us&aka=0&business=0&seasons=0&seasonYear=0&technical=0&filter=2&exactFilter=0&limit=5&forceYear=0&trailers=0&movieTrivia=0&awards=0&moviePhotos=0&movieVideos=0&actors=0&biography=0&uniqueName=0&filmography=0&bornAndDead=0&starSign=0&actorActress=0&actorTrivia=0&similarMovies=0&adultSearch=0&goofs=0"es=0&fullSize=0&companyCredits=0&callback=JSON_CALLBACK").success(function (response) {
- console.log(response);
- $scope.movies = response.data.movies;
- })
- }]);
Kindly, put your token value in the above http request, in order to get the correct result.
Note: If you have noticed, in the above code segment, you have injected a dependency $http and $timeout. For now, only concentrate on the $http and forget about $timeout.
Complete code for routing.js main JavaScript file is shown below:
Code Routing.js file
- /// <reference path="../Scripts/angular.js" />
- /// <reference path="../Scripts/fusioncharts.js" />
- /// <reference path="../Scripts/fusioncharts.charts.js" />
- /// <reference path="DataService.js" />
- /// <reference path="../Scripts/angular-route.js" />
- var routingApp = angular.module('routingApp', ['ngRoute']);
- routingApp.directive("login", function () {
- var directive = {};
- //restrict = E, signifies that directive is Element directive
- directive.restrict = 'E';
- //template replaces the complete element with its text.
- directive.templateUrl = "/Application/Login.html";//"My first directive";
- return directive;
- });
- routingApp.config(['$routeProvider', function ($routeProvider) {
- //alert("Route Initiated");
- $routeProvider.
- // when('/Home', { templateUrl: '/Application/login.html', controller: 'DotnetPiperController' }).
- when('/Movie', { templateUrl: '/Application/Movie.html', controller: 'MovieController' }).
- when('/SearchMovie', { templateUrl: '/Application/SearchMovie.html', controller: 'ApiMovieController' }).
- when('/Tolly', { templateUrl: '/Application/Tollywood.html', controller: 'tollyController' }).
- otherwise({ redirectTo: '' });
- }]);
- routingApp.controller("tollyController", function ($scope) {
- $scope.tollyMessage = "Welcome to TollyWood to watch Action,Thriller and Suspence movies";
- });
- routingApp.controller("MovieController", ['$scope', function ($scope) {
- $scope.edit = false;
- $scope.message = "Welcome to DotnetPiper.com to learn Angular";
- $scope.error = false;
- $scope.clear = false;
- $scope.success = false;
- // alert("Servcie Called");
- var movies = [
- { title: "Revenent", year: "2015", rating: "5Star", plot: " A revenger Journey" },
- { title: "Counjouring2", year: "2016", rating: "4Star", plot: " A Complete Hourror" },
- { title: "DDLJ", year: "1995", rating: "SuperStar", plot: "Romantic love story" },
- { title: "Sultan", year: "2016", rating: "5Star", plot: "A Warrior" },
- { title: "BajiRao Mastani", year: "2015", rating: "4.5 Star", plot: "Film of the Year" }
- ];
- $scope.movies = movies;
- $scope.AddMovie = function (movie) {
- if ($scope.edit == true) {
- var index = $scope.movies.indexOf(movie);
- // alert("edit Called");
- $scope.movies[index] = movie;
- //alert(movie.rating);
- $scope.updatedMovie = movie;
- $scope.success = true;
- $scope.movie = {};
- }
- else {
- var newMovie = {
- title: $scope.movie.title,
- year: $scope.movie.year,
- rating: $scope.movie.rating,
- plot: $scope.movie.plot
- };
- movies.push(newMovie);
- // alert("Add Called");
- }
- }
- $scope.DeleteMovie = function (movie, index) {
- movies.splice(index, 1);
- // $scope.movie = movie;
- $scope.updatedMovie = movie;
- $scope.success = false;
- $scope.clear = true;
- $scope.movie = {};
- console.log(index);
- }
- $scope.EditMovie = function (movie, index) {
- $scope.selectedRow = null; // initialize our variable to null
- $scope.selectedRow = index;
- $scope.movie = movie;
- $scope.edit = true;
- }
- }]);
- routingApp.controller("ApiMovieController", ['$scope', '$http', '$timeout', function ($scope, $http, $timeout) {
- $scope.SearchMovieByTitle = function () {
- var title = $scope.searchByTitle;
- alert(title);
- $http.jsonp("http://api.myapifilms.com/imdb/idIMDB?title=" + title + "&token=5bf94c9e-203f-4a6f-91d0-a63a59a77084&format=json&language=en-us&aka=0&business=0&seasons=0&seasonYear=0&technical=0&filter=2&exactFilter=0&limit=5&forceYear=0&trailers=0&movieTrivia=0&awards=0&moviePhotos=0&movieVideos=0&actors=0&biography=0&uniqueName=0&filmography=0&bornAndDead=0&starSign=0&actorActress=0&actorTrivia=0&similarMovies=0&adultSearch=0&goofs=0"es=0&fullSize=0&companyCredits=0&callback=JSON_CALLBACK").success(function (response) {
- console.log(response);
- $scope.movies = response.data.movies;
- })
- }
- $scope.dateTime = new Date().getMinutes();
- // alert($scope.dateTime);
- document.getElementById("btnSearch").addEventListener('click', function SearchMovieByTitleDigest() {
- var title = $scope.searchByTitle;
- $scope.$watch("searchByTitle", function (newValue, oldValue) {
- $scope.searchByTitle = newValue;
- console.log("$scope.searchByTitle Called " + $scope.searchByTitle);
- alert($scope.searchByTitle);
- });
- //console.log(title);
- //alert(title);
- $http.jsonp("http://api.myapifilms.com/imdb/idIMDB?title=" + $scope.searchByTitle + "&token=5bc9e-203f-4a6f-91d0-a63a59de4222&format=json&language=en-us&aka=0&business=0&seasons=0&seasonYear=0&technical=0&filter=2&exactFilter=0&limit=5&forceYear=0&trailers=0&movieTrivia=0&awards=0&moviePhotos=0&movieVideos=0&actors=0&biography=0&uniqueName=0&filmography=0&bornAndDead=0&starSign=0&actorActress=0&actorTrivia=0&similarMovies=0&adultSearch=0&goofs=0"es=0&fullSize=0&companyCredits=0&callback=JSON_CALLBACK")
- .success(function (response) {
- $scope.movies = response.data.movies;
- $timeout(function () {
- $scope.$digest();
- }, 100);
- })
- });
- //document.getElementById("btnSearch").addEventListener('click', function () {
- // console.log("Seach Started");
- // alert("Seach Started");
- // $scope.dateTime = new Date().getMinutes();
- // $scope.$digest();
- // });
- }]);
We are almost done with our Controller code part. It's now turn to create the UI to meet our purpose.
Kindly open your solution and add searchMovie.html file, as shown below in the screenshot:
Please copy and paste the following code in searchMovie.html partial template.
Code for searchMovie.html file,
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <h2>Seach Movie Using IMDB API</h2>
- <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
- <style>
- .selected
- {
- background-color: lightyellow;
- color: red;
- font-weight: bold;
- }
- </style>
- </head>
- <body ng-app="routingApp" class="jumbotron">
- <div ng-controller="ApiMovieController" class="container">
- <!--<div ng-show="success" class="alert-success">Record has been upddated for movie : {{updatedMovie.title}}</div>
- <div ng-show="clear" class="alert-success">Record has been deleted for movie : {{updatedMovie.title}}</div>-->
- <div class="row col-md-8">
- <table class="table table-striped ">
- <tr>
- <td>
- <input type="text" ng-model="searchByTitle" class="form-control" style="width: 300px;" />
- </td>
- {{dateTime}}
- <td>
- <button type="button" ng-click="SearchMovieByTitle()" class="btn btn-info">Search using Angular Scope
- <span class="glyphicon glyphicon-search"></span>
- </button>
- <br />
- </td>
- </tr>
- <tr>
- <td></td>
- <td>
- <button type="button" id="btnSearch" class="btn btn-info">Seacrh Movie Using EventListener
- <span class="glyphicon glyphicon-search"></span>
- </button>
- </td>
- </tr>
- <tr class="thead-inverse">
- <td style="background-color: Highlight">Title</td>
- <td style="background-color: Highlight">Year of Release</td>
- <td style="background-color: Highlight">Rating</td>
- <td style="background-color: Highlight">Plot</td>
- <td style="background-color: Highlight">Actions</td>
- </tr>
- <tbody>
- <tr ng-repeat="movie in movies" ng-class="{'selected':$index == selectedRow}">
- <td>{{movie.title}}
- </td>
- <td>{{movie.year}}
- </td>
- <td>{{movie.rating}}
- </td>
- <td>{{movie.plot }}
- </td>
- <td></td>
- </tr>
- </tbody>
- </table>
- </div>
- </div>
- </body>
- </html>
Once you paste the above code segment, run an application and click on Search Movie Globally.
Note: Ensure that you had implemented the routing, as shown in the below URL,
The output will be, as depicted below:
Mapping of the above textbox search, using Angular Scope. Whatever value we put into the textbox, ApiMovieController reads that value using scope and passes into http request as Title, as depicted in the screenshot below:
Now, search for the movie as I’ve done for The Revenant and Sultan, both.
Kindly refer tothe given screen for full fledged movie search operation.
Hope it’ll help you some day. Enjoy Coding.
0 comments :
Post a Comment