public IActionResult TakeSurvey(SurveyDefinitionModel payload) { try { var takenSurvey = new TakenSurveyModel(); takenSurvey.SurveyDefinitionModel = new SurveyDefinitionModel { Id = payload.Id, Name = payload.Name, Questions = new List <QuestionModel>() }; // do a null check for answers if (payload.Questions.Any(q => q.Answer == null)) { return(BadRequest("test")); } // add multiple records with AddRange instead of looping through takenSurvey.SurveyDefinitionModel.Questions.AddRange(payload.Questions); this._storageProxy.SaveTakenSurvey(takenSurvey); return(Ok(takenSurvey)); } catch (Exception ex) { // TODO: Log error Console.WriteLine("Exception: " + ex); return(BadRequest($"Error updating the {payload.Name} survey.")); } }
public void SaveTakenSurvey(TakenSurveyModel takenSurvey) { dbContext.TakenSurveys.Add(takenSurvey); dbContext.SaveChanges(); return; }