public async Task <IActionResult> SubscriptionSlot(int id, int?roundSquadID, int?roundSlotID, int?roundSideID, string squadName) { var user = await GetUser(); if (user == null) { return(NotFound()); } var matchUser = await _context.MatchUsers.FirstOrDefaultAsync(u => u.MatchID == id && u.UserID == user.UserID); if (matchUser == null || matchUser.MatchSideID == null) { return(NotFound()); } using (var transaction = await _context.Database.BeginTransactionAsync()) { RoundSlot roundSlot = null; if (roundSlotID != null) { roundSlot = await _context.RoundSlots.Include(s => s.Squad).ThenInclude(s => s.Side).FirstOrDefaultAsync(s => s.Squad.Side.Round.MatchID == id && s.Squad.Side.MatchSideID == matchUser.MatchSideID && s.RoundSlotID == roundSlotID); if (roundSlot == null || roundSlot.MatchUserID != null) { return(RedirectToAction(nameof(Subscription), new { id })); } } else if (roundSquadID != null) { var roundSquad = await _context.RoundSquads.Include(s => s.Side).FirstOrDefaultAsync(s => s.Side.Round.MatchID == id && s.Side.MatchSideID == matchUser.MatchSideID && s.RoundSquadID == roundSquadID); if (roundSquad == null || roundSquad.SlotsCount >= 9 || roundSquad.RestrictTeamComposition) { return(RedirectToAction(nameof(Subscription), new { id })); } roundSlot = new RoundSlot() { Squad = roundSquad, SlotNumber = roundSquad.SlotsCount, Role = FireTeamRole.Alpha }; roundSlot.SetTimestamp(); roundSquad.SlotsCount++; _context.Add(roundSlot); _context.Update(roundSquad); await _context.SaveChangesAsync(); } await AssignSlot(matchUser, roundSlot); await transaction.CommitAsync(); } return(RedirectToAction(nameof(Subscription), new { id })); }
public async Task <IActionResult> SubscriptionSlot(int id, int?roundSquadID, int?roundSlotID, int?roundSideID, string squadName) { var user = await GetUser(); if (user == null) { return(NotFound()); } var matchUser = await _context.MatchUsers.Include(u => u.Side).FirstOrDefaultAsync(u => u.MatchID == id && u.UserID == user.UserID); if (matchUser == null || matchUser.MatchSideID == null) { return(NotFound()); } using (var transaction = await _context.Database.BeginTransactionAsync()) { RoundSlot roundSlot = null; if (roundSlotID != null) { roundSlot = await _context.RoundSlots.Include(s => s.Squad).ThenInclude(s => s.Side).FirstOrDefaultAsync(s => s.Squad.Side.Round.MatchID == id && s.Squad.Side.MatchSideID == matchUser.MatchSideID && s.RoundSlotID == roundSlotID); if (roundSlot == null || roundSlot.MatchUserID != null) { return(RedirectToAction(nameof(Subscription), new { id })); } } else if (roundSquadID != null) { var roundSquad = await _context.RoundSquads.Include(s => s.Side).Include(s => s.Slots).FirstOrDefaultAsync(s => s.Side.Round.MatchID == id && s.Side.MatchSideID == matchUser.MatchSideID && s.RoundSquadID == roundSquadID); if (roundSquad == null || roundSquad.RestrictTeamComposition) { return(RedirectToAction(nameof(Subscription), new { id })); } roundSlot = roundSquad.Slots.FirstOrDefault(s => s.MatchUserID == null); if (roundSlot == null) { roundSquad.SlotsCount++; roundSlot = new RoundSlot() { Squad = roundSquad, SlotNumber = roundSquad.SlotsCount, Role = Role.Member }; roundSlot.SetTimestamp(); _context.Add(roundSlot); _context.Update(roundSquad); await _context.SaveChangesAsync(); } } else if (!string.IsNullOrEmpty(squadName) && roundSideID != null) { if (matchUser.Side.SquadsPolicy != SquadsPolicy.Unrestricted) { return(RedirectToAction(nameof(Subscription), new { id })); } var others = await _context.RoundSquads.Where(rs => rs.RoundSideID == roundSideID).ToListAsync(); var roundSquad = new RoundSquad() { Name = squadName, UniqueDesignation = RoundSquad.UniqueDesignations.First(num => !others.Any(t => t.UniqueDesignation == num)), RoundSideID = roundSideID.Value, SlotsCount = 1, Slots = new List <RoundSlot>(), Side = await _context.RoundSides.FindAsync(roundSideID.Value), RestrictTeamComposition = false, InviteOnly = false }; roundSlot = new RoundSlot() { Squad = roundSquad, SlotNumber = 1, Role = Role.SquadLeader }; roundSlot.SetTimestamp(); _context.Add(roundSlot); _context.Add(roundSquad); await _context.SaveChangesAsync(); } else { return(BadRequest()); } await AssignSlot(matchUser, roundSlot); await transaction.CommitAsync(); } return(RedirectToAction(nameof(Subscription), new { id })); }