public async Task <RecommendsMarket> GetRecommendsMarket(List <string> itemsPurchase)
        {
            var recommendsMarketFirebase = await _recommendsMarketRepository.GetRecommendsMarket();

            if (recommendsMarketFirebase.Items.Count == 0)
            {
                var productsFirebase = await MergeProductsByMarket();

                var itemsRecommends = await GetProductsRecommends(productsFirebase);

                recommendsMarketFirebase.Items = itemsRecommends;
                await _recommendsMarketRepository.SaveAsync(recommendsMarketFirebase);
            }
            var recommendsMarket = new RecommendsMarket {
                Items = new List <RecommendsMarketItem>()
            };
            Dictionary <string, int> markets = new Dictionary <string, int>();

            foreach (var item in itemsPurchase)
            {
                var product = recommendsMarketFirebase.Items.FirstOrDefault(x => x.SearchableName == item);
                if (product == null)
                {
                    continue;
                }

                recommendsMarket.Items.Add(product);
                GetBestShoppingMarket(markets, product);
            }
            recommendsMarket.Market     = markets.OrderByDescending(x => x.Value).FirstOrDefault().Key;
            recommendsMarket.Message    = GetAmountMarketByProducts(markets);
            recommendsMarket.TotalPrice = recommendsMarket.Items.Sum(x => x.Price);
            return(recommendsMarket);
        }
        public async Task <RecommendsMarket> GetProductsByMarket(List <string> itemsPurchase, string market)
        {
            if (string.IsNullOrEmpty(market))
            {
                return new RecommendsMarket {
                           Message = "Mercado não informado", Items = new List <RecommendsMarketItem>()
                }
            }
            ;

            var productsFirebase = await _productRepository.GetAsync(market);

            var recommendsMarket = new RecommendsMarket {
                Items = new List <RecommendsMarketItem>()
            };

            foreach (var item in itemsPurchase)
            {
                var product = productsFirebase.FirstOrDefault(x => x.SearchableName == item);

                if (product == null)
                {
                    continue;
                }

                recommendsMarket.Items.Add(FormatProductToRecommendMarkeItem(product));
            }

            recommendsMarket.Market     = market;
            recommendsMarket.TotalPrice = recommendsMarket.Items.Sum(x => x.Price);
            return(recommendsMarket);
        }
    }
 public async Task SaveAsync(RecommendsMarket recommendsMarket)
 {
     var db = _firebaseConnection.Open();
     DocumentReference docRef = db.Collection("recommendsMarket").Document("recommendation");
     await docRef.SetAsync(recommendsMarket);
 }
        public RecommendsMarketServiceTest()
        {
            _productRepositoryMock = new Mock <IProductRepository>();
            _products = new List <Product>
            {
                new Product
                {
                    Name                    = "Agua",
                    Market                  = "tonin",
                    Price                   = 2.05d,
                    PenultimatePrice        = 2.05d,
                    SearchableName          = "agua",
                    TemOferta               = false,
                    DateOfLastPurchase      = "26/08/2020",
                    DatePenultimatePurchase = "26/08/2020"
                },
                new Product
                {
                    Name                    = "Linguica",
                    Market                  = "tonin",
                    Price                   = 10.05d,
                    PenultimatePrice        = 10.05d,
                    SearchableName          = "linguica",
                    TemOferta               = false,
                    DateOfLastPurchase      = "26/08/2020",
                    DatePenultimatePurchase = "26/08/2020"
                },
                new Product
                {
                    Name                    = "Agua",
                    Market                  = "savegnago",
                    Price                   = 1.95d,
                    PenultimatePrice        = 2.07d,
                    SearchableName          = "agua",
                    TemOferta               = true,
                    DateOfLastPurchase      = "26/08/2020",
                    DatePenultimatePurchase = "26/08/2020"
                },
                new Product
                {
                    Name                    = "Linguica",
                    Market                  = "savegnago",
                    Price                   = 11.05d,
                    PenultimatePrice        = 9.95d,
                    SearchableName          = "linguica",
                    TemOferta               = false,
                    DateOfLastPurchase      = "26/08/2020",
                    DatePenultimatePurchase = "26/08/2020"
                },
                new Product
                {
                    Name                    = "Iorgute",
                    Market                  = "tonin",
                    Price                   = 1.54d,
                    PenultimatePrice        = 2.07d,
                    SearchableName          = "iorgute",
                    TemOferta               = false,
                    DateOfLastPurchase      = "26/08/2020",
                    DatePenultimatePurchase = "26/08/2020"
                },
            };

            _productRepositoryMock.Setup(x => x.GetAsync(It.IsAny <string>())).Returns(Task.FromResult <ICollection <Product> >(_products));

            _searchableNames = new List <string>();
            _searchableNames.Add("linguica");
            _searchableNames.Add("agua");
            _searchableNames.Add("iorgute");
            _productRepositoryMock.Setup(x => x.GetSearchableNamesAsync()).Returns(Task.FromResult <ICollection <string> >(_searchableNames));

            _productRepositoryMock.Setup(x => x.AddAsync(new List <Product>(), "")).Returns(Task.FromResult(new { }));
            _productRepositoryMock.Setup(x => x.AddSearchableNamesAsync(new List <string>())).Returns(Task.FromResult(new { }));
            _productRepositoryMock.Setup(x => x.GetMarkets()).Returns(Task.FromResult <ICollection <string> >(new List <string> {
                "tonin", "savegnago"
            }));

            _recommendsMarket = new RecommendsMarket {
                Items = new List <RecommendsMarketItem>()
            };
            _recommendsMarketRepositoryMock = new Mock <IRecommendsMarketRepository>();
            _recommendsMarketRepositoryMock.Setup(x => x.GetRecommendsMarket()).Returns(Task.FromResult <RecommendsMarket>(_recommendsMarket));
            _recommendsMarketRepositoryMock.Setup(x => x.SaveAsync(It.IsAny <RecommendsMarket>())).Returns(Task.FromResult(new { }));

            _recommendsMarketService = new RecommendsMarketService(_productRepositoryMock.Object, _recommendsMarketRepositoryMock.Object);

            _request = new List <string>();
            _request.Add("linguica");
            _request.Add("agua");
            _request.Add("coca-cola");
            _request.Add("iorgute");
        }