public async Task ReturnsTotalCost_SingleItem_SingleQuantity()
            {
                var itemRepository     = new InMemoryItemRepository();
                var itemReadRepository = new InMemoryItemReadRepository();

                itemRepository.EventOccurred += async entity => await new ItemReadModelHandler(itemReadRepository).Handle(entity.NewEvents.ToList <object>());

                var itemController =
                    new ItemController(itemRepository, itemReadRepository)
                    .BootstrapForTests();

                var postNewPotatoItemDto = new PostRequestDto
                {
                    Code  = "potato",
                    Price = 30
                };

                var postPotatoResult = await itemController.Post(postNewPotatoItemDto);

                var potatoDto = (ItemDto)postPotatoResult.Value;

                var calculatorController = new CalculatorController(itemRepository, new InMemoryCouponReadRepository())
                                           .BootstrapForTests();

                var calculatorComputeCostRequestDto =
                    new CalculatorComputeCostRequestDto
                {
                    ShoppingItems =
                        new List <ShoppingItemDto>
                    {
                        new ShoppingItemDto
                        {
                            Id       = potatoDto.Id,
                            Quantity = 1
                        }
                    }
                };

                var result = await calculatorController.ComputeCost(calculatorComputeCostRequestDto);

                Assert.Equal((int)HttpStatusCode.OK, result.StatusCode);
                Assert.NotNull(result.Value);

                var dto = (CalculatorComputeCostDto)result.Value;

                Assert.Equal(30, dto.TotalCost);
            }
Ejemplo n.º 2
0
        public async Task <ObjectResult> Post([FromBody] PostRequestDto postRequestDto)
        {
            var existingItem = await _readRepository.GetAsync(postRequestDto.Code);

            if (existingItem != null)
            {
                var selfUrl = Url.Action("GetByItemCode", new { code = existingItem.Code });

                return(new SeeOtherObjectResult(selfUrl, MapToDto(existingItem)));
            }
            else
            {
                var entity = new Model.Item(
                    id: Guid.NewGuid(),
                    code: postRequestDto.Code,
                    price: postRequestDto.Price,
                    itemTypeId: postRequestDto.ItemTypeId);
                await _repository.SaveAsync(entity, DateTimeOffset.UtcNow);

                var selfUrl = Url.Action("GetByItemCode", new { code = entity.Code });
                return(Created(selfUrl, MapToDto(entity)));
            }
        }
            public async Task ApplyHighestDiscount_TwoItems_DiscountedOneItemByFixedAmountAndPercentage()
            {
                var itemRepository     = new InMemoryItemRepository();
                var itemReadRepository = new InMemoryItemReadRepository();

                itemRepository.EventOccurred += async entity => await new ItemReadModelHandler(itemReadRepository).Handle(entity.NewEvents.ToList <object>());

                var itemController =
                    new ItemController(itemRepository, itemReadRepository)
                    .BootstrapForTests();

                var postNewPotatoItemDto = new PostRequestDto
                {
                    Code  = "potato",
                    Price = 30
                };

                var postPotatoResult = await itemController.Post(postNewPotatoItemDto);

                var potatoDto = (ItemDto)postPotatoResult.Value;

                var postNewLettuceItemDto = new PostRequestDto
                {
                    Code  = "lettuce",
                    Price = 50
                };

                var postLettuceResult = await itemController.Post(postNewLettuceItemDto);

                var lettuceDto = (ItemDto)postLettuceResult.Value;

                var setDiscountOnPotatoDto = new SetDiscountRequestDto
                {
                    AmountOff = 5
                };

                await itemController.SetDiscount(potatoDto.Id, setDiscountOnPotatoDto);

                var calculatorController = new CalculatorController(itemRepository, new InMemoryCouponReadRepository())
                                           .BootstrapForTests();

                var calculatorComputeCostRequestDto =
                    new CalculatorComputeCostRequestDto
                {
                    ShoppingItems =
                        new List <ShoppingItemDto>
                    {
                        new ShoppingItemDto
                        {
                            Id       = potatoDto.Id,
                            Quantity = 3
                        },
                        new ShoppingItemDto
                        {
                            Id       = lettuceDto.Id,
                            Quantity = 1
                        }
                    }
                };

                var result = await calculatorController.ComputeCost(calculatorComputeCostRequestDto);

                Assert.Equal((int)HttpStatusCode.OK, result.StatusCode);
                Assert.NotNull(result.Value);

                var dto = (CalculatorComputeCostDto)result.Value;

                Assert.Equal(125, dto.TotalCost);
            }
            public async Task ApplyHighestDiscount_HasAmountOffHasPercentOffHasCoupon(
                string testName,
                double itemPercentOff,
                decimal itemAmountOff,
                double couponPercentOff,
                decimal expectedTotalCost)
            {
                var itemRepository     = new InMemoryItemRepository();
                var itemReadRepository = new InMemoryItemReadRepository();

                itemRepository.EventOccurred += async entity => await new ItemReadModelHandler(itemReadRepository).Handle(entity.NewEvents.ToList <object>());

                var itemController =
                    new ItemController(itemRepository, itemReadRepository)
                    .BootstrapForTests();

                var postNewPotatoItemDto = new PostRequestDto
                {
                    Code  = "potato",
                    Price = 50
                };

                var postPotatoResult = await itemController.Post(postNewPotatoItemDto);

                var potatoDto = (ItemDto)postPotatoResult.Value;

                var setDiscountOnPotatoDto = new SetDiscountRequestDto
                {
                    PercentOff = itemPercentOff,
                    AmountOff  = itemAmountOff
                };

                await itemController.SetDiscount(potatoDto.Id, setDiscountOnPotatoDto);

                var couponRepository     = new InMemoryCouponRepository();
                var couponReadRepository = new InMemoryCouponReadRepository();

                couponRepository.EventOccurred += async entity =>
                                                  await new CouponReadModelHandler(couponReadRepository).Handle(entity.NewEvents.ToList <object>());

                var couponController = new CouponController(couponRepository)
                                       .BootstrapForTests();

                var postRequestDto = new ShoppingCartApi.Controllers.Coupon.PostRequestDto
                {
                    Code       = "GRAND_SALE",
                    PercentOff = couponPercentOff
                };

                await couponController.Post(postRequestDto);

                var calculatorController =
                    new CalculatorController(itemRepository, couponReadRepository)
                    .BootstrapForTests();

                var calculatorComputeCostRequestDto =
                    new CalculatorComputeCostRequestDto
                {
                    CouponCode    = "GRAND_SALE",
                    ShoppingItems =
                        new List <ShoppingItemDto>
                    {
                        new ShoppingItemDto
                        {
                            Id       = potatoDto.Id,
                            Quantity = 2
                        }
                    }
                };

                var result = await calculatorController.ComputeCost(calculatorComputeCostRequestDto);

                Assert.Equal((int)HttpStatusCode.OK, result.StatusCode);
                Assert.NotNull(result.Value);

                var dto = (CalculatorComputeCostDto)result.Value;

                Assert.Equal(expectedTotalCost, dto.TotalCost);
            }
            public async Task ApplyDiscount_FromCoupon()
            {
                var itemRepository     = new InMemoryItemRepository();
                var itemReadRepository = new InMemoryItemReadRepository();

                itemRepository.EventOccurred += async entity => await new ItemReadModelHandler(itemReadRepository).Handle(entity.NewEvents.ToList <object>());

                var itemController =
                    new ItemController(itemRepository, itemReadRepository)
                    .BootstrapForTests();

                var postNewPotatoItemDto = new PostRequestDto
                {
                    Code  = "potato",
                    Price = 30
                };

                var postPotatoResult = await itemController.Post(postNewPotatoItemDto);

                var potatoDto = (ItemDto)postPotatoResult.Value;

                var postNewLettuceItemDto = new PostRequestDto
                {
                    Code  = "lettuce",
                    Price = 50
                };

                var postLettuceResult = await itemController.Post(postNewLettuceItemDto);

                var lettuceDto = (ItemDto)postLettuceResult.Value;

                var couponRepository     = new InMemoryCouponRepository();
                var couponReadRepository = new InMemoryCouponReadRepository();

                couponRepository.EventOccurred += async entity =>
                                                  await new CouponReadModelHandler(couponReadRepository).Handle(entity.NewEvents.ToList <object>());

                var couponController = new CouponController(couponRepository)
                                       .BootstrapForTests();

                var postRequestDto = new ShoppingCartApi.Controllers.Coupon.PostRequestDto
                {
                    Code       = "GRAND_SALE",
                    PercentOff = 20
                };

                await couponController.Post(postRequestDto);

                var calculatorController =
                    new CalculatorController(itemRepository, couponReadRepository)
                    .BootstrapForTests();

                var calculatorComputeCostRequestDto =
                    new CalculatorComputeCostRequestDto
                {
                    CouponCode    = "GRAND_SALE",
                    ShoppingItems =
                        new List <ShoppingItemDto>
                    {
                        new ShoppingItemDto
                        {
                            Id       = potatoDto.Id,
                            Quantity = 3
                        },
                        new ShoppingItemDto
                        {
                            Id       = lettuceDto.Id,
                            Quantity = 1
                        }
                    }
                };

                var result = await calculatorController.ComputeCost(calculatorComputeCostRequestDto);

                Assert.Equal((int)HttpStatusCode.OK, result.StatusCode);
                Assert.NotNull(result.Value);

                var dto = (CalculatorComputeCostDto)result.Value;

                Assert.Equal(112, dto.TotalCost);
            }