Beispiel #1
0
        private void EnsureInvariants(MeetupState previous)
        {
            var valid = Id != MeetupId.None && Title != MeetupTitle.None &&
                        State switch
            {
                MeetupState.Published => RequiredFields(Published),
                MeetupState.Canceled => RequiredFields(Canceled),
                MeetupState.Closed => RequiredFields(Closed),
                _ => true
            };

            if (!valid)
            {
                throw new MeetupDomainException($"Invalid state {State}");
            }

            bool RequiredFields(Func <MeetupState[]> states) =>
            states().Any(s => s == previous) &&
            Location != Address.None &&
            NumberOfSeats != SeatsNumber.None &&
            TimeRange != DateTimeRange.None;

            MeetupState[] Published() => new[] { MeetupState.Published, MeetupState.Created };
            MeetupState[] Canceled() => new[] { MeetupState.Canceled, MeetupState.Published };
            MeetupState[] Closed() => new[] { MeetupState.Closed, MeetupState.Published };
        }
    }
Beispiel #2
0
        public void Cancel()
        {
            var previous = State;

            State = MeetupState.Canceled;
            EnsureInvariants(previous);
        }
Beispiel #3
0
 public Meetup(MeetupTitle title, Location location)
 {
     Title         = title;
     Location      = location;
     State         = MeetupState.Created;
     NumberOfSeats = NumberOfSeats.None;
 }
Beispiel #4
0
        public void Publish()
        {
            var previous = State;

            State = MeetupState.Published;
            EnsureInvariants(previous);
        }
Beispiel #5
0
        public void Close()
        {
            var previous = State;

            State = MeetupState.Closed;
            Console.WriteLine($"MeetupState previous {previous}, Current {State}");
            EnsureInvariants(previous);
        }
Beispiel #6
0
 public void Publish()
 {
     if (NumberOfSeats == NumberOfSeats.None)
     {
         throw new ArgumentException(nameof(NumberOfSeats));
     }
     State = MeetupState.Published;
 }
Beispiel #7
0
        public MeetupState TransitionTo(MeetupState to)
        {
            if (!CanTransitionTo(to))
            {
                throw new ArgumentException($"{nameof(to)}");
            }

            return(to);
        }
Beispiel #8
0
 public Meetup(MeetupId id, MeetupTitle title, Address location, SeatsNumber numberOfSeats, DateTimeRange timeRange, MeetupState state)
 {
     Id            = id;
     Title         = title;
     Location      = location;
     NumberOfSeats = numberOfSeats;
     TimeRange     = timeRange;
     State         = state;
     EnsureInvariants(State);
 }
Beispiel #9
0
 public Meetup(MeetupId id, MeetupTitle title)
 {
     Id            = id;
     Title         = title;
     Location      = Address.None;
     NumberOfSeats = SeatsNumber.None;
     TimeRange     = DateTimeRange.None;
     State         = MeetupState.Created;
     EnsureInvariants();
 }
Beispiel #10
0
 public MeetupAggregate(
     MeetupId id,
     MeetupTitle title,
     ValidatedLocation location,
     NumberOfSeats seats,
     MeetupState state,
     Dictionary <MemberId, DateTime> going,
     Dictionary <MemberId, DateTime> notGoing)
 {
     Id            = id;
     Title         = title;
     Location      = location;
     NumberOfSeats = seats;
     State         = state;
     _going        = going;
     _notGoing     = notGoing;
 }
Beispiel #11
0
 public Meetup(
     MeetupId id,
     MeetupTitle title,
     Address location,
     SeatsNumber numberOfSeats,
     DateTimeRange timeRange,
     Dictionary <MemberId, DateTime> membersGoing,
     Dictionary <MemberId, DateTime> membersNotGoing,
     MeetupState state)
 {
     Id               = id;
     Title            = title;
     Location         = location;
     NumberOfSeats    = numberOfSeats;
     TimeRange        = timeRange;
     State            = state;
     _membersGoing    = membersGoing;
     _membersNotGoing = membersNotGoing;
     EnsureInvariants();
 }
Beispiel #12
0
        protected override void When(object @event)
        {
            State.EnsureCanRaiseEvent(@event.GetType());
            switch (@event)
            {
            case Events.MeetupCreated ev:
                Id    = MeetupId.From(ev.Id);
                Title = MeetupTitle.From(ev.Title);
                State = MeetupState.Created;
                break;

            case Events.MeetupPublished ev:
                State = MeetupState.Published;
                break;

            case Events.MeetupCanceled ev:
                State = MeetupState.Canceled;
                break;

            case Events.MeetupClosed ev:
                State = MeetupState.Closed;
                break;

            case Events.MeetupNumberOfSeatsUpdated ev:
                NumberOfSeats = SeatsNumber.From(ev.NumberOfSeats);
                break;

            case Events.MeetupTimeUpdated ev:
                TimeRange = DateTimeRange.From(ev.Start, ev.End);
                break;

            case Events.MeetupTitleUpdated ev:
                Title = MeetupTitle.From(ev.Title);
                break;

            case Events.MeetupLocationUpdated ev:
                Location = Address.From(ev.Location);
                break;
            }
        }
Beispiel #13
0
 public bool CanTransitionTo(MeetupState to) =>
 _allowedTransitions.Any(x => x == to);