public ActionResult GenerateMatches(GenerateMatchesViewModel model) { if (ModelState.IsValid) { TestHelper helper = new TestHelper(); bool result = helper.GenerateMatches( model.Days.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries), model.Lessons.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries), model.Limit, model.StartTime, model.EndTime); if (result) { helper.ResetAppliedState(); return(RedirectToAction("Index", new { message = MessageType.Success })); } else { ViewBag.Error = "创建失败"; return(View(model)); } } return(View(model)); }
public IActionResult GenerateSchedules(GenerateMatchesViewModel viewModel) { TempData["Results"] = _repository.GenerateSchedules(viewModel); if (TempData["Results"].Equals("Season Created Successfully")) { return(RedirectToAction("Index", "Matches", new { id = viewModel.SeasonId, date = viewModel.StartingDate.Value.ToString("MM-dd-yyyy") })); } return(RedirectToAction("ScheduleMatches")); }
public string GenerateSchedules(GenerateMatchesViewModel viewModel) { var returnValue = ""; if (viewModel.SeasonId != null && viewModel.StartingDate != null) { var dateRange = new List <DateTime> { viewModel.StartingDate.Value }; for (var i = 0; i < 14; i++) { dateRange.Add(dateRange[i].AddDays(7)); //Generate 15 saturdays } var existingMatchDates = _context.Matches.Select(m => m.MatchDate).Distinct(); var doesMatchAlreadyExist = dateRange.Any(date1 => existingMatchDates.Contains(date1)); if (doesMatchAlreadyExist) { returnValue += "Season already exists that has dates in this range."; } else { foreach (var division in _context.Divisions.Where(d => d.SeasonId == viewModel.SeasonId).Select(d => d.DivisionId)) { var command = _context.Database.GetDbConnection().CreateCommand(); command.CommandText = "dbo.CreateScheduleByDivision"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@SeasonId", SqlDbType.Int) { Value = viewModel.SeasonId }); command.Parameters.Add(new SqlParameter("@DivisionId", SqlDbType.Int) { Value = division }); command.Parameters.Add(new SqlParameter("@StartDate", SqlDbType.Date) { Value = viewModel.StartingDate }); if (command.Connection.State != ConnectionState.Open) { command.Connection.Open(); } command.ExecuteNonQuery(); } returnValue += "Season Created Successfully"; } } else { returnValue += "Invalid Input"; } return(returnValue); }