public void Apply(VoteChange notification) { if (notification == null) { throw new ArgumentNullException(nameof(notification)); } // Take care of the mutation, adding and removing fake uncast votes as necessary switch (notification.Mutation) { case VoteMutationType.Added: this.Votes.Add(notification.Vote); int uncastVoteIdx = this.Votes.FindIndex(x => x.IsCast == false); if (uncastVoteIdx != -1) { this.Votes.RemoveAt(uncastVoteIdx); } this.VotesByParticipant.Add(notification.Vote); this.VotesByNote.Add(notification.Vote); this.VotesByNoteGroup.Add(notification.Vote); break; case VoteMutationType.Removed: int voteId = notification.Vote.Id; this.Votes.RemoveAll(v => v.Id == voteId); this.Votes.Add(VoteModel.CreateEmptyFrom(notification.Vote)); this.VotesByParticipant.Remove(notification.Vote); this.VotesByNote.Remove(notification.Vote); this.VotesByNoteGroup.Remove(notification.Vote); break; default: throw new ArgumentOutOfRangeException(nameof(notification)); } }
public void Remove(VoteModel notificationVote) { int?key = this._keySelector.Invoke(notificationVote); if (key == null) { return; } if (!this._dictionary.TryGetValue(key.Value, out List <VoteModel>?votes)) { return; } votes.RemoveAll(x => x.Id == notificationVote.Id); // Add artificial uncast vote VoteModel artificialVote = VoteModel.CreateEmptyFrom(notificationVote); if (this._keySelector.Invoke(artificialVote) != null) { votes.Add(artificialVote); } }