public static async Task SeedAsync(SurveyContext context, ILoggerFactory loggerFactory) { try { if (!context.SurveyTypes.Any()) { var surveyTypesData = File.ReadAllText("../Infrastructure/Data/SeedData/surveyTypes.json"); var surveyTypes = JsonSerializer.Deserialize <List <SurveyType> >(surveyTypesData); foreach (var item in surveyTypes) { context.SurveyTypes.Add(item); } await context.SaveChangesAsync(); } if (!context.Surveys.Any()) { var surveysData = File.ReadAllText("../Infrastructure/Data/SeedData/surveys.json"); var surveys = JsonSerializer.Deserialize <List <Survey> >(surveysData); foreach (var item in surveys) { context.Surveys.Add(item); } await context.SaveChangesAsync(); } if (!context.Questions.Any()) { var questionsData = File.ReadAllText("../Infrastructure/Data/SeedData/questions.json"); var questions = JsonSerializer.Deserialize <List <Question> >(questionsData); foreach (var item in questions) { context.Questions.Add(item); } await context.SaveChangesAsync(); } if (!context.Options.Any()) { var optionsData = File.ReadAllText("../Infrastructure/Data/SeedData/options.json"); var options = JsonSerializer.Deserialize <List <Option> >(optionsData); foreach (var item in options) { context.Options.Add(item); } await context.SaveChangesAsync(); } } catch (Exception ex) { var logger = loggerFactory.CreateLogger <SurveyContextSeed>(); logger.LogError(ex.Message); } }
public async Task <bool> AddSurveyResponse(Survey survey, SurveyResponseDto surveyResponse) { var answers = new List <Answer>(); foreach (var question in survey.Questions) { var responseAnswer = surveyResponse.Answers.FirstOrDefault(a => a.QuestionId == question.Id); if (responseAnswer != null) { var answer = new Answer { Question = question, QuestionId = question.Id, QuestionAnswer = responseAnswer.QuestionAnswer }; answers.Add(answer); } } var response = new Response { Email = surveyResponse.Email, Name = surveyResponse.Name, Survey = survey, SurveyId = survey.Id, Answers = answers }; _context.SurveyResponses.Add(response); var saved = await _context.SaveChangesAsync(); return(saved > 0); }