public void Execute(CreateOrderDto dto) { _validator.ValidateAndThrow(dto); var order = new Order { UserId = dto.Id, Address = dto.Address, OrderDate = dto.OrderDate }; foreach (var item in dto.Items) { var product = _context.Products.Find(item.ProductId); product.Quantity -= item.Quantity; order.OrderLines.Add(new OrderLine { ProductId = item.ProductId, Quantity = item.Quantity, Name = product.Name, Price = product.Price }); } _context.Orders.Add(order); _context.SaveChanges(); }
public void Execute(CreateOrderDto request) { _validator.ValidateAndThrow(request); int IdUser = actor.Id; var order = new Order { UserId = IdUser, Address = request.Address, Date = request.OrderDate }; foreach (var item in request.Items) { var product = _context.Products.Find(item.ProductId); product.Quantity -= item.Quantity; order.OrderInfo.Add(new OrderInfo { Name = product.Name, Quantity = item.Quantity, Price = product.Price, ProductId = item.ProductId, });; } _context.Orders.Add(order); _context.SaveChanges(); }
public void Execute(CreateOrderDto request) { _validator.ValidateAndThrow(request); var order = new Order { Address = request.Address, UserId = request.UserId, OrderDate = request.OrderDate }; foreach (var item in request.Items) { var book = _context.Books.Find(item.BookId); book.Quantity -= item.Quantity; order.OrderLines.Add(new OrderLine { BookId = item.BookId, Quantity = item.Quantity, Name = book.Title, Price = book.Price, OrderId = order.Id }); } _context.Orders.Add(order); _context.SaveChanges(); }
public void Execute(CreateOrderDto request) { _validator.ValidateAndThrow(request); var order = new Order { Address = request.Address, UserId = _actor.Id, OrderDate = request.OrderDate }; foreach (var item in request.Items) { var movie = _context.Movies.Find(item.MovieId); movie.Quantity -= item.Quantity; order.OrderLines.Add(new OrderLine { MovieId = item.MovieId, Quantity = item.Quantity, Title = movie.Title, Price = movie.Price, OrderId = order.Id }); } _context.Orders.Add(order); _context.SaveChanges(); }
public void Execute(OrderDto request) { _validator.ValidateAndThrow(request); var userId = _actor.Id; using (var dbContextTransaction = _context.Database.BeginTransaction()) { var order = new Order { Address = request.Address, UserId = userId, OrderStatus = OrderStatus.OnHold, PaymentOption = request.PaymentOption }; _context.Orders.Add(order); _context.SaveChanges(); foreach (var line in request.OrderLines) { var product = _context.Products.Include(p => p.Prices).Where(p => p.Id == line.ProductId).FirstOrDefault(); var orderLine = new OrderLine { Name = product.Name, Price = product.Prices.OrderByDescending(date => date.CreatedAt).FirstOrDefault().Price, OrderId = order.Id, ProductId = product.Id, Quantity = line.Quantity }; product.Stock -= line.Quantity; _context.OrderLines.Add(orderLine); } _context.SaveChanges(); dbContextTransaction.Commit(); } }