// ReSharper disable once UnusedMember.Global public void Delete(LessonDeleteRequest request) { Guard.IsTrue(lessonId => 0 < lessonId, request.LessonId); Guard.AgainstEmpty(request.Provider); Guard.AgainstEmpty(request.ProviderId); var existingUser = UserHelpers.GetExistingUser(request, UserRepository); Guard.IsTrue(eu => eu.IsNew == false, existingUser); Guard.AgainstEmpty(request.StudentProvider); Guard.AgainstEmpty(request.StudentProviderId); var existingStudent = UserHelpers.GetExistingUser(new LessonDeleteRequest { Provider = request.StudentProvider, ProviderId = request.StudentProviderId }, UserRepository); Guard.IsTrue(eu => eu.IsNew == false, existingStudent); Guard.IsTrue(es => es.IsStudent, existingStudent); var grade = GradeRepository.GetGrade(request.LessonId, request.StudentProvider, request.ProviderId); if (grade == null) { return; } if (existingUser.IsTeacher == false || request.Provider != grade.TeacherProvider || request.ProviderId != grade.TeacherProviderId) { throw new HttpError(HttpStatusCode.Unauthorized, "Unauthorized"); } if (grade.IsDeleted) { return; } GradeRepository.DeleteGrade(grade.Id); }
// ReSharper disable once UnusedMember.Global public IEnumerable <GroupedStudentLessonAnswerResponse> Post(StudentLessonAnswerGetAllRequest request) { Guard.AgainstEmpty(request.Provider); Guard.AgainstEmpty(request.ProviderId); var existingUser = UserHelpers.GetExistingUser(request, UserRepository); Guard.IsTrue(eu => eu.IsNew == false, existingUser); Guard.GreaterThan(0, request.LessonId, "LessonId"); Guard.IsTrue(eu => eu.IsTeacher, existingUser); var lesson = LessonRepository.Find(request.LessonId); if (request.Provider != lesson.Provider || request.ProviderId != lesson.ProviderId) { throw new HttpError(HttpStatusCode.Unauthorized, "Unauthorized"); } var studentLessonAnswers = StudentLessonAnswerRepository.GetAllByLessonId(request.LessonId); var groupedStudentLessonAnswers = studentLessonAnswers.GroupBy(g => new { g.Provider, g.ProviderId }).ToList(); var allStudents = groupedStudentLessonAnswers.Select(g => UserRepository.GetUserByProviderAndProviderId(g.Key.Provider, g.Key.ProviderId)).ToList(); var response = new List <GroupedStudentLessonAnswerResponse>(); foreach (var grouping in groupedStudentLessonAnswers) { var student = allStudents.SingleOrDefault(a => a.Provider == grouping.Key.Provider && a.ProviderId == grouping.Key.ProviderId); if (student == null || student.IsStudent == false) { ErrorLogRepository.Add(new ErrorLog { Application = "MetacognitiveTutor.Api", Message = $"No student found for Provider '{grouping.Key.Provider}', ProviderId '{grouping.Key.ProviderId}'; LessonId: '{request.LessonId}'.", Provider = request.Provider, ProviderId = request.ProviderId, }); continue; } var grade = GradeRepository.GetGrade(request.LessonId, student.Provider, student.ProviderId); response.Add(new GroupedStudentLessonAnswerResponse { LessonId = request.LessonId, BookTitle = lesson.BookTitle, Name = student.Name, Provider = student.Provider, ProviderId = student.ProviderId, ProviderPic = student.ProviderPic, StudentLessonAnswers = grouping.Select(g => new StudentLessonAnswerResponse { Id = g.Id, LessonId = g.LessonId, QuestionType = g.QuestionType, QuestionId = g.QuestionId, Question = g.Question, Answer = g.Answer }).OrderByDescending(sla => sla.QuestionType.Equals("card_pyramid", StringComparison.OrdinalIgnoreCase)).ThenByDescending(sla => sla.QuestionType.Equals("word_scramble", StringComparison.OrdinalIgnoreCase)), GradeResponse = grade == null ? new GradeResponse { IsGraded = false } : new GradeResponse { IsGraded = true, Comments = grade.Comments, Grade = grade.Grade } }); } return(response); }
// ReSharper disable once UnusedMember.Global public void Post(GradeUpsertRequest request) { Guard.IsTrue(lessonId => 0 < lessonId, request.LessonId); Guard.AgainstEmpty(request.Provider); Guard.AgainstEmpty(request.ProviderId); var existingUser = UserHelpers.GetExistingUser(request, UserRepository); Guard.IsTrue(eu => eu.IsNew == false, existingUser); Guard.AgainstEmpty(request.StudentProvider); Guard.AgainstEmpty(request.StudentProviderId); var existingStudent = UserHelpers.GetExistingUser(new LessonDeleteRequest { Provider = request.StudentProvider, ProviderId = request.StudentProviderId }, UserRepository); Guard.IsTrue(eu => eu.IsNew == false, existingStudent); Guard.IsTrue(es => es.IsStudent, existingStudent); var lesson = LessonRepository.Find(request.LessonId); if (lesson == null || lesson.IsDeleted) { throw new HttpError(HttpStatusCode.NotFound, "NotFound"); } if (request.Provider != lesson.Provider || request.ProviderId != lesson.ProviderId) { throw new HttpError(HttpStatusCode.Unauthorized, "Unauthorized"); } var grade = GradeRepository.GetGrade(request.LessonId, request.StudentProvider, request.ProviderId); if (grade == null) { grade = new GradeModel { LessonId = request.LessonId, StudentProvider = request.StudentProvider, StudentProviderId = request.StudentProviderId, TeacherProvider = request.Provider, TeacherProviderId = request.ProviderId, Grade = request.Grade, Comments = request.Comments }; GradeRepository.Add(grade); } else { grade.LessonId = request.LessonId; grade.StudentProvider = request.StudentProvider; grade.StudentProviderId = request.StudentProviderId; grade.TeacherProvider = request.Provider; grade.TeacherProviderId = request.ProviderId; grade.Grade = request.Grade; grade.Comments = request.Comments; grade.UpdateDateUtc = DateTime.UtcNow; GradeRepository.Update(grade); } }