Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var app = angular.module('quizApp', ['ngRoute']);
- //var apiBaseURL = 'http://0.0.0.0:3000/api';
- var apiBaseURL = 'http://10.69.2.15:3000/api';
- app.config(function($routeProvider) {
- $routeProvider
- .when('/', {
- controller : 'LoginController',
- templateUrl : 'login.html'
- })
- .when('/quiz/:quizId', {
- controller: 'QuizController',
- templateUrl: 'quiz.html'
- })
- .otherwise({
- redirectTo: '/'
- });
- });
- app.controller('LoginController', ['$scope', '$location', 'users', function($scope, $location, users) {
- $scope.loginError = false;
- $scope.email = 'admin@localhost';
- $scope.password = 'azerty';
- $scope.loginAction = function() {
- $scope.loginError = false;
- var promise = users.loginUser($scope.email, $scope.password);
- promise.then(function(response) {
- $location.path('/quiz/1');
- }, function(error) {
- $scope.loginError = true;
- });
- }
- }]);
- app.controller("QuizController" , ['$scope' , '$location' , 'question' , 'users' , "$routeParams"
- , function($scope , $location , question , users , $routeParams)
- {
- $scope.choosedResponse = null;
- $scope.data = null;
- $scope.badResponse = false;
- if(!users.authenticated) {
- $location.path("/");
- }
- else {
- var promise = question.getQuestion($routeParams.quizId);
- promise.then(function (reponse) {
- $scope.data = reponse.data;
- $scope.choosedResponse = $scope.data.response1;
- }, function (error) {
- console.log(error);
- });
- $scope.checkResponse = function()
- {
- if($scope.choosedResponse == $scope.data.validResponseId) {
- $scope.badResponse = false;
- if($routeParams.quizId < 3)
- {
- var id = parseInt($routeParams.quizId) +1;
- $location.path("/quiz/"+id);
- }
- }
- else {
- $scope.badResponse = true;
- }
- }
- }
- }]);
- app.service('users', ['fakeHttp', function($http) {
- this.authenticated = false;
- var users = this;
- this.loginUser = function(email, password) {
- users.authenticated = false;
- var loginUrl = apiBaseURL + '/Users/login';
- var postData = {
- 'email': email,
- 'password': password
- };
- var headers = {
- 'Content-Type': 'application/json',
- 'Accept': 'application/json'
- };
- var promise = $http.post(loginUrl, postData, headers);
- promise.then(function(response) {
- users.authenticated = true;
- }, function(error) {
- users.authenticated = false;
- });
- return promise;
- };
- }]);
- app.service("question" , ["fakeHttp" , function($http)
- {
- this.title;
- this.getQuestion = function(id)
- {
- var questionUrl = apiBaseURL + '/questions/'+id;
- var promise = $http.get(questionUrl);
- return promise;
- }
- }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement