private async Task DoSwitchCommand(Context ctx, ICollection <PKMember> members) { // Make sure there are no dupes in the list // We do this by checking if removing duplicate member IDs results in a list of different length if (members.Select(m => m.Id).Distinct().Count() != members.Count) { throw Errors.DuplicateSwitchMembers; } // Find the last switch and its members if applicable var lastSwitch = await _switches.GetLatestSwitch(ctx.System); if (lastSwitch != null) { var lastSwitchMembers = await _switches.GetSwitchMembers(lastSwitch); // Make sure the requested switch isn't identical to the last one if (lastSwitchMembers.Select(m => m.Id).SequenceEqual(members.Select(m => m.Id))) { throw Errors.SameSwitch(members); } } await _switches.RegisterSwitch(ctx.System, members); if (members.Count == 0) { await ctx.Reply($"{Emojis.Success} Switch-out registered."); } else { await ctx.Reply($"{Emojis.Success} Switch registered. Current fronter is now {string.Join(", ", members.Select(m => m.Name)).SanitizeMentions()}."); } }
public async Task <IActionResult> PostSwitch([FromBody] PostSwitchParams param) { if (param.Members.Distinct().Count() != param.Members.Count()) { return(BadRequest("Duplicate members in member list.")); } // We get the current switch, if it exists var latestSwitch = await _switches.GetLatestSwitch(_auth.CurrentSystem); if (latestSwitch != null) { var latestSwitchMembers = await _switches.GetSwitchMembers(latestSwitch); // Bail if this switch is identical to the latest one if (latestSwitchMembers.Select(m => m.Hid).SequenceEqual(param.Members)) { return(BadRequest("New members identical to existing fronters.")); } } // Resolve member objects for all given IDs IEnumerable <PKMember> membersList; using (var conn = await _conn.Obtain()) membersList = (await conn.QueryAsync <PKMember>("select * from members where hid = any(@Hids)", new { Hids = param.Members })).ToList(); foreach (var member in membersList) { if (member.System != _auth.CurrentSystem.Id) { return(BadRequest($"Cannot switch to member '{member.Hid}' not in system.")); } } // membersList is in DB order, and we want it in actual input order // so we go through a dict and map the original input appropriately var membersDict = membersList.ToDictionary(m => m.Hid); var membersInOrder = new List <PKMember>(); // We do this without .Select() since we want to have the early return bail if it doesn't find the member foreach (var givenMemberId in param.Members) { if (!membersDict.TryGetValue(givenMemberId, out var member)) { return(BadRequest($"Member '{givenMemberId}' not found.")); } membersInOrder.Add(member); } // Finally, log the switch (yay!) await _switches.RegisterSwitch(_auth.CurrentSystem, membersInOrder); return(NoContent()); }