Ejemplo n.º 1
0
        public async Task <List <ProductViewModel> > GetAllProductsAsync(CancellationToken ct = default)
        {
            // retrieve all products that are available
            List <Product> products = await this._productRepository.GetAllAsync(ct);

            List <ProductViewModel> productsView = new List <ProductViewModel>();

            // check if item has any sales
            foreach (Product product in products)
            {
                ProductViewModel productView = ProductConverter.Convert(product);
                if (product.IsOnSale == true)
                {
                    List <SaleItem> sales = await this._saleItemRepository.GetByProductId(product.Id);

                    List <SaleItemViewModel> saleViews = new List <SaleItemViewModel>();
                    foreach (SaleItem sale in sales)
                    {
                        // if sale is valid
                        if (sale.ExpireDate > DateTime.Now)
                        {
                            SaleItemViewModel saleView = SaleItemConverter.Convert(sale);
                            saleViews.Add(saleView);
                        }
                    }
                    productView.Sales = saleViews;
                }

                productsView.Add(productView);
            }

            return(productsView);
        }
Ejemplo n.º 2
0
        private async Task <CustomProductViewModel> GetCustomSackProdctAsync(CancellationToken ct = default)
        {
            CustomProduct customSack = await this._customProductRepository.GetCustomSackAsync(ct);

            List <MixCategory> mixCategories = await this._mixCategoryRepository.GetAllByProductIdAsync(customSack.Id);

            foreach (MixCategory category in mixCategories)
            {
                category.Ingredients = await this._ingredientRepository.GetAllByMixCategoryIdAsync(category.Id, ct);
            }

            customSack.MixCategories = mixCategories;

            CustomProductViewModel customSackView = CustomProductConverter.Convert(customSack);

            if (customSackView.IsOnSale == true)
            {
                List <SaleItem> sales = await this._saleItemRepository.GetByCustomProductId(customSack.Id);

                List <SaleItemViewModel> saleViews = new List <SaleItemViewModel>();
                foreach (SaleItem sale in sales)
                {
                    SaleItemViewModel saleView = SaleItemConverter.Convert(sale);
                    // if sale item is a promo code
                    if (sale.Type == OfferType.PromoCode)
                    {
                        // ensure promo code id exists on
                        if (String.IsNullOrEmpty(sale.PromoCodeId) == false)
                        {
                            PromoCodeViewModel promoViewModel = PromoCodeConverter.Convert(await this._promoCodeRepository.GetByIdAsync(sale.PromoCodeId, ct));
                            // check if promo is valid
                            if (promoViewModel.ExpireDate > DateTime.Now)
                            {
                                saleView.PromoCode = promoViewModel;
                                saleViews.Add(saleView);
                            }
                        }
                    }
                    else
                    {
                        // if sale is valid
                        if (sale.ExpireDate > DateTime.Now)
                        {
                            saleViews.Add(saleView);
                        }
                    }
                }
                customSackView.Sales = saleViews;
            }

            return(customSackView);
        }