Exemple #1
0
        /// <inheritdoc />
        public async Task <BillDto> CreateBill(ShoppingBasketDto basket)
        {
            var bill = new BillDto
            {
                TotalPrice = 0
            };

            try
            {
                await ValidateBasket(basket).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                throw new InvalidBasketException("Invalid Basket", ex);
            }
            foreach (var item in basket.Articles)
            {
                var articleDto          = item.Article;
                var articleDiscountDto  = item.ArticleDiscount;
                var articlePriceDto     = item.ArticlePrice;
                var numberOfArticlesDto = item.NumberOfArticles;
                var article             = _mapper.Map <Article>(articleDto);
                var articleDiscount     = _mapper.Map <ArticleDiscount>(articleDiscountDto);

                bill.TotalPrice += CalculatePrice(numberOfArticlesDto.Value, articlePriceDto.UnitPrice.Value, articleDiscount);
            }
            return(bill);
        }
        public DiscountApplicationSteps()
        {
            this.shoppingBasketDto = new ShoppingBasketDto();
            this.mapperService     = new MapperService();
            this.efDbContext       = new EfDbContext();
            this.efDbSetWrapper    = new EfDbSetWrapper <ShoppingBasket>(efDbContext);

            this.shoppingBasketService = new ShoppingBasketService(shoppingBasketDto, mapperService, efDbSetWrapper, efDbContext);
        }
Exemple #3
0
        public void CreateDependenciesOfTests()
        {
            this.shoppingBasketDto = new ShoppingBasketDto();
            this.mapperService     = new MapperService();
            this.efDbContext       = new EfDbContext();
            this.efDbSetWrapper    = new EfDbSetWrapper <ShoppingBasket>(efDbContext);

            this.shoppingBasketService = new ShoppingBasketService(shoppingBasketDto, mapperService, efDbSetWrapper, efDbContext);
        }
Exemple #4
0
        internal async Task ValidateBasket(ShoppingBasketDto basket)
        {
            if (basket == null || basket.Articles == null || !basket.Articles.Any())
            {
                throw new InvalidBasketException("Basket is empty");
            }

            foreach (var item in basket.Articles)
            {
                var articleDto          = item.Article;
                var articleDiscountDto  = item.ArticleDiscount;
                var articlePriceDto     = item.ArticlePrice;
                var numberOfArticlesDto = item.NumberOfArticles;

                // check number of articles
                if (!numberOfArticlesDto.HasValue)
                {
                    throw new InvalidBasketException("Number of articles is invalid");
                }

                // check discount
                if (articleDiscountDto != null)
                {
                    ArticleDiscount articleDiscount = await _discountRepository.GetByIdAsync(articleDiscountDto.Id.Value).ConfigureAwait(false);

                    var articleDiscountDbDto = _mapper.Map <ArticleDiscountDto>(articleDiscount);
                    if (!articleDiscountDbDto.Equals(articleDiscountDto))
                    {
                        throw new InvalidDiscountException("ArticleDiscount is invalid");
                    }
                }

                // check article
                Article article = await _articleRepository.GetByIdAsync(articleDto.Id.Value).ConfigureAwait(false);

                var articleDbDto = _mapper.Map <ArticleDto>(article);
                if (!articleDbDto.Equals(articleDto))
                {
                    throw new InvalidArticleException("Article is invalid");
                }

                // check articlePrice
                var articlePrice = await _priceRepository.GetByIdAsync(articlePriceDto.Id.Value).ConfigureAwait(false);

                var articlePriceDbDto = _mapper.Map <ArticlePriceDto>(articlePrice);
                if (!articlePriceDbDto.Equals(articlePriceDto))
                {
                    throw new InvalidArticlePriceException("ArticlePrice is invalid");
                }
            }
        }
 public void CreateShoppingBasketDto()
 {
     this.shoppingBasketDto = new ShoppingBasketDto();
 }