public IImmutableList <ValidationResult> SaveCompletedQuestion(CompletedSurveyDto dto) { if (dto.Questions == null) { Status.AddError("No Questions have been submitted with this completed survey."); return(Status.Errors); } var completedSurvey = _context.Find <CompletedSurvey>(dto.CompletedSurveyId); if (completedSurvey == null) { Status.AddError($"Could not find Survey with an Id of {dto.CompletedSurveyId}"); return(Status.Errors); } var completedQuestions = _mapper.MapQuestionsFromDto(dto.Questions, _context); foreach (var question in completedQuestions) { Status = completedSurvey.AddQuestion(question, _context); } if (Status.HasErrors) { return(Status.Errors); } _context.SaveChanges(); return(null); }
public void TestSaveSurveyNoQuestions() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentCompletedSurveys = context.CompletedSurveys.Count(); var completedQuestionsDto = EfTestData.CreateCompletedQuestionsDto(); var completedSurveyDto = new CompletedSurveyDto { Name = "Test Completed Survey", CaseNo = 999 }; var service = new SaveCompletedSurveysService(context, new MapCompletedQuestionsFromDtoService()); var result = service.SaveCompletedSurvey(completedSurveyDto); context.SaveChanges(); result.ShouldNotBeNull(); result.First().ErrorMessage.ShouldEqual("No Questions have been submitted with this completed survey."); context.CompletedSurveys.Count().ShouldEqual(currentCompletedSurveys); } }
public void TestSaveSurveyNoName() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentCompletedSurveys = context.CompletedSurveys.Count(); var completedQuestionsDto = EfTestData.CreateCompletedQuestionsDto(); var completedSurveyDto = new CompletedSurveyDto { CaseNo = 999, Questions = completedQuestionsDto }; var service = new SaveCompletedSurveysService(context, new MapCompletedQuestionsFromDtoService()); var result = service.SaveCompletedSurvey(completedSurveyDto); context.SaveChanges(); result.ShouldNotBeNull(); service.Errors.Count.ShouldEqual(1); service.Errors.First().ErrorMessage.ShouldEqual("A survey name has not been supplied."); context.CompletedSurveys.Count().ShouldEqual(currentCompletedSurveys); } }
public void TestSaveSurveyOk() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentCompletedSurveys = context.CompletedSurveys.Count(); var completedQuestionsDto = EfTestData.CreateCompletedQuestionsDto(); var completedSurveyDto = new CompletedSurveyDto { Name = "Test Completed Survey", CaseNo = 999, Questions = completedQuestionsDto }; var service = new SaveCompletedSurveysService(context, new MapCompletedQuestionsFromDtoService()); var result = service.SaveCompletedSurvey(completedSurveyDto); result.ShouldBeNull(); context.SaveChanges(); service.Errors.Count.ShouldEqual(0); context.CompletedSurveys.Count().ShouldEqual(currentCompletedSurveys + 1); } }
public void ShouldReturnErrorMessageWhenSurveyNotFound() { var options = SqliteInMemory.CreateOptions<SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentCompletedQuestions = context.CompletedQuestions.Count(); var completedSurveyId = -1; var completedSurveyDto = new CompletedSurveyDto { CompletedSurveyId = completedSurveyId, Name = "Test Completed Survey", CaseNo = 999, Questions = completedQuestionsDto }; var service = new SaveCompletedQuestionToCompletedSurveyService(context, new MapCompletedQuestionsFromDtoService()); var result = service.SaveCompletedQuestion(completedSurveyDto); result.ShouldNotBeNull(); context.SaveChanges(); result.First().ErrorMessage.ShouldEqual($"Could not find Survey with an Id of {completedSurveyId}"); context.CompletedQuestions.Count().ShouldEqual(currentCompletedQuestions); } }
public IImmutableList <ValidationResult> SaveCompletedSurvey(CompletedSurveyDto dto) { if (dto.Questions == null) { Status.AddError("No Questions have been submitted with this completed survey."); return(Status.Errors); } var completedQuestions = _mapper.MapQuestionsFromDto(dto.Questions, _context); var completedSurveyDto = new SaveCompletedSurveyDto(dto.Name, dto.CaseNo, completedQuestions); _runner.RunAction(completedSurveyDto); return(_runner.HasErrors ? _runner.Errors : null); }
public void ShouldReturnErrorMessageWhenNoAnswerProvided() { var options = SqliteInMemory.CreateOptions<SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentCompletedQuestions = context.CompletedQuestions.Count(); var completedQuestionsDto = new List<CompletedQuestionDto> { new CompletedQuestionDto { QuestionId = 1, } }; var completedSurveyDto = new CompletedSurveyDto { CompletedSurveyId = 1, Name = "Test Completed Survey", CaseNo = 999, Questions = completedQuestionsDto }; var service = new SaveCompletedQuestionToCompletedSurveyService(context, new MapCompletedQuestionsFromDtoService()); var result = service.SaveCompletedQuestion(completedSurveyDto); result.ShouldNotBeNull(); context.SaveChanges(); result.First().ErrorMessage.ShouldEqual("An answer is needed when submitting a question."); context.CompletedQuestions.Count().ShouldEqual(currentCompletedQuestions); } }
public JsonResult SaveCompletedSurvey(CompletedSurveyDto dto, [FromServices] ISaveCompletedSurveyService service) { var status = service.SaveCompletedSurvey(dto); return(status == null ? new JsonResult("Ok") : new JsonResult(status.ToList())); }