public static Grade GradeCreateAndEditDTOToGrade(GradeCreateAndEditDTO dto) { Grade retVal = new Grade(); retVal.Id = dto.Id; retVal.Value = dto.Value; retVal.DateOfGrading = DateTime.Now; retVal.Student.Id = dto.StudentId; //retVal. logger.Info("Converting GradeCreateAndEditDTO to Grade."); return(retVal); }
public IHttpActionResult EditGradeAdmin(int id, string teacherId, [FromBody] GradeCreateAndEditDTO dto) { if (!ModelState.IsValid) { logger.Warn("Bad model state."); return(BadRequest()); } try { GradeDTO retVal = service.EditGrade(id, teacherId, dto); logger.Info("Admin edited grade. status OK"); return(Ok(retVal)); } catch (Exception e) { return(BadRequest(e.Message)); } }
public IHttpActionResult EditGradeTeacher(int id, [FromBody] GradeCreateAndEditDTO dto) { string teacherId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value; if (!ModelState.IsValid) { logger.Warn("Bad model state."); return(BadRequest()); } try { GradeDTO retVal = service.EditGrade(id, teacherId, dto); logger.Info("Teacher edited grade. status OK"); return(Ok(retVal)); } catch (Exception e) { return(BadRequest(e.Message)); } }
public GradeDTO EditGrade(int id, string teacherId, GradeCreateAndEditDTO dto) { Grade grade = db.GradesRepository.GetByID(id); Teacher teacher = db.TeachersRepository.GetByID(teacherId); Student student = db.StudentsRepository.GetByID(dto.StudentId); SchoolSubject subject = db.SchoolSubjectsRepository.GetByID(dto.SchoolSubjectId); SchoolClass sc = db.SchoolClassesRepository.GetByID(student.SchoolClass.Id); if (teacher == null) { logger.Warn("Teacher with id {0} not found.", teacherId); throw new KeyNotFoundException("teacherId doesn't exist."); } if (student == null) { logger.Warn("Student with id {0} not found.", dto.StudentId); throw new KeyNotFoundException("dto.StudentId doesn't exist."); } if (subject == null) { logger.Warn("Subject with id {0} not found.", dto.SchoolSubjectId); throw new KeyNotFoundException("dto.SchoolSubjectId doesn't exist."); } if (!teacher.TeacherSchoolSubjects.Select(x => x.SchoolSubject).Any(y => y.Id == subject.Id)) { logger.Warn("Teacher {0} {1} (id: {2}) does not teach subject {3} (id: {4}). Cannot grade the student.", teacher.FirstName, teacher.LastName, teacher.Id, subject.Name, subject.Id); throw new Exception("Teacher doesn't teach the given subject."); //TODO 99: fix exceptions! } if (!teacher.TeacherSchoolSubjects.Any(x => x.SchoolClassTeacherSchoolSubjects.Any(y => y.SchoolClass.Id == sc.Id))) { logger.Warn("Teacher {0} {1} (id: {2}) doesn't teach the subject {3} (id: {4}) to student {5} {6} (id: {7}). Cannot grade the student.", teacher.FirstName, teacher.LastName, teacher.Id, subject.Name, subject.Id, student.FirstName, student.LastName, student.Id); throw new Exception("Teacher doesn't teach the subject to the given student"); } TeacherSchoolSubject tss = db.TeacherSchoolSubjectSRepository.Get() .Where(x => x.SchoolSubject.Id == subject.Id && x.Teacher.Id == teacher.Id).FirstOrDefault(); if (tss == null) { logger.Warn("TeacherSchoolSubject combination with teacherId {0} and subject id {1} not found.", teacher.Id, subject.Id); throw new KeyNotFoundException("Given TeacherSchoolSubject combination doesn't exist. Please make one."); } SchoolClassTeacherSchoolSubject sctss = db.SchoolClassTeacherSchoolSubjectRepository.Get() .Where(x => x.TeacherSchoolSubject.Id == tss.Id && x.SchoolClass.Id == sc.Id).FirstOrDefault(); if (sctss == null) { logger.Warn("SchoolClassTeacherSchoolSubject combination with schoolClassId {0} and teacherSchoolSubjectId {1} not found.", sc.Id, tss.Id); throw new KeyNotFoundException("Given SchoolClassTeacherSchoolSubject combination doesn't exist. Please make one."); } grade.Value = dto.Value; grade.DateOfGrading = DateTime.Now; grade.Student = student; grade.SchoolClassTeacherSchoolSubject = sctss; logger.Info("Updating grade"); db.GradesRepository.Update(grade); db.Save(); return(GradeToGradeDTOConverters.GradeToGradeDTO(grade)); }
public GradeDTO CreateGrade(string teacherId, GradeCreateAndEditDTO dto) { Teacher teacher = db.TeachersRepository.GetByID(teacherId); Student student = db.StudentsRepository.GetByID(dto.StudentId); SchoolSubject subject = db.SchoolSubjectsRepository.GetByID(dto.SchoolSubjectId); SchoolClass sc = db.SchoolClassesRepository.GetByID(student.SchoolClass.Id); if (teacher == null) { logger.Warn("Teacher with id {0} not found.", teacherId); throw new KeyNotFoundException("teacherId doesn't exist."); } if (student == null) { logger.Warn("Student with id {0} not found.", dto.StudentId); throw new KeyNotFoundException("dto.StudentId doesn't exist."); } if (subject == null) { logger.Warn("Subject with id {0} not found.", dto.SchoolSubjectId); throw new KeyNotFoundException("dto.SchoolSubjectId doesn't exist."); } if (!teacher.TeacherSchoolSubjects.Select(x => x.SchoolSubject).Any(y => y.Id == subject.Id)) { logger.Warn("Teacher {0} {1} (id: {2}) does not teach subject {3} (id: {4}). Cannot grade the student.", teacher.FirstName, teacher.LastName, teacher.Id, subject.Name, subject.Id); throw new Exception("Teacher doesn't teach the given subject."); //TODO 99: fix exceptions! } if (!teacher.TeacherSchoolSubjects.Any(x => x.SchoolClassTeacherSchoolSubjects.Any(y => y.SchoolClass.Id == sc.Id))) { logger.Warn("Teacher {0} {1} (id: {2}) doesn't teach the subject {3} (id: {4}) to student {5} {6} (id: {7}). Cannot grade the student.", teacher.FirstName, teacher.LastName, teacher.Id, subject.Name, subject.Id, student.FirstName, student.LastName, student.Id); throw new Exception("Teacher doesn't teach the subject to the given student"); } TeacherSchoolSubject tss = db.TeacherSchoolSubjectSRepository.Get() .Where(x => x.SchoolSubject.Id == subject.Id && x.Teacher.Id == teacher.Id).FirstOrDefault(); if (tss == null) { logger.Warn("TeacherSchoolSubject combination with teacherId {0} and subject id {1} not found.", teacher.Id, subject.Id); throw new KeyNotFoundException("Given TeacherSchoolSubject combination doesn't exist. Please make one."); } SchoolClassTeacherSchoolSubject sctss = db.SchoolClassTeacherSchoolSubjectRepository.Get() .Where(x => x.TeacherSchoolSubject.Id == tss.Id && x.SchoolClass.Id == sc.Id).FirstOrDefault(); if (sctss == null) { logger.Warn("SchoolClassTeacherSchoolSubject combination with schoolClassId {0} and teacherSchoolSubjectId {1} not found.", sc.Id, tss.Id); throw new KeyNotFoundException("Given SchoolClassTeacherSchoolSubject combination doesn't exist. Please make one."); } Grade grade = new Grade() { Value = dto.Value, DateOfGrading = DateTime.Now, SchoolClassTeacherSchoolSubject = sctss, Student = student }; logger.Info("Creating grade. teacherId: {0}, studentId: {1}, subjectId: {2}", teacher.Id, student.Id, subject.Id); db.GradesRepository.Insert(grade); db.Save(); string toEmail = student.Parent.Email; string parentName = student.Parent.FirstName + " " + student.Parent.LastName; string studentName = student.FirstName + " " + student.LastName; string date = grade.DateOfGrading.Day + "." + grade.DateOfGrading.Month + "." + grade.DateOfGrading.Year; string subjectName = subject.Name; int gradeValue = grade.Value; EmailSenders.EmailGradingEventToParent(toEmail, parentName, studentName, date, subjectName, gradeValue); return(GradeToGradeDTOConverters.GradeToGradeDTO(grade)); }