public IHttpActionResult AddSubjectToStudent(string studentId, int subjectId) { Student student = studentsService.GetById(studentId); if (student == null || subjectsService.GetById(subjectId) == null) { return(NotFound()); } try { if (student.StudentAttendsSubject.Select(x => x.Subject.SubjectId).Contains(subjectId)) { throw new Exception("Student already attends this subject!"); } else { logger.Info("Adding subject to student"); studentsService.AddSubjectToStudent(studentId, subjectId); return(Ok(student.StudentAttendsSubject.Select(x => x.Subject))); } } catch (Exception e) { logger.Error(e, "Subject already added"); return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e))); } }