// Survey Answers #region public Task<bool> CreateSurveyAnswersAsync(DTOSurvey paramDTOSurvey) public Task <bool> CreateSurveyAnswersAsync(DTOSurvey paramDTOSurvey) { try { List <SurveyAnswer> SurveyAnswers = new List <SurveyAnswer>(); foreach (var SurveyItem in paramDTOSurvey.SurveyItem) { // Delete possible existing answer var ExistingAnswers = _context.SurveyAnswer .Where(x => x.SurveyItemId == SurveyItem.Id) .Where(x => x.UserId == paramDTOSurvey.UserId) .ToList(); if (ExistingAnswers != null) { _context.SurveyAnswer.RemoveRange(ExistingAnswers); _context.SaveChanges(); } // Save Answer if (SurveyItem.ItemType != "Multi-Select Dropdown") { SurveyAnswer NewSurveyAnswer = new SurveyAnswer(); NewSurveyAnswer.AnswerValue = SurveyItem.AnswerValueString; NewSurveyAnswer.AnswerValueDateTime = SurveyItem.AnswerValueDateTime; NewSurveyAnswer.SurveyItemId = SurveyItem.Id; NewSurveyAnswer.UserId = paramDTOSurvey.UserId; _context.SurveyAnswer.Add(NewSurveyAnswer); _context.SaveChanges(); } if (SurveyItem.AnswerValueList != null) { foreach (var item in SurveyItem.AnswerValueList) { SurveyAnswer NewSurveyAnswerValueList = new SurveyAnswer(); NewSurveyAnswerValueList.AnswerValue = item; NewSurveyAnswerValueList.SurveyItemId = SurveyItem.Id; NewSurveyAnswerValueList.UserId = paramDTOSurvey.UserId; _context.SurveyAnswer.Add(NewSurveyAnswerValueList); _context.SaveChanges(); } } } return(Task.FromResult(true)); } catch { DetachAllEntities(); throw; } }
// Survey Answers #region public Task<bool> CreateSurveyAnswersAsync(DTOSurvey paramDTOSurvey) public Task <bool> CreateSurveyAnswersAsync(DTOSurvey paramDTOSurvey) { try { List <SurveyAnswer> SurveyAnswers = new List <SurveyAnswer>(); foreach (var SurveyItem in paramDTOSurvey.SurveyItem) { // Delete possible existing answer var ExistingAnswer = _context.SurveyAnswer .Where(x => x.SurveyItemId == SurveyItem.Id) .Where(x => x.UserId == paramDTOSurvey.UserId) .FirstOrDefault(); if (ExistingAnswer != null) { _context.SurveyAnswer.RemoveRange(ExistingAnswer); _context.SaveChanges(); } SurveyAnswer NewSurveyAnswer = new SurveyAnswer(); NewSurveyAnswer.AnswerValue = SurveyItem.AnswerValueString; NewSurveyAnswer.AnswerValueDateTime = SurveyItem.AnswerValueDateTime; NewSurveyAnswer.SurveyItemId = SurveyItem.Id; NewSurveyAnswer.UserId = paramDTOSurvey.UserId; _context.SurveyAnswer.Add(NewSurveyAnswer); _context.SaveChanges(); } return(Task.FromResult(true)); } catch (Exception ex) { DetachAllEntities(); throw ex; } }