コード例 #1
0
ファイル: OrderController.cs プロジェクト: deviladam/Onlab
        public IActionResult OrderAdd(OrderTimes order)
        {
            if (order.ReleaseTime < DateTime.Now)
            {
                ModelState.AddModelError(string.Empty, "Túl korai az átvétel időpontja.");
                return(View());
            }

            if (order.ReleaseTime > order.Deadline)
            {
                ModelState.AddModelError(string.Empty, "Visszahozatal időpontja nem valós.");
                return(View());
            }
            var userId = User.Claims.FirstOrDefault((c => c.Type == ClaimTypes.NameIdentifier)).Value;

            _orderManager.TakeCartOrder(order, userId);
            return(RedirectToAction(nameof(ListOrders)));
        }
コード例 #2
0
        public int TakeCartOrder(OrderTimes orderDetails, string userId)
        {
            var cart = _dbContext.Carts.Where(c => c.ApplicationUserId == userId).SingleOrDefault();

            if (cart == null)
            {
                return(1);
            }
            int   price = 0;
            Order order = new Order
            {
                ReleaseTime       = orderDetails.ReleaseTime,
                Deadline          = orderDetails.Deadline,
                Status            = 0,
                ApplicationUserId = userId
            };
            var dbOrder = _dbContext.Add(order).Entity;

            foreach (var item in cart.Items)
            {
                OrderdItem orderdItem = new OrderdItem
                {
                    Quantity = item.Quantity,
                    ItemId   = item.ItemId,
                    OrderId  = order.Id
                };
                price += item.Quantity * item.Item.Price;
                _dbContext.Add(orderdItem);
                _dbContext.Remove(item);
            }
            _dbContext.Remove(cart);
            TimeSpan substract = orderDetails.Deadline.Subtract(orderDetails.ReleaseTime);

            dbOrder.Price = (substract.Days + (substract.Minutes > 0?1:0)) * price;
            _dbContext.SaveChanges();
            return(0);
        }