Exemplo n.º 1
0
        public static void UsePurchaseOrders(this IEndpointRouteBuilder builder,
                                             PurchaseOrderRepository purchaseOrders)
        {
            builder
            .MapGet(string.Empty, async context => {
                var orders = await purchaseOrders.List(context.RequestAborted);

                return(new HalResponse(context.Request, PurchaseOrderListRepresentation.Instance, orders));
            })
            .MapPost(string.Empty, async(HttpContext context, PurchaseOrder purchaseOrder) => {
                if (purchaseOrder.PurchaseOrderId == Guid.Empty)
                {
                    purchaseOrder.PurchaseOrderId = Guid.NewGuid();
                }

                await purchaseOrders.Save(purchaseOrder, context.RequestAborted);

                return(new HalResponse(context.Request, PurchaseOrderRepresentation.Instance, purchaseOrder)
                {
                    StatusCode = HttpStatusCode.Created,
                    Headers =
                    {
                        ["location"] = purchaseOrder.PurchaseOrderId.ToString()
                    }
                });
            })
            .MapGet("{purchaseOrderId:guid}", async context => {
                if (!context.TryParseGuid(nameof(PurchaseOrder.PurchaseOrderId), out var purchaseOrderId))
                {
                    return(new HalResponse(context.Request, PurchaseOrderRepresentation.Instance)
                    {
                        StatusCode = HttpStatusCode.NotFound
                    });
                }

                var order = await purchaseOrders.Get(purchaseOrderId, context.RequestAborted);

                return(new HalResponse(context.Request, PurchaseOrderRepresentation.Instance, order)
                {
                    StatusCode = order.HasValue ? HttpStatusCode.OK : HttpStatusCode.NotFound
                });
            })
            .MapPut("{purchaseOrderId:guid}", async(HttpContext context, PurchaseOrder purchaseOrder) => {
                if (!context.TryParseGuid(nameof(purchaseOrder.PurchaseOrderId), out var purchaseOrderId))
                {
                    return(new HalResponse(context.Request, PurchaseOrderRepresentation.Instance)
                    {
                        StatusCode = HttpStatusCode.NotFound
                    });
                }

                purchaseOrder.PurchaseOrderId = purchaseOrderId;

                await purchaseOrders.Save(purchaseOrder, context.RequestAborted);

                return(new HalResponse(context.Request, PurchaseOrderRepresentation.Instance, purchaseOrder));
            })
            .MapBusinessTransaction <PurchaseOrder>("{purchaseOrderId:guid}");
        }
Exemplo n.º 2
0
        public async Task when_persisting_to_storage(PurchaseOrder purchaseOrder)
        {
            using var streamStore = new InMemoryStreamStore();
            var repository = new PurchaseOrderRepository(streamStore);

            await repository.Save(purchaseOrder);

            var copy = await repository.Get(Guid.Parse(purchaseOrder.PurchaseOrderId));

            Assert.Equal(purchaseOrder, copy, new LogicEqualityComparer <PurchaseOrder> {
                UseObjectHashes = false
            });
        }