public IActionResult CreateWeek( string ownerIdentityName, [FromBody] WeekForCreationDto newWeekDto) { if (newWeekDto == null) { return(BadRequest()); } if (ModelState.IsValid == false) { return(BadRequest(ModelState)); } if (!_repo.ClassEntityExists(ownerIdentityName)) { return(NotFound()); } var finalWeek = _mapper.Map <Entities.Week>(newWeekDto); //START: Might be able to replace this by defining student name as a Key value for EFCore var weeks = _repo.GetAllWeeksFromClass(ownerIdentityName); foreach (var s in weeks) { if (s.Number == finalWeek.Number) { return(UnprocessableEntity("There is already a Week with that number. How did that happen?")); } } //STOP: Might be able to replace this by defining student name as a Key value for EFCore _repo.AddWeek(ownerIdentityName, finalWeek); if (!_repo.Save()) { return(StatusCode(500, $"A problem happened while handling your request to create a week with number: {newWeekDto.Number}.")); } var weekToReturn = _mapper.Map <WeekDto>(finalWeek); return(CreatedAtAction("GetWeek", new { weekId = weekToReturn.Id }, weekToReturn)); }