Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. var app = angular.module('quizApp', ['ngRoute']);
  2.  
  3. //var apiBaseURL = 'http://0.0.0.0:3000/api';
  4. var apiBaseURL = 'http://10.69.2.15:3000/api';
  5.  
  6. app.config(function($routeProvider) {
  7. $routeProvider
  8. .when('/', {
  9. controller : 'LoginController',
  10. templateUrl : 'login.html'
  11. })
  12. .when('/quiz/:quizId', {
  13. controller: 'QuizController',
  14. templateUrl: 'quiz.html'
  15. })
  16. .otherwise({
  17. redirectTo: '/'
  18. });
  19. });
  20.  
  21. app.controller('LoginController', ['$scope', '$location', 'users', function($scope, $location, users) {
  22. $scope.loginError = false;
  23.  
  24. $scope.email = 'admin@localhost';
  25. $scope.password = 'azerty';
  26.  
  27. $scope.loginAction = function() {
  28. $scope.loginError = false;
  29.  
  30. var promise = users.loginUser($scope.email, $scope.password);
  31. promise.then(function(response) {
  32. $location.path('/quiz/1');
  33. }, function(error) {
  34. $scope.loginError = true;
  35. });
  36. }
  37. }]);
  38.  
  39. app.controller("QuizController" , ['$scope' , '$location' , 'question' , 'users' , "$routeParams"
  40. , function($scope , $location , question , users , $routeParams)
  41. {
  42. $scope.choosedResponse = null;
  43. $scope.data = null;
  44. $scope.badResponse = false;
  45. if(!users.authenticated) {
  46. $location.path("/");
  47. }
  48. else {
  49.  
  50. var promise = question.getQuestion($routeParams.quizId);
  51.  
  52. promise.then(function (reponse) {
  53. $scope.data = reponse.data;
  54. $scope.choosedResponse = $scope.data.response1;
  55. }, function (error) {
  56. console.log(error);
  57. });
  58. $scope.checkResponse = function()
  59. {
  60. if($scope.choosedResponse == $scope.data.validResponseId) {
  61. $scope.badResponse = false;
  62. if($routeParams.quizId < 3)
  63. {
  64. var id = parseInt($routeParams.quizId) +1;
  65. $location.path("/quiz/"+id);
  66. }
  67. }
  68. else {
  69. $scope.badResponse = true;
  70. }
  71. }
  72. }
  73.  
  74. }]);
  75.  
  76. app.service('users', ['fakeHttp', function($http) {
  77.  
  78. this.authenticated = false;
  79. var users = this;
  80.  
  81. this.loginUser = function(email, password) {
  82.  
  83. users.authenticated = false;
  84. var loginUrl = apiBaseURL + '/Users/login';
  85. var postData = {
  86. 'email': email,
  87. 'password': password
  88. };
  89. var headers = {
  90. 'Content-Type': 'application/json',
  91. 'Accept': 'application/json'
  92. };
  93. var promise = $http.post(loginUrl, postData, headers);
  94. promise.then(function(response) {
  95. users.authenticated = true;
  96. }, function(error) {
  97. users.authenticated = false;
  98. });
  99. return promise;
  100. };
  101. }]);
  102.  
  103. app.service("question" , ["fakeHttp" , function($http)
  104. {
  105. this.title;
  106. this.getQuestion = function(id)
  107. {
  108. var questionUrl = apiBaseURL + '/questions/'+id;
  109. var promise = $http.get(questionUrl);
  110. return promise;
  111. }
  112. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement