// GET: AdminMatchUsers/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var matchUser = await _context.MatchUsers .Include(m => m.Match) .Include(m => m.Match).ThenInclude(s => s.Rounds) .Include(m => m.Match).ThenInclude(s => s.Rounds).ThenInclude(r => r.Sides).ThenInclude(r => r.Squads).ThenInclude(s => s.Slots) .Include(m => m.Side) .Include(m => m.User) .FirstOrDefaultAsync(m => m.MatchUserID == id); if (matchUser == null) { return(NotFound()); } var vm = new MatchUserEditViewModel(); vm.MatchUser = matchUser; PrepareEditViewModel(vm); return(View(vm)); }
private async Task ApplyUserSlots(MatchUserEditViewModel vm) { if (vm.MatchUser.MatchSideID == null) { var previousSlots = await _context.RoundSlots.Where(s => s.MatchUserID == vm.MatchUser.MatchUserID).ToListAsync(); foreach (var slot in previousSlots) { slot.MatchUserID = null; slot.IsValidated = false; slot.SetTimestamp(); _context.Update(slot); } } else { var slotIds = vm.SlotPerRound.Where(s => s.RoundSlotID != null).Select(s => s.RoundSlotID).ToList(); var previousSlots = await _context.RoundSlots.Where(s => !slotIds.Contains(s.RoundSlotID) && s.MatchUserID == vm.MatchUser.MatchUserID).ToListAsync(); foreach (var slot in previousSlots) { slot.MatchUserID = null; slot.IsValidated = false; slot.SetTimestamp(); _context.Update(slot); } var newSlots = await _context.RoundSlots.Where(s => slotIds.Contains(s.RoundSlotID) && s.Squad.Side.MatchSide.MatchSideID == vm.MatchUser.MatchSideID).ToListAsync(); foreach (var slot in newSlots) { slot.MatchUserID = vm.MatchUser.MatchUserID; slot.IsValidated = true; slot.SetTimestamp(); _context.Update(slot); } } }
public async Task <IActionResult> Edit(int id, MatchUserEditViewModel vm) { if (id != vm.MatchUser.MatchUserID) { return(NotFound()); } if (ModelState.IsValid) { try { using (var transac = await _context.Database.BeginTransactionAsync()) { _context.Update(vm.MatchUser); await ApplyUserSlots(vm); await _context.SaveChangesAsync(); await transac.CommitAsync(); } } catch (DbUpdateConcurrencyException) { if (!MatchUserExists(vm.MatchUser.MatchUserID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Details), ControllersName.AdminMatchs, new { id = vm.MatchUser.MatchID }, "users")); } PrepareEditViewModel(vm); return(View(vm)); }
public async Task <IActionResult> Edit(int id, MatchUserEditViewModel vm) { if (id != vm.MatchUser.MatchUserID) { return(NotFound()); } if (ModelState.IsValid) { try { using (var transac = await _context.Database.BeginTransactionAsync()) { _context.Update(vm.MatchUser); if (vm.MatchUser.MatchSideID == null) { var previousSlots = await _context.RoundSlots.Where(s => s.MatchUserID == vm.MatchUser.MatchUserID).ToListAsync(); foreach (var slot in previousSlots) { slot.MatchUserID = null; slot.SetTimestamp(); _context.Update(slot); } } else { var slotIds = vm.SlotPerRound.Where(s => s.RoundSlotID != null).Select(s => s.RoundSlotID).ToList(); var previousSlots = await _context.RoundSlots.Where(s => !slotIds.Contains(s.RoundSlotID) && s.MatchUserID == vm.MatchUser.MatchUserID).ToListAsync(); foreach (var slot in previousSlots) { slot.MatchUserID = null; slot.SetTimestamp(); _context.Update(slot); } var newSlots = await _context.RoundSlots.Where(s => slotIds.Contains(s.RoundSlotID) && s.Squad.Side.MatchSide.MatchSideID == vm.MatchUser.MatchSideID).ToListAsync(); foreach (var slot in newSlots) { slot.MatchUserID = vm.MatchUser.MatchUserID; slot.SetTimestamp(); _context.Update(slot); } } await _context.SaveChangesAsync(); await transac.CommitAsync(); } } catch (DbUpdateConcurrencyException) { if (!MatchUserExists(vm.MatchUser.MatchUserID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Details), ControllersName.AdminMatchs, new { id = vm.MatchUser.MatchID }, "users")); } PrepareEditViewModel(vm); return(View(vm)); }
private void PrepareEditViewModel(MatchUserEditViewModel vm) { vm.MatchSideDropdownList = new SelectList(_context.MatchSides.Where(m => m.MatchID == vm.MatchUser.MatchID), "MatchSideID", "Name", vm.MatchUser.MatchSideID); vm.SlotPerRound = vm.MatchUser.Match.Rounds.OrderBy(r => r.Number).Select(r => CreateVM(r, vm.MatchUser)).ToList(); }