Ejemplo n.º 1
0
        public static async Task <IActionResult> RunAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "order/{orderId}")]
            Amount amount,
            string orderId,
            [DomainEventStream("Sales", "Order", "{orderId}")]
            DomainEventStream eventStream,
            ILogger log)
        {
            var events = await eventStream.Events();

            var orderAccepted = events.GetFirst <OrderAccepted>().Where(order => order.OrderId == orderId);

            if (orderAccepted == null)
            {
                return(new NotFoundResult());
            }

            var amountOfOrderChanged = new AmountOfOrderChanged(Guid.Empty.ToString())
            {
                OrderId        = orderId,
                PreviousAmount = orderAccepted.Amount,
                NewAmount      = amount.Value
            };

            await eventStream.Append(amountOfOrderChanged, orderId);

            return(new OkObjectResult(new
            {
                prevoiusAmount = amountOfOrderChanged.PreviousAmount,
                newAmount = amountOfOrderChanged.NewAmount,
                message = "Amount changed"
            }));
        }
Ejemplo n.º 2
0
        public static async Task <IActionResult> RunAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "order")]
            Order order,
            [DomainEventStream("Sales", "Order")]
            DomainEventStream eventStream,
            ILogger log)
        {
            /// Here is the place for business validation.
            /// If an Order is correct, we create the event OrderAccepted

            var orderId     = Guid.NewGuid().ToString();
            var orderNumber = NextOrderNumber();

            if (IsValid(order))
            {
                var orderAccepted = new OrderAccepted(Guid.Empty.ToString())
                {
                    OrderId     = orderId,
                    OrderNumber = orderNumber,
                    Paste       = order.Paste,
                    Tomatoes    = order.Tomatoes,
                    Cheese      = order.Cheese,
                    Amount      = order.Amount,
                    CustomerId  = order.CustomerId
                };

                // Put the Event into EventStream.
                // EntityId is needed because the Stream doesn't know the Id and can't read from Event
                await eventStream.Append(orderAccepted, orderAccepted.OrderId);

                return(new OkObjectResult(new
                {
                    orderNumber = orderAccepted.OrderNumber,
                    orderId = orderAccepted.OrderId,
                    message = "Order accepted"
                }));
            }
            else
            {
                var orderDeclined = new OrderDeclined(Guid.Empty.ToString())
                {
                    OrderId      = orderId,
                    OrderNumber  = orderNumber,
                    OrderContent =
                        $"Paste: {order.Paste}, Tomatoes: {order.Tomatoes}, Cheese{order.Cheese}, Amount: {order.Amount}",
                    CustomerId = order.CustomerId,
                    Reason     = "[put the reason in here]"
                };

                try
                {
                }
                catch (Exception ex)
                {
                    log.LogError(ex.Message, ex);

                    return(new BadRequestObjectResult(new
                    {
                        errorCode = "ORDER_DECLINED_BUT_SERVER_ERROR",
                        message = $"Order declined because of [put the reason in here]" +
                                  $"(Internal Server Error: {ex.Message}"
                    }));
                }

                return(new BadRequestObjectResult(new
                {
                    errorCode = "ORDER_DECLINED",
                    message = "Order declined because of [put the reason in here]"
                }));
            }
        }