public IHttpActionResult AddMarkToStudent(string studentId, int subjectId, string teacherId, int markValue) { Teacher teacher = teachersService.GetById(teacherId); Student student = studentsService.GetById(studentId); if (teacher == null || student == null || subjectsService.GetById(subjectId) == null) { return(NotFound()); } try { if (!teacher.TeacherTeachesSubject.Select(x => x.Subject.SubjectId).Contains(subjectId)) { throw new NullReferenceException("Teacher doesn't teach this subject!"); } else if (!student.StudentAttendsSubject.Select(x => x.TacherTeachesSubject?.Teacher).Contains(teacher)) { throw new NullReferenceException("Teacher doesn't teach this subject to provided student!"); } else { logger.Info("Adding mark to student"); Mark mark = teachersService.AddMarkToStudent(studentId, subjectId, teacherId, markValue); return(Ok(mark)); } } catch (NullReferenceException e) { logger.Error(e.Message, "Adding mark to student"); return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message))); } }