public async Task <IActionResult> Edit(int id, RoundSquadFormViewModel vm) { if (id != vm.Squad.RoundSquadID) { return(NotFound()); } if (ModelState.IsValid) { try { using (var transac = await _context.Database.BeginTransactionAsync()) { await PrepareViewModel(vm); var removed = vm.Squad.Slots.Where(s => s.Role == null && s.RoundSlotID != 0).ToList(); // Il y a un risque de concurrence d'accès, on s'assure que si un utilisateur s'est affecté entre temps que ce n'est pas perdu if (await DetectConcurrentUpdates(vm) || await CheckUserAvailibilty(vm)) { await PrepareDrowndownList(vm); return(View(vm)); } vm.Squad.Slots = vm.Squad.Slots.Where(s => s.Role != null).ToList(); vm.Squad.SlotsCount = vm.Squad.Slots.Count(); _context.Update(vm.Squad); // Numérote les slots, et s'assure que les utilisateurs n'ont pas de doublons NormalizeSlotsNumber(vm); foreach (var slot in vm.Squad.Slots) { slot.SetTimestamp(); if (slot.RoundSlotID == 0) { _context.Add(slot); } else { _context.Update(slot); } } foreach (var slot in removed) { _context.Remove(slot); } await _context.SaveChangesAsync(); await transac.CommitAsync(); } } catch (DbUpdateConcurrencyException) { if (!RoundSquadExists(vm.Squad.RoundSquadID)) { return(NotFound()); } else { throw; } } return(RedirectToRound(vm.Squad)); } await PrepareViewModel(vm); await PrepareDrowndownList(vm); return(View(vm)); }
public async Task <IActionResult> Edit(int id, MatchFormViewModel vm, string applyTemplate) { if (id != vm.Match.MatchID) { return(NotFound()); } ApplyTemplate(vm); if (ModelState.IsValid && string.IsNullOrEmpty(applyTemplate)) { ConsolidateMatchForm(vm); try { await LoadTacMap(vm.Match); if (!string.IsNullOrEmpty(vm.Match.WorldName)) { if (vm.Match.TacMapId == null || vm.Match.TacMap.WorldName != vm.Match.WorldName) { await CreateTacMap(vm); } else if (vm.Match.TacMap.Label != vm.Match.Name) { await _tacMaps.Update(vm.Match.TacMap.Id, new ApiTacMapPatch() { Label = vm.Match.Name }); } } _context.Update(vm.Match); foreach (var side in vm.Match.Sides) { _context.Update(side); if (side.SquadsPolicy == SquadsPolicy.SquadsAndSlotsRestricted) { var squadsToRestrict = await _context.RoundSquads.Where(s => s.Side.MatchSideID == side.MatchSideID && !s.RestrictTeamComposition).ToListAsync(); foreach (var squad in squadsToRestrict) { squad.RestrictTeamComposition = true; _context.Update(squad); } } } foreach (var round in vm.Match.Rounds) { if (round.RoundID == 0) { _context.Add(round); } else { _context.Update(round); } } foreach (var roundSide in vm.Match.Rounds.SelectMany(r => r.Sides)) { if (roundSide.RoundSideID == 0) { _context.Add(roundSide); } else { _context.Update(roundSide); } } var remains = vm.Match.Rounds.Where(r => r.RoundID != 0).Select(r => r.RoundID).ToList(); foreach (var removed in _context.Rounds.Where(r => r.MatchID == vm.Match.MatchID && !remains.Contains(r.RoundID)).ToList()) { _context.Remove(removed); } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MatchExists(vm.Match.MatchID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } await PrepareDrowdownLists(vm); return(View(vm)); }