Пример #1
0
        public void UpsertProduct(ProductToGet product)
        {
            var p = _context.Products.Find(product.ProductId);

            if (p == null)
            {
                p = new Product();
                _context.Products.Add(p);

                p.ProductId = product.ProductId;
                p.StoreId   = _configuration.GetSection("Store:Id").Get <Guid>();
            }

            p.Image = product.Image;
            p.Name  = product.Name;
            p.Price = product.Price;

            p.Ingredients.Clear();
            foreach (var item in product.Items)
            {
                p.Ingredients.Add(new Item {
                    ItemId = item.ItemId, Name = item.Name
                });
            }

            _context.SaveChanges();
        }
Пример #2
0
        public async Task Upsert(ProductToGet product)
        {
            if (!_memoryCache.TryGetValue(_cacheName, out Dictionary <Guid, ProductToGet> products))
            {
                products[product.ProductId] = product;
            }

            await Task.CompletedTask;
        }
 public static Product ToProduct(this ProductToGet productToGet)
 {
     return(new Product()
     {
         Id = productToGet.ProductId,
         Ingredients = "",
         Items = productToGet.Items.Select(i => new Item()
         {
             Id = i.ItemId
         }).ToList()
     });
 }
Пример #4
0
        public IActionResult Get(string productId)
        {
            var product = _repository.Get(productId);


            var productToGet = new ProductToGet()
            {
                ImagePath = product.ImagePath,
                Name      = product.Name,
                Price     = product.Price,
                ProductId = product.Id,
                Tax       = product.Tax
            };

            return(Ok(productToGet));
        }
Пример #5
0
        public async Task MergeProductWithIngredientsAsync(ProductToGet storeProduct)
        {
            var productIngredients = storeProduct.Items.Select(i => i.Name).ToList();
            var ingredients        = await _unitOfWork.IngredientsRepository.GetByNamesAsync(productIngredients);

            if (ingredients.Any() == false)
            {
                ingredients = _mapper.Map <IList <Ingredient> >(storeProduct.Items);
            }

            var productWithIngredients = new ProductWithIngredients
            {
                Id          = storeProduct.ProductId.ToString(),
                StoreId     = storeProduct.StoreId.ToString(),
                Ingredients = ingredients
            };

            await _unitOfWork.MergedProductsRepository.InsertOrUpdate(productWithIngredients);
        }
Пример #6
0
        public async Task Update(ProductToGet productToGet)
        {
            Product product = await _productRepository.Get(productToGet.ProductId);

            if (product == null)
            {
                return;
            }

            // Looking for items remove or changed
            var itemsToRemove = new List <Item>();

            foreach (var item in product.Items)
            {
                var itemToGet = productToGet.Items.FirstOrDefault(p => p.ItemId == item.Id);

                if (itemToGet == null)
                {
                    itemsToRemove.Add(item);
                }
                else
                {
                    item.Name = itemToGet.Name;
                }
            }

            // Seeking for items added
            foreach (var itemToGet in productToGet.Items)
            {
                if (!product.Items.Exists(i => i.Id == itemToGet.ItemId))
                {
                    product.AddItem(itemToGet.ItemId, itemToGet.Name);
                }
            }

            // Removing items
            foreach (var itemToRemove in product.Items)
            {
                product.RemoveItem(itemToRemove.Id);
            }

            await _productRepository.Save(product);
        }
        public async Task <IList <Product> > GetProductsAsync()
        {
            ProductToGet[] productsToGet = null;

            try
            {
                await _logServiceBus.SendMessagesAsync($"Recuperando produtos \"storeName = {_configuration["StoreInfo:StoreName"]}\"...");

                productsToGet = JsonConvert.DeserializeObject <ProductToGet[]>(await _httpClient.GetStringAsync($"{_configuration["API:Products"]}/?storeName={_configuration["StoreInfo:StoreName"]}"));
            }
            catch (Exception E)
            {
                await _logServiceBus.SendMessagesAsync($"Falha ao recuperar os dados dos produtos, segue a descrição \"{E.Message}\".");

                //Falha ao acessar o microserviço de produtos

                if (_configuration["API:mocked"] == "true")
                {
                    await _logServiceBus.SendMessagesAsync($"Criando dados fakes para produtos");

                    productsToGet = new ProductToGet[]
                    {
                        new ProductToGet()
                        {
                            ProductId = Guid.NewGuid(),
                            Items     = new List <ItemToGet>()
                            {
                                new ItemToGet()
                                {
                                    ItemId = Guid.NewGuid(), Name = "beef"
                                },
                                new ItemToGet()
                                {
                                    ItemId = Guid.NewGuid(), Name = "mustard"
                                },
                                new ItemToGet()
                                {
                                    ItemId = Guid.NewGuid(), Name = "bread"
                                }
                            }
                        },
                        new ProductToGet()
                        {
                            ProductId = Guid.NewGuid(),
                            Items     = new List <ItemToGet>()
                            {
                                new ItemToGet()
                                {
                                    ItemId = Guid.NewGuid(), Name = "ketchup"
                                },
                                new ItemToGet()
                                {
                                    ItemId = Guid.NewGuid(), Name = "bread"
                                }
                            }
                        },
                        new ProductToGet()
                        {
                            ProductId = Guid.NewGuid(),
                            Items     = new List <ItemToGet>()
                            {
                                new ItemToGet()
                                {
                                    ItemId = Guid.NewGuid(), Name = "salmon"
                                },
                                new ItemToGet()
                                {
                                    ItemId = Guid.NewGuid(), Name = "onion wings"
                                },
                                new ItemToGet()
                                {
                                    ItemId = Guid.NewGuid(), Name = "whole bread"
                                }
                            }
                        }
                    };
                }
                else
                {
                    throw;
                }
            }

            return(productsToGet.Select(x => x.ToProduct()).ToList());
        }