public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { //Get Basket From the repo var basket = await _basketRepo.GetBasketAsync(basketId); //Get items from the product repo var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _productRepo.GetByIdAsync(item.Id); var itemOrderded = new ProductItemOrder(productItem.Id, productItem.Name, productItem.PictureUrl); var orderItem = new OrderItem(itemOrderded, productItem.Price, item.Quantity); items.Add(orderItem); } //Get delivery method from repo var deliveryMethod = await _dmRepo.GetByIdAsync(deliveryMethodId); //Calculate subtotal var subtotal = items.Sum(item => item.Price * item.Quantity); //Create Order var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal); //Return Order return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { // get basket from the repo var basket = await _basketRepo.GetBasketAsync(basketId); // get items from the product repo var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); var itemOrdered = new ProductItemOrder(productItem.id, productItem.Name, productItem.PictureUrl); var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } // get delivery method from repo var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); // calc subtotal var subtotal = items.Sum(item => item.Price * item.Quantity); //check to see if order exists var spec = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId); var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec); if (existingOrder != null) { _unitOfWork.Repository <Order>().Delete(existingOrder); await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId); } //create order var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal, basket.PaymentIntentId); _unitOfWork.Repository <Order>().Add(order); // save to db var result = await _unitOfWork.Complete(); if (result <= 0) { return(null); } return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { // get basket from the repo var basket = await _basketRespo.GetBasketAsync(basketId); // get items from the product repo var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); var itemOrdered = new ProductItemOrder(productItem.Id, productItem.Name, productItem.PictureUrl); var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } // get the deivery Method from repo var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); // calc subtotal var subtotal = items.Sum(item => item.Price * item.Quantity); // create order var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal); _unitOfWork.Repository <Order>().Add(order); //TODO: Save to db var result = await _unitOfWork.Complete(); if (result < 0) { return(null); } //delete basket await _basketRespo.DeleteBasketAsync(basketId); // return order return(order); }
public OrderItem(ProductItemOrder itemOrdered, decimal price, int quantity) { ItemOrdered = itemOrdered; Price = price; Quantity = quantity; }