'use strict';

module.exports = ['$q', '$log', '$http', 'authService', questionService];

function questionService($q, $log, $http, authService) {
  $log.debug('questionService');

  let service = {};
  service.questions = [];

  service.getQuestions = function() {
    $log.debug('inside of service.getQuestions()');

    let url = `${__API_URL__}/api/questions`;

    let config = {
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json'
      }
    };

    return $http.get(url, config)
    .then( res => {
      $log.log('questions retrieved', res);
      service.questions = res.data;
      return service.questions;
    })
    .catch( err => {
      $log.error(err.message);
      return $q.reject(err);
    });

  };

  service.createQuestion = function(question) {
    $log.debug('inside of service.createQuestion()');

    return authService.getToken()
    .then( token => {
      let url = `${__API_URL__}/api/questions`;
      let config = {
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json'
          // Authorization: 'Bearer' + authService.getToken()
        }
      };

      return $http.post(url, question, config);
    })
    .then( res => {
      $log.log('question created');
      let question = res.data;
      service.questions.unshift(question);
      console.log(question);
      return question;
    })
    .catch( err => {
      $log.error(err.message);
      return $q.reject(err);
    });
  };
  return service;
}

@@ -1084,24 +1084,25 @@
 cation/json'
+,
 %0A          /
@@ -1122,41 +1122,25 @@
 on: 
-'
+%60
 Bearer
-' + authService.getT
+ $%7Bt
 oken
-()
+%7D%60
 %0A   
@@ -1489,21 +1489,874 @@
     %7D);%0A
-
   %7D;%0A
+%0A  service.updateQuestion = function(questionID, questionData) %7B%0A    $log.debug('questionService.updateQuestion()');%0A%0A    return authService.getToken()%0A    .then( token =%3E %7B%0A      let url = %60$%7B__API_URL__%7D/api/questions/$%7BquestionID%7D%60;%0A      let config = %7B%0A        headers: %7B%0A          Accept: 'application/json',%0A          // Authorization: %60Bearer $%7Btoken%7D%60,%0A          'Content-Type': 'application/json'%0A        %7D%0A      %7D;%0A%0A      return $http.put(url, questionData, config);%0A    %7D)%0A    .then( res =%3E %7B%0A      for (let i = 0; i %3C service.questions.length; i++) %7B%0A        let current = service.questions%5Bi%5D;%0A        if (current._id === questionID) %7B%0A          service.questions%5Bi%5D = res.data;%0A          break;%0A        %7D%0A      %7D%0A%0A      return res.data;%0A    %7D)%0A    .catch( err =%3E %7B%0A      $log.error(err.message);%0A      return $q.reject(err);%0A    %7D);%0A  %7D;%0A%0A
   return

PUT on question working