Ejemplo n.º 1
0
        public async Task <ActionResult <Order> > PostOrder(Order order)
        {
            var item = _context.Item.FirstOrDefault(x => x.Sku.ToLower() == order.Sku.ToLower());

            if (item == null)
            {
                return(new ConflictResult());
            }

            if (item.StockQuantity < order.Quantity)
            {
                return(new ConflictResult());
            }

            var userExists = _context.User.Any(x => x.Id == order.UserId);

            if (!userExists)
            {
                return(new ForbidResult());
            }

            item.StockQuantity -= order.Quantity;
            order.Modified      = DateTime.Now;
            order.Status        = (int)Enumerations.OrderStatus.Active;

            _context.Entry(item).State = EntityState.Modified;
            _context.Order.Add(order);
            _context.SaveChanges();

            var logOrder = new LogOrder()
            {
                Sku      = order.Sku,
                Modified = DateTime.Now,
                OrderId  = order.Id,
                UserId   = order.UserId,
                Quantity = order.Quantity,
                Status   = order.Status
            };

            _context.LogOrder.Add(logOrder);
            _context.SaveChanges();

            return(CreatedAtAction("GetOrder", new { id = order.Id }, order));
        }
Ejemplo n.º 2
0
 public void Commit()
 {
     Context.SaveChanges();
 }