/// <summary>
 /// Method for converting a FeedbackEntity instance to a Feedback instance.
 /// </summary>
 /// <param name="entity"></param>
 /// <returns>Instance of class Feedback.</returns>
 public Feedback FeedbackEntityToFeedback(FeedbackEntity entity)
 {
     // Construct and return a new instance of class Feedback, containing the data from the passed entity class
     return new Feedback()
     {
         Content = entity.Content,
         AnswerId = entity.Answer.Id,
         QuestionId = entity.Question.Id,
         User = UserEntityToUser(entity.User),
         FeedbackType = entity.FeedbackType,
         FeedbackState = entity.FeedbackState,
         CreationTime = entity.CreationTime
     };
 }
        /// <summary>
        /// Method used for saving user feedback for an answer.
        /// </summary>
        /// <param name="feedback">The textual feedback given by the user.</param>
        /// <param name="answerId">The id of the answer for which the feedback has been given.</param>
        /// <param name="questionId">The id of the question to which the answer is assigned.</param>
        /// <param name="feedbackType">The type of feedback which indicates if the user accepted or declined the answer.</param>
        /// <param name="feedbackToken">The feedback token is required to provide feedback to answers on a question. It
        /// is used to make sure the user that asked the question is also the user giving the feedback and that feedback
        /// can only be given once.</param>
        public void CreateFeedback(string feedback, int answerId, int questionId, FeedbackType feedbackType, string feedbackToken)
        {
            // Validate input data
            Validation.StringCheck(feedback);
            Validation.IdCheck(answerId);
            Validation.IdCheck(questionId);
            Validation.StringCheck(feedbackToken);

            using (IntelliCloudContext context = new IntelliCloudContext())
            {
                // Get the answer entity from the context
                AnswerEntity answer = context.Answers
                    .Include(a => a.User)
                    .Include(a => a.User.Sources)
                    .Include(a => a.LanguageDefinition)
                    .SingleOrDefault(a => a.Id == answerId);

                if (answer == null)
                    throw new NotFoundException("No answer entity exists with the specified ID.");

                // Set the state of the answer to UnderReview - employee needs to process the feedback given by the user
                answer.AnswerState = AnswerState.UnderReview;

                // Get the question entity from the context
                QuestionEntity question = context.Questions
                                          .Include(q => q.User)
                                          .Include(q => q.User.Sources)
                                          .Include(q => q.Source)
                                          .Include(q => q.LanguageDefinition)
                                          .Include(q => q.Answer)
                                          .Include(q => q.Answer.LanguageDefinition)
                                          .Include(q => q.Answer.User)
                                          .Include(q => q.Answer.User.Sources)
                                          .Include(q => q.Answerer)
                                          .Include(q => q.Answerer.Sources)
                                          .SingleOrDefault(q => q.Id == questionId);

                if (question == null)
                    throw new NotFoundException("No question entity exists with the specified ID.");

                // Check if the user who asked the question is the one to posted the feedback and make sure feedback is
                // only given once.
                if (question.FeedbackToken != feedbackToken)
                    throw new InvalidOperationException(
                        "Feedback can only be given once by the user who asked the question.");
                
                // Set the state of the question to Open - employee needs to process the feedback given by the user
                question.QuestionState = QuestionState.Open;
                // Reset token so feedback can only be given once.
                question.FeedbackToken = null;

                // Store the user's feedback for the given answer
                FeedbackEntity feedbackEntity = new FeedbackEntity();
                feedbackEntity.Question = question;
                feedbackEntity.Answer = answer;
                feedbackEntity.Content = feedback;
                feedbackEntity.CreationTime = DateTime.UtcNow;
                feedbackEntity.FeedbackState = FeedbackState.Open;
                feedbackEntity.FeedbackType = feedbackType;
                feedbackEntity.User = question.User;
                context.Feedbacks.Add(feedbackEntity);

                // Save the changes to the context
                context.SaveChanges();
            }
        }