public IActionResult CreateStudent( string ownerIdentityName, [FromBody] StudentForCreationDto newStudentDto) { if (newStudentDto == null) { return(BadRequest()); } if (ModelState.IsValid == false) { return(BadRequest(ModelState)); } if (!_repo.ClassEntityExists(ownerIdentityName)) { return(NotFound()); } var finalStudent = _mapper.Map <Entities.Student>(newStudentDto); //START: Might be able to replace this by defining student name as a Key value for EFCore var classStudents = _repo.GetAllStudentsFromClass(ownerIdentityName); foreach (var s in classStudents) { if (s.Name == finalStudent.Name) { return(UnprocessableEntity("There is already a Student with that name in this Class")); } } //STOP: Might be able to replace this by defining student name as a Key value for EFCore _repo.AddStudent(ownerIdentityName, finalStudent); if (!_repo.Save()) { return(StatusCode(500, $"A problem happened while handling your request to create a student with name: {newStudentDto.Name}.")); } var createdStudentToReturn = _mapper.Map <StudentDto>(finalStudent); return(CreatedAtAction("GetStudent", new { studentId = createdStudentToReturn.Id }, createdStudentToReturn)); }