コード例 #1
0
        public async Task <bool> CreateOrder(Order order)
        {
            //Order
            DateTime date = new DateTime();

            date = DateTime.Now;

            Order tempOrder = new Order();

            tempOrder.OrderId         = Guid.NewGuid();
            tempOrder.CustomerId      = order.CustomerId;
            tempOrder.CreatedDate     = date;
            tempOrder.TotalPrice      = order.TotalPrice;
            tempOrder.ContactPhone    = order.ContactPhone;
            tempOrder.DeliveryName    = order.DeliveryName;
            tempOrder.DeliveryEmail   = order.DeliveryEmail;
            tempOrder.DeliveryAddress = order.DeliveryAddress;
            tempOrder.TotalPrice      = order.TotalPrice;
            tempOrder.DeliveryPrice   = order.DeliveryPrice;
            tempOrder.DeliveryDate    = date;
            tempOrder.StatusId        = Guid.Parse("A1AD8DEF-626A-4A06-B39C-956B6255C37C"); //Chưa thanh toán

            ctx.Order.Add(tempOrder);
            await ctx.SaveChangesAsync();

            //OrderDetail
            foreach (OrderProductSize item in order.OrderProductSize)
            {
                //Lấy giá
                Product product = new Product();
                product = await productRepository.GetById(item.ProductId);

                //OrderDetail
                OrderProductSize orderProductSize = new OrderProductSize();
                orderProductSize.OrderId   = tempOrder.OrderId;
                orderProductSize.SizeId    = item.SizeId;
                orderProductSize.ProductId = item.ProductId;
                orderProductSize.ColorId   = item.ColorId;
                orderProductSize.Quantity  = item.Quantity;
                orderProductSize.Price     = item.Price;

                //product.Discount == 0 ? product.Price * item.Quantity : (product.Price - (product.Price / 100 * (decimal)product.Discount)) * item.Quantity
                ctx.OrderProductSize.Add(orderProductSize);
                await ctx.SaveChangesAsync();

                //Trừ số lượng
                ProductSize productSize = new ProductSize();
                productSize = await ctx.ProductSize.Where(p => p.ColorId == item.ColorId && p.SizeId == item.SizeId && p.ProductId == item.ProductId)
                              .FirstOrDefaultAsync();

                productSize.InventoryQuantity = productSize.InventoryQuantity - item.Quantity;
                await ctx.SaveChangesAsync();
            }

            return(true);
        }
コード例 #2
0
        public async Task <List <CustomerOrderVM> > GetAllCustomerOrders(Guid customerId)
        {
            List <CustomerOrderVM> customerOrder = await ctx.Order.Where(o => o.CustomerId == customerId)
                                                   .Select(o => new CustomerOrderVM
            {
                OrderId          = o.OrderId,
                CustomerId       = o.CustomerId,
                CreatedDate      = o.CreatedDate,
                TotalPrice       = o.TotalPrice,
                ContactPhone     = o.ContactPhone,
                DeliveryName     = o.DeliveryName,
                DeliveryEmail    = o.DeliveryEmail,
                DeliveryAddress  = o.DeliveryAddress,
                DeliveryPrice    = o.DeliveryPrice,
                DeliveryDate     = o.DeliveryDate,
                NumberOfProducts = o.OrderProductSize.Count,
                StatusId         = o.StatusId,
                StatusName       = o.Status.Name
            })
                                                   .OrderByDescending(o => o.CreatedDate)
                                                   .ToListAsync();

            List <Guid> orderIds = customerOrder.Select(o => o.OrderId)
                                   .ToList();

            foreach (Guid guid in orderIds)
            {
                OrderProductSize orderProductSize = await ctx.OrderProductSize.Where(o => o.OrderId == guid).FirstOrDefaultAsync();

                CustomerOrderVM customerOrderVM = customerOrder.Where(o => o.OrderId == guid).FirstOrDefault();
                customerOrderVM.FirstProductName = await ctx.ProductSize.Where(p => p.ColorId == orderProductSize.ColorId && p.ProductId == orderProductSize.ProductId && p.SizeId == orderProductSize.SizeId)
                                                   .Select(p => p.ProductColor.Product.Name)
                                                   .FirstOrDefaultAsync();
            }

            return(customerOrder);
        }