public List <EngineerShiftModel> ScheduleEngineerShift(ShiftRequestModel shiftRequest) { List <Engineer> engineers = _repository.FindEngineersAvailableOn(shiftRequest.StartDate); if (engineers.Count < shiftRequest.Count) { throw new InvalidOperationException ($"You requested {shiftRequest.Count} engineer{(shiftRequest.Count > 1 ? "s" : "")} but only {engineers.Count} {(engineers.Count > 1 ? "are" : "is")} available"); } ValidateEngineers(engineers, shiftRequest.StartDate); var randomEngineers = engineers.OrderBy(x => new Random().Next()).Take(shiftRequest.Count).ToList(); List <EngineerShift> shifts = randomEngineers.Select(e => new EngineerShift { Date = shiftRequest.StartDate, EngineerId = e.Id, Duration = 4 } ).ToList(); return(Mapper.Map <List <EngineerShiftModel> >(_repository.ScheduleEngineerShift(shifts))); }