/// <summary> /// Did the least refactoring required to make the method easily testable /// To get max possible cases out for testing e.g. repo failure. /// and still would be able to see if the student has passed the test or not. /// </summary> /// <param name="lessonId"></param> /// <param name="grade"></param> /// <param name="isPassed"></param> /// <returns></returns> public string UpdateLessonGrade(int lessonId, double grade, out bool isPassed) { isPassed = false; var lessonRepo = new LessonRepository(); var lesson = lessonRepo.GetLesson(lessonId); if (lesson == null) { return("Lesson not Found!"); } lesson.Grade = grade; if (!lesson.IsPassed) { var moduleRepository = new ModuleRepository(); var module = moduleRepository.GetModule(lessonId); if (module == null) { return("Module not Found!"); } if (grade >= module.MinimumPassingGrade) { lesson.IsPassed = true; } else { lesson.IsPassed = false; } } isPassed = lesson.IsPassed; return("Success"); }
public ServiceResponse UpdateLessonGrade(int lessonId, double grade) { var response = new ServiceResponse(); try { if (!IsGradeValid(grade)) { // I would not set hard-coded strings like this, but I didn't // want to exhaust every little detail for this exercise response.Message = "Grade is invalid."; response.ResponseCode = Enums.LessonServiceCodes.InvalidGrade; return(response); } var lessonRepo = new LessonRepository(); var lesson = lessonRepo.GetLesson(lessonId); if (!IsLessonValid(lesson)) { // again, I would not hard-code strings like this response.Message = "Lesson was not found for the given Lesson Id"; response.ResponseCode = Enums.LessonServiceCodes.InvalidLessonId; return(response); } lesson.Grade = grade; if (!lesson.IsPassed) { var moduleRepository = new ModuleRepository(); var module = moduleRepository.GetModule(lessonId); if (!IsModuleValid(module)) { response.Message = "Module was not found for the given Lesson Id"; response.ResponseCode = Enums.LessonServiceCodes.InvalidLessonIdForModule; return(response); } lesson.IsPassed = (grade >= module.MinimumPassingGrade); } response.ResponseCode = Enums.LessonServiceCodes.UpdateSuccess; } catch (Exception ex) { response.ResponseCode = Enums.LessonServiceCodes.Exception; response.Exception = ex; } return(response); }
public new void NavigateToScreen(string moduleKey) { try { CurrentScreen = ModuleRepository.GetModule(moduleKey); AdminConsoleViewModel.CurrentModuleKey = moduleKey; _eventAggregator.Publish(AdminConsoleViewModel); } catch (Exception ex) { _logger.ErrorFormat("Can't navigate to screen {0}", moduleKey, ex); } }
public void UpdateLessonGrade_Test() { var moduleRepo = new ModuleRepository(); var lessonService = new LessonService(); var module = moduleRepo.GetModule(lessonId); var grade = new Random().Next(0, 100); var isPassing = module.MinimumPassingGrade <= grade; try { var updatedLesson = lessonService.UpdateLessonGrade(lessonId, grade); Assert.AreEqual(updatedLesson.Grade, grade); Assert.AreEqual(updatedLesson.IsPassed, isPassing); } catch (Exception e) { Console.WriteLine(e); } }
public void UpdateLessonGrade(int lessonId, double grade) { var lesson = lessonRepo.GetLesson(lessonId); lesson.Grade = grade; if (!lesson.IsPassed) { var moduleRepository = new ModuleRepository(); var module = moduleRepository.GetModule(lessonId); if (grade >= module.MinimumPassingGrade) { lesson.IsPassed = true; } else { lesson.IsPassed = false; } } }
public Lesson UpdateLessonGrade(int lessonId, double grade) { if (grade < 0 || grade > 100) { throw new ArgumentException("Grade should be greater than or equal to zero, and less than 100", nameof(grade)); } var lessonRepo = new LessonRepository(); var lesson = lessonRepo.GetLesson(lessonId); if (lesson == null) { throw new Exception($"Could not find lesson with lessonId: {lessonId}"); } lesson.Grade = grade; if (!lesson.IsPassed) { var moduleRepository = new ModuleRepository(); var module = moduleRepository.GetModule(lessonId); if (module == null) { throw new Exception($"Could not find module with lessonId: {lessonId}"); } if (grade >= module.MinimumPassingGrade) { lesson.IsPassed = true; } else { lesson.IsPassed = false; } } return(lesson); }
public void UpdateLessonGrade(int lessonId, double grade) { var lessonRepo = new LessonRepository(); var lesson = lessonRepo.GetLesson(lessonId); lesson.Grade = grade; // For consistency, we need to set the lesson's pass status // each time the grade changes //if (!lesson.IsPassed) //{ var moduleRepository = new ModuleRepository(); var module = moduleRepository.GetModule(lessonId); if (grade >= module.MinimumPassingGrade) { lesson.IsPassed = true; } else { lesson.IsPassed = false; } //} }