예제 #1
0
        public void Execute(OrderDto request)
        {
            _validator.ValidateAndThrow(request);


            using (var transaction = _context.Database.BeginTransaction())
            {
                var product = _context.Products.Find(request.ProductId);
                var order   = new Order
                {
                    Address   = request.Address,
                    OrderDate = DateTime.Now,
                    UserId    = request.UserId
                };
                product.Quantity -= request.Quantity;
                _context.Orders.Add(order);
                _context.SaveChanges();
                var orderLine = new OrderLine
                {
                    Name      = product.Title,
                    Quantity  = request.Quantity,
                    Price     = product.Price,
                    Address   = request.Address,
                    OrderId   = order.Id,
                    ProductId = product.Id
                };

                _context.OrderLines.Add(orderLine);
                _context.SaveChanges();
                transaction.Commit();
            }
        }