Exemplo n.º 1
0
        public async Task <ICollection <Cart> > GetRandomCartsAsync()
        {
            var rand            = new Random();
            var searchEngine    = new SimilarProductSearchEngine();
            var cartSize        = 5;
            var victoryShopInfo = await _ctx.Shops.FirstOrDefaultAsync(s => s.Brand == ShopBrand.Victory);

            var ybitanShopInfo = await _ctx.Shops.FirstOrDefaultAsync(s => s.Brand == ShopBrand.YBitan);

            var coobShopInfo = await _ctx.Shops.FirstOrDefaultAsync(s => s.Brand == ShopBrand.Coob);

            var result = new List <Cart>()
            {
                new Cart()
                {
                    Products = new List <Product>(), Shop = victoryShopInfo
                },
                new Cart()
                {
                    Products = new List <Product>(), Shop = ybitanShopInfo
                },
                new Cart()
                {
                    Products = new List <Product>(), Shop = coobShopInfo
                },
            };
            var products = await Task.WhenAll(new ProductContext().Products.Include(p => p.Shop).Where(p => p.Shop.Brand == ShopBrand.Victory).ToListAsync(),
                                              new ProductContext().Products.Include(p => p.Shop).Where(p => p.Shop.Brand == ShopBrand.YBitan).ToListAsync(),
                                              new ProductContext().Products.Include(p => p.Shop).Where(p => p.Shop.Brand == ShopBrand.Coob).ToListAsync());

            var victoryProducts = products[0];
            var ybitanProducts  = products[1];
            var coobProducts    = products[2];

            for (var i = 0; i < cartSize; i++)
            {
                var victoryProduct = victoryProducts[rand.Next(victoryProducts.Count)];
                var ybitanProduct  = searchEngine.Search(ybitanProducts, victoryProduct);
                var coobProduct    = searchEngine.Search(coobProducts, victoryProduct);

                result[0].Products.Add(victoryProduct);
                result[1].Products.Add(ybitanProduct);
                result[2].Products.Add(coobProduct);
            }
            return(result);
        }
Exemplo n.º 2
0
 public ProductsRepository(ProductContext ctx)
 {
     _ctx           = ctx;
     _similarSearch = new SimilarProductSearchEngine();
 }