Beispiel #1
0
        public async Task <Product> GetById(int Id)
        {
            var product = await _context.Products.FindAsync(Id);

            List <int> ingredientsId = await _context.IngredientFromProductProducts
                                       .Where(p => p.ProductsId == Id)
                                       .Select(p => p.IngredientsId)
                                       .ToListAsync();

            GetProductDto productDto = new GetProductDto()
            {
                Id          = product.Id,
                Name        = product.Name,
                Description = product.Description,
                Price       = product.Price,
                Discount    = product.Discount
            };

            foreach (int i in ingredientsId)
            {
                IngredientsFromProduct ingredient = await _context
                                                    .IngredientsFromProducts
                                                    .FindAsync(i);

                productDto.Ingredients
                .Add(new GetIngredientDto()
                {
                    Id = ingredient.Id, Name = ingredient.Name
                });
            }
            return(product);
        }
Beispiel #2
0
        public async Task <ActionResult> Add(IngredientsFromProduct ingredient)
        {
            bool existsIngredient = await _ingredientFromProductRepository.CreateIngredient(ingredient);

            if (existsIngredient)
            {
                return(CreatedAtAction("Get", new { Id = ingredient.Id }, ingredient));
            }
            return(UnprocessableEntity("Product already exists"));
        }
        public async Task <bool> CreateIngredient(IngredientsFromProduct ingredient)
        {
            bool existsIngredient = _context.IngredientsFromProducts.Any(p => p.Id == ingredient.Id);

            if (!existsIngredient)
            {
                _context.IngredientsFromProducts.Add(ingredient);
                await _context.SaveChangesAsync();

                return(true);
            }
            return(false);
        }
        public async Task <int> Handle(CreateIngredientCommand request, CancellationToken cancellation)
        {
            var ingredient = new IngredientsFromProduct
            {
                Id   = request.Id,
                Name = request.Name
            };
            await repository.AddAsync(ingredient);



            return(ingredient.Id);
        }
Beispiel #5
0
        public async Task <FileResult> Download()
        {
            StringBuilder        result       = new StringBuilder();
            var                  products     = repository.GetAll();
            List <GetProductDto> listProducts = new List <GetProductDto>();

            foreach (var product in products)
            {
                List <int> ingredientsId = await context.IngredientFromProductProducts
                                           .Where(p => p.ProductsId == product.Id)
                                           .Select(p => p.IngredientsId)
                                           .ToListAsync();

                GetProductDto productDto = new GetProductDto()
                {
                    Id          = product.Id,
                    Name        = product.Name,
                    Description = product.Description,
                    Price       = product.Price,
                    Discount    = product.Discount
                };
                foreach (int i in ingredientsId)
                {
                    IngredientsFromProduct ingredient = await context
                                                        .Ingredients
                                                        .FindAsync(i);

                    productDto.Ingredients
                    .Add(new GetIngredientDto()
                    {
                        Id = ingredient.Id, Name = ingredient.Name
                    });
                }
                listProducts.Add(productDto);
            }
            result.Append("Id, Name, Description, Price, Discount\n");
            foreach (GetProductDto p in listProducts)
            {
                result.Append(String.Format("{0}, {1}, {2}, {3}, {4}\n", p.Id, p.Name, p.Description, p.Price, p.Discount));
            }

            string fileName = "Products.csv";

            byte[] fileBytes = Encoding.ASCII.GetBytes(result.ToString());

            return(File(fileBytes, "text/csv", fileName)); // this is the key!
        }
        public async Task <IEnumerable <GetProductDto> > Handle(GetProductsByArray request, CancellationToken cancellation)
        {
            var products = repository.GetAll().ToList();

            products = products.Where(val => request.ListofIds.Contains(val.Id) == true).ToList();

            List <GetProductDto> listProducts = new List <GetProductDto>();

            foreach (var product in products)
            {
                List <int> ingredientsId = await context.IngredientFromProductProducts
                                           .Where(p => p.ProductsId == product.Id)
                                           .Select(p => p.IngredientsId)
                                           .ToListAsync();

                GetProductDto productDto = new GetProductDto()
                {
                    Id          = product.Id,
                    Name        = product.Name,
                    Description = product.Description,
                    Price       = product.Price,
                    Discount    = product.Discount
                };
                foreach (int i in ingredientsId)
                {
                    IngredientsFromProduct ingredient = await context
                                                        .Ingredients
                                                        .FindAsync(i);

                    productDto.Ingredients
                    .Add(new GetIngredientDto()
                    {
                        Id = ingredient.Id, Name = ingredient.Name
                    });
                }
                listProducts.Add(productDto);
            }
            return(listProducts.AsEnumerable());
        }
Beispiel #7
0
        public async Task <GetProductDto> Handle(GetProductByIdQuery request, CancellationToken cancellationToken)
        {
            var product = await productRepository.GetById(request.Id);

            if (product == null)
            {
                throw new Exception("Product doesn't exist");
            }


            List <int> ingredientsId = await context.IngredientFromProductProducts
                                       .Where(p => p.ProductsId == request.Id)
                                       .Select(p => p.IngredientsId)
                                       .ToListAsync();

            GetProductDto productDto = new GetProductDto()
            {
                Id          = product.Id,
                Name        = product.Name,
                Description = product.Description,
                Price       = product.Price,
                Discount    = product.Discount
            };

            foreach (int i in ingredientsId)
            {
                IngredientsFromProduct ingredient = await context
                                                    .Ingredients
                                                    .FindAsync(i);

                productDto.Ingredients
                .Add(new GetIngredientDto()
                {
                    Id = ingredient.Id, Name = ingredient.Name
                });
            }
            return(productDto);
        }
Beispiel #8
0
        public async Task <IEnumerable <GetProductDto> > GetAll()
        {
            List <Product> products = await _context.Products.ToListAsync();

            List <GetProductDto> productDtos = new List <GetProductDto>();

            foreach (Product product in products)
            {
                List <int> ingredientsId = await _context.IngredientFromProductProducts
                                           .Where(p => p.ProductsId == product.Id)
                                           .Select(p => p.IngredientsId)
                                           .ToListAsync();

                GetProductDto productDto = new GetProductDto()
                {
                    Id          = product.Id,
                    Name        = product.Name,
                    Description = product.Description,
                    Price       = product.Price,
                    Discount    = product.Discount
                };
                foreach (int i in ingredientsId)
                {
                    IngredientsFromProduct ingredient = await _context
                                                        .IngredientsFromProducts
                                                        .FindAsync(i);

                    productDto.Ingredients
                    .Add(new GetIngredientDto()
                    {
                        Id = ingredient.Id, Name = ingredient.Name
                    });
                }
                productDtos.Add(productDto);
            }
            return(productDtos);
        }