示例#1
0
        public IHttpActionResult Post(CreateProductCommand cmd)
        {
            if (string.IsNullOrWhiteSpace(cmd.Name))
            {
                var response = new HttpResponseMessage(HttpStatusCode.Forbidden)
                {
                    Content      = new StringContent("code must be supplied in the body"),
                    ReasonPhrase = "Missing product code"
                };
                throw new HttpResponseException(response);
            }

            try
            {
                var command = new CreateProduct(Guid.NewGuid(), cmd.Name, cmd.Description, cmd.Price);
                handler.Handle(command);

                var link = new Uri(string.Format("http://localhost:8181/api/products/{0}", command.Id));
                return(Created <CreateProduct>(link, command));
            }
            catch (AggregateNotFoundException)
            {
                return(NotFound());
            }
            catch (AggregateDeletedException)
            {
                return(Conflict());
            }
        }
示例#2
0
        public ActionResult Post(CreateProductCommand cmd)
        {
            if (string.IsNullOrWhiteSpace(cmd.Name))
            {
                return(Problem(
                           title: "Missing product code",
                           detail: "code must be supplied in the body",
                           statusCode: StatusCodes.Status400BadRequest
                           ));
            }

            try
            {
                var command = new CreateProduct(Guid.NewGuid(), cmd.Name, cmd.Description, cmd.Price);
                handler.Handle(command);

                var link = new Uri(string.Format("http://localhost:44339/api/products/{0}", command.Id));
                return(Created(link, command));
            }
            catch (AggregateNotFoundException)
            {
                return(NotFound());
            }
            catch (AggregateDeletedException)
            {
                return(Conflict());
            }
        }
示例#3
0
        public async Task ChangePrice()
        {
            var(_, repository, mediator) = TestSetup.Init();

            var handler = new ProductCommandHandlers(repository, mediator);

            var id = Guid.NewGuid();

            var productCreateCommand = new ProductCreateCommand
            {
                Id    = id,
                Name  = "mock name",
                Price = 500
            };

            await handler.Handle(productCreateCommand, CancellationToken.None);

            const int newPrice           = 600;
            var       priceChangeCommand = new ProductChangePriceCommand
            {
                ProductId = id,
                Price     = newPrice
            };

            await handler.Handle(priceChangeCommand, CancellationToken.None);

            var agg = await repository.GetHydratedAggregate <Kanayri.Domain.Product.Product>(id, CancellationToken.None);

            Assert.Equal(newPrice, agg.Price);
        }
示例#4
0
        public async Task QueryChangePrice()
        {
            var(_, repository, mediator) = TestSetup.Init();

            var handler = new ProductCommandHandlers(repository, mediator);

            var id = Guid.NewGuid();

            const int price1 = 500;
            const int price2 = 600;
            const int price3 = 700;

            var productCreateCommand = new ProductCreateCommand
            {
                Id    = id,
                Name  = "mock name",
                Price = price1
            };

            await handler.Handle(productCreateCommand, CancellationToken.None);

            var priceChange1 = new ProductChangePriceCommand
            {
                ProductId = id,
                Price     = price2
            };

            await handler.Handle(priceChange1, CancellationToken.None);

            var priceChange2 = new ProductChangePriceCommand
            {
                ProductId = id,
                Price     = price3
            };

            await handler.Handle(priceChange2, CancellationToken.None);

            var events = await repository.GetEventsOfType <ProductPriceChangedEvent>(id, CancellationToken.None);

            var changedEvents = events as ProductPriceChangedEvent[] ?? events.ToArray();

            Assert.Equal(2, changedEvents.Count());
            Assert.Equal(price2, changedEvents.ElementAt(0).Price);
            Assert.Equal(price3, changedEvents.ElementAt(1).Price);
        }
示例#5
0
        public async Task CreateProduct()
        {
            var(context, repository, mediator) = TestSetup.Init();

            var handler = new ProductCommandHandlers(repository, mediator);

            var command = new ProductCreateCommand
            {
                Id    = Guid.NewGuid(),
                Name  = "New Product",
                Price = 500
            };


            await handler.Handle(command, new CancellationToken(false));

            var aggregates = await context.Aggregate.ToListAsync();

            var events = await context.Events.ToListAsync();

            Assert.Single(aggregates);
            Assert.Single(events);
        }