public async Task <IEnumerable <KeyValuePair <DateTime, int> > > Distribute(int month) { if (month >= 12 && month <= 1) { throw new Exception(Constants.InvalidMonthExceptionMessage); } var users = new List <User>() { new User { Id = 1, Firstname = "Christoph", Lastname = "Thiel", ShiftCount = 2 }, new User { Id = 2, Firstname = "David", Lastname = "Eilmsteiner", ShiftCount = 1, Preferences = new Collection <Preference> { new Preference { Avoid = true, Type = PreferenceType.Holiday } } }, new User { Id = 3, Firstname = "Andreas", Lastname = "Weinzierl", ShiftCount = 1 }, new User { Id = 4, Firstname = "Daniel", Lastname = "Heureka", ShiftCount = 2 }, new User { Id = 5, Firstname = "Gustav", Lastname = "Gans", ShiftCount = 2 }, new User { Id = 6, Firstname = "Helmut", Lastname = "Schätz", ShiftCount = 1 }, new User { Id = 7, Firstname = "Wolfgang", Lastname = "Ramm", ShiftCount = 2 }, new User { Id = 8, Firstname = "Matthias", Lastname = "Buhm", ShiftCount = 2 }, new User { Id = 9, Firstname = "Otto", Lastname = "Walkes", ShiftCount = 1 }, new User { Id = 10, Firstname = "Wolfgang", Lastname = "Beethoven", ShiftCount = 1 }, new User { Id = 11, Firstname = "Imran", Lastname = "Topalovic", ShiftCount = 2 }, new User { Id = 12, Firstname = "Xaver", Lastname = "DJ", ShiftCount = 2 }, new User { Id = 13, Firstname = "Denzel", Lastname = "Chicago", ShiftCount = 1 }, new User { Id = 14, Firstname = "Paula", Lastname = "Pulus", ShiftCount = 2 }, new User { Id = 15, Firstname = "Titus", Lastname = "Banana", ShiftCount = 2 }, new User { Id = 16, Firstname = "Emil", Lastname = "Donovan", ShiftCount = 1 }, new User { Id = 17, Firstname = "Edward", Lastname = "Thatch", ShiftCount = 1 }, new User { Id = 18, Firstname = "Edward", Lastname = "Kenway", ShiftCount = 2 }, new User { Id = 19, Firstname = "Benjamin", Lastname = "Hornigold", ShiftCount = 2 }, new User { Id = 20, Firstname = "Klaus", Lastname = "Dieter", ShiftCount = 1 }, new User { Id = 21, Firstname = "Alexander", Lastname = "Gramm", ShiftCount = 2 }, new User { Id = 22, Firstname = "Rudolf", Lastname = "Rosenberger", ShiftCount = 2 }, new User { Id = 23, Firstname = "Anton", Lastname = "Toni", ShiftCount = 1 }, new User { Id = 24, Firstname = "Konrad", Lastname = "Conelius", ShiftCount = 1 }, new User { Id = 25, Firstname = "Niklas", Lastname = "Bronze", ShiftCount = 2 }, new User { Id = 26, Firstname = "Siegi", Lastname = "Vogel", ShiftCount = 2 }, new User { Id = 27, Firstname = "Keine", Lastname = "Ahnung", ShiftCount = 1 }, new User { Id = 28, Firstname = "Christoph", Lastname = "LastButNOtLeast", ShiftCount = 0 } }; users = _unitOfWork.Users.All(nameof(User.Preferences)) .ToList(); //var year = month < DateTime.Today.Month ? DateTime.Today.Year + 1 : DateTime.Today.Year; var year = DateTime.Today.Year; var amountOfDays = DateTime.DaysInMonth(year, month); var amountOfShifts = amountOfDays + _holidaysService.All() .Where(h => DateTime.Parse(h.Date).Month == month) .Count() * 2 + CountDays(DayOfWeek.Saturday, year, month, amountOfDays) * 2 + CountDays(DayOfWeek.Sunday, year, month, amountOfDays) * 2; /* if (users.Sum(u => u.ShiftCount) < amountOfShifts) * throw new Exception(Constants.ImpossibleDistributionExceptionMessage); */ var shifts = new List <KeyValuePair <DateTime, int> >(); for (var i = 1; i <= amountOfDays; i++) { var date = new DateTime(year, month, i, 18, 0, 0); var iterations = IsWeekend(date) || IsHoliday(date) ? 3 : 1; for (var j = 0; j < iterations; j++) { date = new DateTime(year, month, i, 18, 0, 0).AddHours(-6 * j); var help = users.Where(u => u.ShiftCount > shifts.Count(s => s.Value == u.Id)); var validUsers = users.Where(u => u.ShiftCount > shifts.Count(s => s.Value == u.Id)) .Where(u => !PreferencesInvalid(u.Preferences, date)); var random = new Random(); var position = random.Next(0, validUsers.Count() - 1); shifts.Add(new KeyValuePair <DateTime, int>(date, validUsers.ElementAt(position).Id)); } } return(shifts); }
public IActionResult All() { return(Ok(_service.All(null))); }