コード例 #1
0
ファイル: OrderController.cs プロジェクト: binhtv1997/GiatUi
        public ActionResult CreateOrder([FromBody] OrderCM model)
        {
            var timeCreate = DateTime.Now;
            var checkSlot  = _slotService.GetSlots(s => s.TimeStart.Date == timeCreate.Date).Where(t1 => t1.TimeStart.TimeOfDay <= timeCreate.TimeOfDay).Where(t2 => t2.TimeEnd.TimeOfDay >= timeCreate.TimeOfDay).ToList();

            if (checkSlot.Any())
            {
                var checkCus = _customerService.GetCustomer(model.CustomerId.Value);
                if (checkCus == null)
                {
                    return(NotFound());
                }

                Order newOrder = model.Adapt <Order>();
                newOrder.IsDelete   = false;
                newOrder.SlotTakeId = checkSlot[0].Id;
                _orderService.CreateOrder(newOrder);
                _orderService.Save();

                foreach (var item in model.ListOrderServices)
                {
                    OrderService orderService = item.Adapt <OrderService>();
                    orderService.OrderId = newOrder.Id;
                    _orderServiceService.CreateOrderService(orderService);
                }
                _orderServiceService.Save();
                return(Ok(200));
            }
            else
            {
                return(BadRequest("Dont Have Any Slot"));
            }
        }
コード例 #2
0
        public async Task <ActionResult> createOrder(OrderCM model)
        {
            double totalAmount = 0;

            foreach (var item in model.OrderDetailCMs)
            {
                if (item.Quantity <= 0 || item.Quantity > 5)
                {
                    return(BadRequest());
                }
                var product = _productService.GetProduct(item.ProductId);
                if (product == null)
                {
                    return(BadRequest());
                }
                totalAmount += product.CurrentPrice * item.Quantity;
            }
            var user = await _userManager.GetUserAsync(User);

            var order = model.Adapt <Order>();

            order.BuyerId       = user.Id;
            order.TotalAmount   = totalAmount;
            order.CurrentStatus = (int)OrderCurrentStatus.received;
            _orderService.CreateOrder(order);
            foreach (var item in model.OrderDetailCMs)
            {
                var orderDetail = item.Adapt <OrderDetail>();
                orderDetail.OrderId = order.Id;
                _detailService.CreateOrderDetail(orderDetail);
            }
            _orderService.SaveChanges();
            OrderStatus status = new OrderStatus();

            status.OrderId  = order.Id;
            status.StatusId = (int)OrderStatusId.start;
            _statusService.CreateOrderStatus(status);
            _statusService.SaveChanges();
            return(Ok());
        }