Advertisement
Guest User

Untitled

a guest
Mar 26th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. (function () {
  2. 'use strict';
  3. /** Service to return the data */
  4.  
  5. angular.module('MovieApp').
  6. service('dataService', // the data service name, can be anything we want
  7. ['$q', // dependency, $q handles promises, the request initially returns a promise, not the data
  8. '$http', // dependency, $http handles the ajax request
  9. function($q, $http) { // the parameters must be in the same order as the dependencies
  10.  
  11. /*
  12. * var to hold the data base url
  13. */
  14. var urlBase = '/cm0665-assignment/server/index.php';
  15. var loginUrl = '/cm0665-assignment/server/login.php';
  16.  
  17. /*
  18. * method to retrieve courses, or, more accurately a promise which when
  19. * fulfilled calls the success method
  20. */
  21. this.getCategories = function () {
  22. var defer = $q.defer(), // The promise
  23. data = {
  24. action: 'listCategory',
  25. //subject: 'category'
  26. };
  27.  
  28. $http.get(urlBase, {params: data, cache: true}). // notice the dot to start the chain to success()
  29. success(function(response){
  30. defer.resolve({
  31. data: response.ResultSet.Result, // create data property with value from response
  32. rowCount: response.RowCount // create rowCount property with value from response
  33. });
  34. }). // another dot to chain to error()
  35. error(function(err){
  36. defer.reject(err);
  37. });
  38. // the call to getCourses returns this promise which is fulfilled
  39. // by the .get method .success or .failure
  40. return defer.promise;
  41. };
  42.  
  43. this.getMovies = function (categoryid) {
  44. var defer = $q.defer(),
  45. data = {
  46. action: 'listFilms',
  47. //subject: 'films',
  48. category_id: categoryid
  49. }
  50. $http.get(urlBase, {params: data, cache: true}).
  51. success(function(response){
  52. defer.resolve({
  53. data: response.ResultSet.Result,
  54. rowCount: response.RowCount
  55. });
  56. }).
  57. error(function(err){
  58. defer.reject(err);
  59. });
  60. return defer.promise;
  61. };
  62.  
  63. this.getActors = function (filmid) {
  64. var defer = $q.defer(),
  65. data = {
  66. action: 'listActors',
  67. film_id: filmid
  68. }
  69. $http.get(urlBase, {params: data, cache: true}).
  70. success(function(response){
  71. defer.resolve({
  72. data: response.ResultSet.Result,
  73. rowCount: response.RowCount
  74. });
  75. }).
  76. error(function(err){
  77. defer.reject(err);
  78. });
  79. return defer.promise;
  80. };
  81.  
  82. this.getAllMovie = function () {
  83. var defer = $q.defer(),
  84. data = {
  85. action: 'listFilms'
  86. }
  87. $http.get(urlBase, {params: data, cache: true}).
  88. success(function(response){
  89. defer.resolve({
  90. data: response.ResultSet.Result,
  91. rowCount: response.RowCount
  92. });
  93. }).
  94. error(function(err){
  95. defer.reject(err);
  96. });
  97. return defer.promise;
  98. };
  99.  
  100. this.getSearchResult = function () {
  101. var defer = $q.defer(),
  102. data = {
  103. action: 'search',
  104. // term: terms
  105. }
  106. $http.get(urlBase, {params: data, cache: true}).
  107. success(function(response){
  108. defer.resolve({
  109. data: response.ResultSet.Result,
  110. rowCount: response.RowCount
  111. });
  112. }).
  113. error(function(err){
  114. defer.reject(err);
  115. });
  116. return defer.promise;
  117. };
  118.  
  119. this.login = function (userID, passwd) {
  120. var defer = $q.defer(),
  121. data = {
  122. //action: 'loginRob',
  123. userid: userID,
  124. password: passwd
  125. };
  126.  
  127. $http.post(loginUrl, data). // notice the dot to start the chain to success()
  128. success(function (response) {
  129. defer.resolve({
  130. data: response.status, // create data property with value from response
  131. result: response,
  132. user: response.username
  133. });
  134. console.log(response);
  135. }). // another dot to chain to error()
  136.  
  137. error(function (err) {
  138. defer.reject(err);
  139. });
  140. return defer.promise;
  141. };
  142.  
  143. }
  144. ]
  145. );
  146. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement