private static RsvpAggregate Reduce(RsvpAggregate state, MeetupRsvpDeclinedEvent @event) { var memberId = @event.MemberId; state.MembersGoing.Remove(memberId); state.MembersWaiting.Remove(memberId); state.MembersNotGoing.TryAdd(memberId); if (AvailableSpots(state) && state.MembersWaiting.Any()) { var firstMemberWaiting = state.MembersWaiting.First(); state.MembersGoing.Add(firstMemberWaiting); state.MembersWaiting.Remove(firstMemberWaiting); } return(state); }
private static RsvpAggregate Reduce(RsvpAggregate state, MeetupRsvpAcceptedEvent @event) { var memberId = @event.MemberId; state.MembersGoing.Remove(memberId); state.MembersNotGoing.Remove(memberId); state.MembersWaiting.Remove(memberId); if (AvailableSpots(state)) { state.MembersGoing.Add(memberId); } else { state.MembersWaiting.Add(memberId); } return(state); }
private static RsvpAggregate Reduce(RsvpAggregate state, MeetupNumberOfSpotsChangedEvent @event) { var numberOfSpotsDiff = @event.NumberOfSpots - state.NumberOfSpots; if (numberOfSpotsDiff > 0) { var membersWaiting = state.MembersWaiting.Take(numberOfSpotsDiff); state.MembersGoing.AddRange(membersWaiting); state.MembersWaiting.RemoveRange(membersWaiting); } else { var membersGoing = state.MembersGoing.TakeLast(Math.Abs(numberOfSpotsDiff)); state.MembersWaiting.InsertRange(0, membersGoing); state.MembersGoing.RemoveRange(membersGoing); } state.NumberOfSpots = @event.NumberOfSpots; return(state); }
public static RsvpAggregate Reduce(RsvpAggregate state, object @event) { switch (@event) { case MeetupRsvpOpenedEvent rsvpOpened: state.NumberOfSpots = rsvpOpened.NumberOfSpots; break; case MeetupRsvpAcceptedEvent rsvpAccepted: state = Reduce(state, rsvpAccepted); break; case MeetupRsvpDeclinedEvent rsvpDeclined: state = Reduce(state, rsvpDeclined); break; case MeetupNumberOfSpotsChangedEvent spotsChanged: state = Reduce(state, spotsChanged); break; } return(state); }
private static bool AvailableSpots(RsvpAggregate state) => state.NumberOfSpots > state.MembersGoing.Count;
public static RsvpAggregate Reduce(this RsvpAggregate aggregate, object @event) => RsvpAggregate.Reduce(aggregate, @event);