/// <summary>
        /// Создание заказа в базе данных
        /// </summary>
        /// <param name="products">Список продуктов в корзине</param>
        /// <returns></returns>
        public async Task <Order_DB> CreateOrderInDBAsync(IEnumerable <CartItem> products)
        {
            if (products == null || products.Count() == 0)
            {
                throw new ArgumentException("Products count should be more than thero.");
            }

            var order = new Order_DB
            {
                User_Id       = 2,
                Creation_Date = DateTime.Now,
                Status_Id     = 1
            };

            _shopDBContext.Shop_Orders.Add(order);

            for (int i = 0; i < products.Count(); i++)
            {
                order.Order_Composition.Add(new OrderComposition_DB
                {
                    Order_Id     = order.Id,
                    Product_Id   = products.ElementAt(i).ProductId,
                    ProductCount = products.ElementAt(i).Count
                });
            }

            await _shopDBContext.SaveChangesAsync();

            order.Status = await _shopDBContext.OrderStatus.Where(s => s.Id == order.Status_Id).SingleAsync();

            order.User = await _shopDBContext.Users.Where(u => u.Id == order.User_Id).SingleAsync();

            return(order);
        }
Exemple #2
0
        public async Task GetPageCountAsync_Success()
        => await WithDBContextAsync(async (context) =>
        {
            //Arrange
            var orders = new List <Order_DB>();
            for (int i = 1; i < 500; i++)
            {
                orders.Add(Order_DB.GetDefaultValue(i));
            }
            await context.Shop_Orders.AddRangeAsync(orders);
            await context.SaveChangesAsync();
            var service = new OrderService(context);

            //Act
            var count = await service.GetPageCountAsync();

            //Assert
            Assert.Equal(36, count);
        });
        private IEnumerable <Order_DB> CreateOrders(int count)
        {
            var orders = new List <Order_DB>();

            for (int i = 1; i <= count; i++)
            {
                var order = Order_DB.GetDefaultValue(i);
                order.Status = new OrderStatus_DB
                {
                    Id   = order.Status_Id,
                    Name = OrderStatus.Created.ToString()
                };
                order.User = User_DB.GetDefaultValue(order.User_Id);
                order.Order_Composition
                .ForEach(oc => oc.Product = Product_DB.GetDefaultValue(oc.Product_Id));
                orders.Add(order);
            }
            return(orders);
        }
Exemple #4
0
        public async Task GetOrdersByFilterAsync_ZeroPage_Exception()
        => await WithDBContextAsync(async (context) =>
        {
            //Arrange
            var orders = new List <Order_DB>();
            for (int i = 1; i < 500; i++)
            {
                orders.Add(Order_DB.GetDefaultValue(i));
            }
            await context.Shop_Orders.AddRangeAsync(orders);
            await context.SaveChangesAsync();
            var service = new OrderService(context);

            //Act
            Func <Task> act = () => service.GetOrdersByFilterAsync(0);

            //Assert
            var exception = await Assert.ThrowsAsync <ArgumentException>(act);
            Assert.Equal("Page is can`t be 0", exception.Message);
        });
Exemple #5
0
        public async Task GetOrdersByFilterAsync_Success(int page)
        => await WithDBContextAsync(async (context) =>
        {
            //Arrange
            var orders = new List <Order_DB>();
            for (int i = 1; i < 500; i++)
            {
                orders.Add(Order_DB.GetDefaultValue(i));
            }
            await context.Shop_Orders.AddRangeAsync(orders);
            await context.SaveChangesAsync();
            var service = new OrderService(context);

            //Act
            var result = await service.GetOrdersByFilterAsync(page);

            //Assert
            Assert.NotNull(result);
            Assert.Equal(14, result.Count());
        });