Ejemplo n.º 1
0
        public async Task AddAsync_ShouldAddProduct_WhenProductIsValid()
        {
            var product = new Product("testProduct", 100, 10, 50, 10,
                                      CategoryOfProduct.GetCategory(CategoryOfProductId.Bread.ToString()));

            await _sut.AddAsync(product);

            _fixture.Products.Count(p => p.Name == product.Name).ShouldBe(1);
        }
Ejemplo n.º 2
0
        public async Task <int> AddAsync(string name, double calories, double proteins,
                                         double carbohydrates, double fats, string categoryName)
        {
            var product = await _productRepository.CheckIfExistsAsync(name);

            product = new Product(name, calories, proteins, carbohydrates, fats, CategoryOfProduct.GetCategory(categoryName));

            await _productRepository.AddAsync(product);

            return(product.Id);
        }
Ejemplo n.º 3
0
        public async Task DeleteAsync_ShouldRemoveProduct_WhenProductIsValid()
        {
            var product = new Product("testProduct", 100, 10, 50, 10,
                                      CategoryOfProduct.GetCategory(CategoryOfProductId.Bread.ToString()));

            await _sut.AddAsync(product);

            await _sut.DeleteAsync(product);

            _fixture.Products.Count().ShouldBe(0);
        }
Ejemplo n.º 4
0
        public async Task AddRangeAsync_ShouldAddListOfProduct_WhenProductsAreValid()
        {
            var products = new List <Product>
            {
                new Product("testProduct1", 100, 10, 50, 10,
                            CategoryOfProduct.GetCategory(CategoryOfProductId.Bread.ToString())),

                new Product("testProduct2", 100, 10, 50, 10,
                            CategoryOfProduct.GetCategory(CategoryOfProductId.Bread.ToString()))
            };

            await _sut.AddRangeAsync(products);

            _fixture.Products.Count().ShouldBe(2);
        }
Ejemplo n.º 5
0
        public async Task GetAsyncByName_ShouldReturnProduct_WhenProductExists()
        {
            var product = new Product("testProduct", 100, 10, 50, 10,
                                      CategoryOfProduct.GetCategory(CategoryOfProductId.Bread.ToString()));

            await _sut.AddAsync(product);

            var returnProduct = await _sut.GetAsync(product.Name);

            returnProduct.Id.ShouldBe(product.Id);
            returnProduct.Name.ShouldBe(product.Name);
            returnProduct.Calories.ShouldBe(product.Calories);
            returnProduct.Proteins.ShouldBe(product.Proteins);
            returnProduct.Carbohydrates.ShouldBe(product.Carbohydrates);
            returnProduct.Fats.ShouldBe(product.Fats);
        }
Ejemplo n.º 6
0
        public async Task UpdateAsync_ShouldUpdateProduct_WhenProductIsValid()
        {
            var product = new Product("testProduct", 100, 10, 50, 10,
                                      CategoryOfProduct.GetCategory(CategoryOfProductId.Bread.ToString()));

            await _sut.AddAsync(product);

            product.SetFats(30);
            product.SetCalories(15);

            await _sut.UpdateAsync(product);

            var returnProduct = await _fixture.Products.FindAsync(product.Id);

            returnProduct.Fats.ShouldBe(30);
            returnProduct.Calories.ShouldBe(15);
        }
Ejemplo n.º 7
0
        public async Task GetAllAsync_ShouldReturnAllProducts_WhenParametersAreOmitted()
        {
            var products = new List <Product>
            {
                new Product("testProduct", 100, 10, 50, 10,
                            CategoryOfProduct.GetCategory(CategoryOfProductId.Bread.ToString())),

                new Product("testProduct2", 100, 10, 50, 10,
                            CategoryOfProduct.GetCategory(CategoryOfProductId.Bread.ToString()))
            };

            await _sut.AddRangeAsync(products);

            var returnProducts = await _sut.GetAllAsync(null, null, new PaginationQuery());

            returnProducts.Items.Count.ShouldBe(2);
            returnProducts.ShouldBeOfType(typeof(PagedList <Product>));
        }
Ejemplo n.º 8
0
        public async Task <PagedList <Product> > GetAllAsync(string name, string category, PaginationQuery paginationQuery)
        {
            IQueryable <Product> query = _context.Products.Include(p => p.CategoryOfProduct)
                                         .AsNoTracking();

            if (name is not null)
            {
                query = query.Where(p => p.Name.Contains(name));
            }

            if (category is not null)
            {
                query = query.Where(p => p.CategoryOfProduct.Name == CategoryOfProduct.GetCategory(category).Name);
            }

            return(await PagedList <Product> .ToPagedList(query.OrderBy(x => x.Name),
                                                          paginationQuery.PageNumber,
                                                          paginationQuery.PageSize));
        }
Ejemplo n.º 9
0
        public async Task GetAllAsync_ShouldReturnProductsFromGivenCategory_WhenCategoryIsGiven()
        {
            var products = new List <Product>
            {
                new Product("testProduct", 100, 10, 50, 10,
                            CategoryOfProduct.GetCategory(CategoryOfProductId.Bread.ToString())),

                new Product("testProduct2", 100, 10, 50, 10,
                            CategoryOfProduct.GetCategory(CategoryOfProductId.Bread.ToString())),

                new Product("testProduct3", 100, 20, 30, 15,
                            CategoryOfProduct.GetCategory(CategoryOfProductId.Drinks.ToString()))
            };

            await _sut.AddRangeAsync(products);

            var returnProducts = await _sut.GetAllAsync(null, CategoryOfProductId.Drinks.ToString(), new PaginationQuery());

            returnProducts.Items.Count.ShouldBe(1);
        }
Ejemplo n.º 10
0
        public async Task UpdateAsync(int id, string name, double calories, double proteins,
                                      double carbohydrates, double fats, string categoryName)
        {
            var product = await _productRepository.GetOrFailAsync(id);

            // If product has the same name, ensure that name will be searching for product with this name
            if (await _productRepository.AnyAsync(p => p.Name == name))
            {
                if (product.Name != name)
                {
                    throw new NameInUseException(nameof(Product), name);
                }
            }

            product.SetName(name);
            product.SetCalories(calories);
            product.SetProteins(proteins);
            product.SetCarbohydrates(carbohydrates);
            product.SetFats(fats);
            product.CategoryOfProduct.Id = CategoryOfProduct.GetCategory(categoryName).Id;

            await _productRepository.UpdateAsync(product);
        }