public async Task <ActionResult> Index(string id, SurveyResponseViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var existingUser = await _userDataRepo.Get(id); if (!string.IsNullOrEmpty(model.ScoreboardName)) { existingUser = existingUser ?? new UserData(); if (!string.IsNullOrWhiteSpace(existingUser.HashedPassword) && !BCrypt.Net.BCrypt.Verify(model.Password, existingUser.HashedPassword)) { ModelState.AddModelError("Password", "A username has already been set for this user, and the password you entered does not match. Please try again."); return(View(model)); } existingUser.ScoreboardName = model.ScoreboardName; existingUser.UserId = id; existingUser.HashedPassword = BCrypt.Net.BCrypt.HashPassword(model.Password); await _userDataRepo.Save(existingUser); } var surveyResponse = Mapper.Map <SurveyResponse>(model); surveyResponse.UserId = id; await _surveyResponseRepo.Save(surveyResponse); ViewData["Success"] = true; return(View(model)); }
public ActionResult SurveyResponses(int Id) { using (MovingTacticsEntities db = new MovingTacticsEntities()) { List <SurveyResponseViewModel> model = new List <SurveyResponseViewModel>(); var query = (from survey in db.Survey join surveyResponses in db.SurveyResponses on survey.SurveyId equals surveyResponses.SurveyId join member in db.Members on surveyResponses.MemberId equals member.MemberId where survey.SurveyId == Id select new { survey.SurveyTitle, member.MemberName, surveyResponses.DateCreated, surveyResponses.TimeCreated, surveyResponses.SurveyResponseId }).ToList(); foreach (var item in query) { SurveyResponseViewModel responseModel = new SurveyResponseViewModel { SurveyResponseId = item.SurveyResponseId, Survey = item.SurveyTitle, Member = item.MemberName, DateCreated = item.DateCreated, TimeCreated = item.TimeCreated }; model.Add(responseModel); } return(View(model)); } }
public IHttpActionResult PostSurveyResponse(SurveyResponseViewModel surveyResponse) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //surveyResponse.Id = System.Guid.NewGuid(); foreach (var resp in surveyResponse.Responses) { var response = new SurveyResponse { SurveyId = surveyResponse.SurveyId, UserId = surveyResponse.UserId, QuestionId = resp.QuestionId, ResponseText = resp.ResponseText, ResponseReceivedTime = resp.ResponseReceivedTime }; db.SurveyResponses.Add(response); } //Save responses try { db.SaveChanges(); } catch (DbUpdateException) { if (SurveyResponseExists(surveyResponse.Id)) { return(Conflict()); } else { throw; } } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } return(CreatedAtRoute("DefaultApi", new { id = surveyResponse.Id }, surveyResponse)); }
public JsonResult JsonUpdateSurveyResponse(SurveyResponseViewModel responseViewModel) { // Convert from view model var surveyResponse = ToSurveyResponse(responseViewModel); // Add audit info surveyResponse.UserID = AssistedUser.UserID; surveyResponse.ModifyUserID = CurrentUser.UserID; surveyResponse.ModifyDate = DateTime.Now; surveyResponse.ModifyDateUtc = DateTime.UtcNow; if (surveyResponse.CreateUserID <= 0) { surveyResponse.CreateUserID = CurrentUser.UserID; } // Get 'previous' version var previousSurvey = GetSurveyForUser(surveyResponse.SurveyName); // Save 'new' version var response = _surveyService.SaveSurveyResponse(ref surveyResponse); // If save was successful, return fresh copy of the survey var surveyViewModel = default(SurveyViewModel); if (response.Success) { var currentSurvey = GetSurveyForUser(surveyResponse.SurveyName); var notification = Using <NotificationFactory>().CreateNotification(currentSurvey.SurveyNotificationType, previousSurvey, currentSurvey, GetAssistedUser()); if (notification != null) { notification.SummaryUrl = Url.Action("Print", "Survey", new { currentSurvey.SurveyName, AssistedUser.UserID }, Request.Url.Scheme); notification.Send(); } surveyViewModel = ToSurveyViewModel(currentSurvey); } return(Json(new { success = response.Success, message = response.Message, validationErrors = response.Result, survey = surveyViewModel })); }
/// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="destination"></param> /// <param name="destMember"></param> /// <param name="context"></param> /// <returns></returns> public List <Dictionary <string, object> > Resolve(SurveyResponse source, SurveyResponseViewModel destination, List <Dictionary <string, object> > destMember, ResolutionContext context) { List <Dictionary <string, object> > responseValues = new List <Dictionary <string, object> >(); foreach (var response in source.ResponseValues) { var obj = response.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(f => { return(f.PropertyType != typeof(SurveyResponse) && f.Name != "Id"); }) .ToDictionary(prop => NamesContractResolver.GetResolvedPropertyName(prop.Name), prop => prop.GetValue(response, null)); responseValues.Add(obj); } return(responseValues); }
public async Task <ActionResult> Index(string id) { if (string.IsNullOrWhiteSpace(id)) { return(RedirectToAction("Index", "Home")); } UserId = id; var existingUser = await _userDataRepo.Get(id); var model = new SurveyResponseViewModel(); if (existingUser != null) { model.ScoreboardName = existingUser.ScoreboardName; } return(View(model)); }
public JsonResult JsonProcessSurveyTrigger(SurveyResponseViewModel responseViewModel) { // Convert from view model var surveyResponse = ToSurveyResponse(responseViewModel); // Get survey with this response applied var survey = _surveyService.GetSurvey(new SurveyRequest { SurveyName = surveyResponse.SurveyName, UserID = AssistedUser.UserID, IncludeResponse = false, ApplyResponse = true, ResponseToApply = surveyResponse }); // Convert to our standard View Model var viewModel = ToSurveyViewModel(survey); // Now, let's trim this down by dynamically generating a new object that contains only the properties we need var lightWeight = new { Pages = viewModel.Pages.Select(p => new { p.Id, p.IsVisible, Questions = p.Questions.Select(q => new { q.Id, q.Number, q.IsVisible, PossibleAnswers = q.PossibleAnswers.Select(a => new { a.Id, a.IsSelected }).ToList() }).ToList() }).ToList() }; return(Json(lightWeight)); }
public static SurveyResponse ToSurveyResponse(SurveyResponseViewModel viewModel) { var surveyResponse = new SurveyResponse() { SurveyID = viewModel.SurveyId, SelectedSurveyPageID = viewModel.SelectedPageId, SurveyName = viewModel.SurveyName, ApplyAsComplete = viewModel.ApplyAsComplete }; if (!viewModel.Answers.IsNullOrEmpty()) { surveyResponse.Answers = viewModel.Answers.Select(a => new SurveyResponseAnswer() { SurveyQuestionID = a.SurveyQuestionId, SurveyPageID = a.SurveyPageId, Answer = a.Answer }).ToList(); } return(surveyResponse); }