public TakeSurveyViewModel Map(TakeSurveyViewModel responses) { Survey survey = _surveyRepository.GetSurvey(responses.SurveyId); TakeSurveyViewModel viewModel = new TakeSurveyViewModel(); // Map the survey to the survey view model. viewModel.SurveyId = survey.SurveyId; viewModel.Title = survey.Title; viewModel.UserName = survey.User.UserName; viewModel.StatusDate = string.Format("{0:d}", survey.StatusDate); viewModel.CategoryDescription = survey.Category.Description; // Map the questions to the view model viewModel.Questions = new List<SurveyQuestionsViewModel>(); //foreach (Core.Model.Question q in survey.Questions) foreach (SurveyQuestionsViewModel q in responses.Questions) { // Get the question and available responses from the repository. Core.Model.Question questionInfo = _questionRepository.GetQuestion(Convert.ToInt64(q.QId_SeqNo.Split('_')[0])); SurveyQuestionsViewModel questionViewModel = new SurveyQuestionsViewModel(); questionViewModel.QId_SeqNo = q.QId_SeqNo; questionViewModel.SequenceNumber = questionInfo.SequenceNumber; questionViewModel.Text = questionInfo.Text; questionViewModel.Answer = q.Answer; // Map the responses to the question questionViewModel.Responses = new List<SurveyResponsesViewModel>(); // this causes a DBContext Disposed error if it's visited a 2nd time. foreach (Core.Model.AvailableResponse r in questionInfo.AvailableResponses) { SurveyResponsesViewModel responsesViewModel = new SurveyResponsesViewModel(); responsesViewModel.QId_SeqNo = q.QId_SeqNo; responsesViewModel.Answer = q.Answer.ToString(); responsesViewModel.LikertScaleNumber = r.LikertScaleNumber; responsesViewModel.Text = r.Text; questionViewModel.Responses.Add(responsesViewModel); } viewModel.Questions.Add(questionViewModel); } return viewModel; }
public Survey Map(TakeSurveyViewModel Responses) { // Update the results of the survey in the db var surveyWithResults = _surveyRepository.GetSurvey(Responses.SurveyId); // create ne respondent Respondent respondent = _respondentFactory.Create(); respondent.Survey = surveyWithResults; // add all the questions and answers to the actual resonses foreach (SurveyQuestionsViewModel q in Responses.Questions) { var answer = _actualResponseFactory.Create(); answer.Question = Convert.ToInt64(q.QId_SeqNo.Split('_')[0]); answer.Response = Convert.ToInt64(q.Answer); answer.Respondent = respondent; // Add answer to actual responses respondent.Responses.Add(answer); } // Now dd the respondent and the anwsers to the survey surveyWithResults.Respondents.Add(respondent); return surveyWithResults; }
/// <summary> /// Map the Survey to the TakeSurveyViewModel. /// </summary> /// <remarks> /// This mapping is done explicitly as the target structure is quite complex /// and the amount of exceptions required if AutoMapper were used would not /// have a significant effect on the amount of code. /// Additionally, the top level represents a single survey, but the /// lower levels represent collections of objects. /// </remarks> public override void OnActionExecuted(ActionExecutedContext filterContext) { Survey survey = (Survey) filterContext.Controller.ViewData.Model; TakeSurveyViewModel viewModel = new TakeSurveyViewModel(); // Map the survey to the survey view model. viewModel.SurveyId = survey.SurveyId; viewModel.Title = survey.Title; viewModel.UserName = survey.User.UserName; viewModel.StatusDate = string.Format("{0:d}", survey.StatusDate); viewModel.CategoryDescription = survey.Category.Description; // Map the questions to the view model viewModel.Questions = new List<SurveyQuestionsViewModel>(); foreach (Core.Model.Question q in survey.Questions) { SurveyQuestionsViewModel questionViewModel = new SurveyQuestionsViewModel(); questionViewModel.QId_SeqNo = q.QuestionId.ToString() + "_" + q.SequenceNumber.ToString(); questionViewModel.SequenceNumber = q.SequenceNumber; questionViewModel.Text = q.Text; // Map the responses to the question questionViewModel.Responses = new List<SurveyResponsesViewModel>(); foreach (Core.Model.AvailableResponse r in q.AvailableResponses) { SurveyResponsesViewModel responsesViewModel = new SurveyResponsesViewModel(); responsesViewModel.QId_SeqNo = questionViewModel.QId_SeqNo; responsesViewModel.LikertScaleNumber = r.LikertScaleNumber; responsesViewModel.Text = r.Text; questionViewModel.Responses.Add(responsesViewModel); } viewModel.Questions.Add(questionViewModel); } filterContext.Controller.ViewData.Model = viewModel; }
/// <summary> /// Returns a populated TakeSurveyViewModel class /// </summary> /// <returns></returns> public TakeSurveyViewModel SetTakeSurveyViewModel_3() { TakeSurveyViewModel vM = new TakeSurveyViewModel(); vM.CategoryDescription = null; vM.StatusDate = null; vM.SurveyId = 3; vM.Title = null; vM.UserName = null; vM.Questions = new List<SurveyQuestionsViewModel>(); // Add response to question1 SurveyQuestionsViewModel response1 = new SurveyQuestionsViewModel(); response1.Answer = "5"; response1.QId_SeqNo="1_1"; response1.SequenceNumber = 0; response1.Responses = null; response1.Text=null; vM.Questions.Add(response1); // Add response to question 2 SurveyQuestionsViewModel response2 = new SurveyQuestionsViewModel(); response2.Answer = "5"; response2.QId_SeqNo="1_2"; response2.SequenceNumber = 0; response2.Responses = null; response2.Text=null; vM.Questions.Add(response2); // Return it return vM; }
public void TakeRedirectsToThankYouWithResultsOnSuccess() { // Arrange // Instantiate the controller, with the necessary mocked dependencies. HomeController controller = ArrangeController(); // Input: This can be null as we don't test the fields in the mapping, this is mocked instead. TakeSurveyViewModel viewModel = new TakeSurveyViewModel(); // Act RedirectToRouteResult result = controller.Take(viewModel) as RedirectToRouteResult; // Assert // Check result is actually returned. Assert.IsNotNull(result, "No redirect result returned"); // Check the route values, not the Controller, it's defaulted to the current: Home. Assert.AreEqual("ThankYou", result.RouteValues["action"], "Expected the route action to be 'ThankYou'"); Assert.AreEqual(3L, result.RouteValues["id"], "Expected the Id to be '3'"); }
public void TakeReturnsViewIfModelInvalid() { // Arrange // Instantiate the controller, with the necessary mocked dependencies. HomeController controller = ArrangeController(); // Input: This can be null as we don't test the fields in the mapping, this is mocked instead. TakeSurveyViewModel viewModel = new TakeSurveyViewModel(); // Introduce an error to the modelstate so a test to return the view is possible controller.ModelState.AddModelError("", "An error condition to set the modelstate.isvalid to false"); ViewResult result = controller.Take(viewModel) as ViewResult; // Assert // Check a review is actually returned Assert.IsNotNull(result, "Expected a view to be returned"); // Check the viewName is "" Assert.AreEqual("", result.ViewName, "incorrect view returned"); // Check the model type Assert.IsInstanceOfType(result.ViewData.Model, typeof(TakeSurveyViewModel), "Incorrect ViewData Model returned"); // Check the valud of the surveyid TakeSurveyViewModel model = (TakeSurveyViewModel)result.ViewData.Model; Assert.AreEqual(3L, model.SurveyId, "Incorrect data returned in the ViewData model"); }
public ActionResult Take(TakeSurveyViewModel Responses) { if (ModelState.IsValid) { // Map the responses to the Survey. Survey surveyWithResults = _takeSurveyViewModelMapper.Map(Responses); // Now update the DbContext Survey[] surveys = new Survey[] { surveyWithResults }; _surveyRepository.UpdateSurveys(surveys); // Display the Thank you page. return RedirectToAction("ThankYou", new { id = surveyWithResults.SurveyId }); } // An error, so reconstruct the survey details and return to the view TakeSurveyViewModel viewModel = _reinstateTakeWurveyViewModelMapper.Map(Responses); return View(viewModel); }