Пример #1
0
        public void Handle(OrderPlaced message)
        {
            if (this.State == ProcessState.NotStarted)
            {
                this.WorkshopID = message.WorkshopId;
                this.OrderID    = message.SourceId;
                // Use the order id as an opaque reservation id for the anchor reservation.
                // It could be anything else, as long as it is deterministic from the OrderPlaced event.
                this.ReservationID             = message.SourceId;
                this.ReservationAutoExpiration = message.ReservationAutoExpiration;
                var expirationWindow = message.ReservationAutoExpiration.Subtract(DateTime.UtcNow);

                if (expirationWindow > TimeSpan.Zero)
                {
                    this.State = ProcessState.AwaitingReservationConfirmation;
                    var anchorReservationCommand =
                        new MakeAnchorReservation
                    {
                        WorkshopID    = this.WorkshopID,
                        ReservationId = this.ReservationID,
                        Anchors       = message.Anchors.ToList()
                    };
                    this.AnchorReservationCommandID = anchorReservationCommand.Id;

                    this.AddCommand(new Envelope <ICommand>(anchorReservationCommand)
                    {
                        TimeToLive = expirationWindow.Add(TimeSpan.FromMinutes(1)),
                    });

                    var expirationCommand = new ExpireRegistrationProcess {
                        ProcessId = this.ID
                    };
                    this.ExpirationCommandId = expirationCommand.Id;
                    this.AddCommand(new Envelope <ICommand>(expirationCommand)
                    {
                        Delay = expirationWindow.Add(BufferTimeBeforeReleasingAnchorsAfterExpiration),
                    });
                }
                else
                {
                    AddCommand(new RejectOrder {
                        OrderId = this.OrderID
                    });
                    Completed = true;
                }
            }
            else
            {
                if (message.WorkshopId != this.WorkshopID)
                {
                    // throw only if not reprocessing
                    throw new InvalidOperationException();
                }
            }
        }
Пример #2
0
        public void Handle(OrderUpdated message)
        {
            if (this.State == ProcessState.AwaitingReservationConfirmation ||
                this.State == ProcessState.ReservationConfirmationReceived)
            {
                this.State = ProcessState.AwaitingReservationConfirmation;

                var anchorReservationCommand =
                    new MakeAnchorReservation
                {
                    WorkshopID    = this.WorkshopID,
                    ReservationId = this.ReservationID,
                    Anchors       = message.Anchors.ToList()
                };
                this.AnchorReservationCommandID = anchorReservationCommand.Id;
                this.AddCommand(anchorReservationCommand);
            }
            else
            {
                throw new InvalidOperationException("The order cannot be updated at this stage.");
            }
        }