public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethod, string basketId, Address shippingAddress) { // get basket var basket = await _basketRepo.GetBasketAsync(basketId); // get products var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl); var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } // get delivery method var delivery = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethod); // calc subtotal decimal subtotal = items.Sum(item => item.Quantity * item.Price); // check if order with the same paymentIntentId exist var spec = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId); var orderToCheck = await _unitOfWork.Repository <Order>().GetBySpecification(spec); if (!(orderToCheck is null)) { _unitOfWork.Repository <Order>().Delete(orderToCheck); // update to make sure order is accurate await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId); } // create order var order = new Order(items, buyerEmail, shippingAddress, delivery, subtotal, basket.PaymentIntentId); _unitOfWork.Repository <Order>().Add(order); // save to db var result = await _unitOfWork.Complete(); if (result <= 0) { return(null); } // delete basket // await _basketRepo.DeleteBasketAsync(basketId); return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int delieveryMethodId, string basketId, Address shippingAddress) { // get basket from 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 ProductItemOrdered(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(delieveryMethodId); // calc subtotal var subtotal = items.Sum(item => item.Price * item.Quantity); // check to see if order exists var spec = new OrderByPaymentIntentWithItemsSpecification(basket.PaymentIntentId); var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec); // create order var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal, basket.PaymentIntentId); _unitOfWork.Repository <Order>().Add(order); if (existingOrder != null) { _unitOfWork.Repository <Order>().Delete(existingOrder); await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId); } // TODO: save to db var result = await _unitOfWork.Complete(); if (result <= 0) { return(null); } // return order return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { // get basket form repoBasket var basket = await _basketRepository.GetBasketAsynyc(basketId); // get items form the producRepo var orderItems = new List <OrderItem>(); foreach (var item in basket.Items) { var producItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); var itemOrder = new ProductItemOrdered(producItem.Id, producItem.Name, producItem.PictureUrl); var orderItem = new OrderItem(itemOrder, item.Price, item.Quantity); orderItems.Add(orderItem); } // get delivery method form repo var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); // check to see if the order exists var spec = new OrderByPaymentIdSpecification(basket.PaymentIntentId); var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec); if (existingOrder != null) { _unitOfWork.Repository <Order>().Delete(existingOrder); await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId); } // calc subtotal se calcula el el constructor // create order var order = new Order(orderItems, buyerEmail, shippingAddress, deliveryMethod, basket.PaymentIntenId); _unitOfWork.Repository <Order>().Add(order); // save to bd // await _orderRepository. var result = await _unitOfWork.Complete(); // si sale mal if (result <= 0) { return(null); } // si todo sale bien: // return the order return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string cartId, Address shippingAddress) { // get cart from repo var cart = await _cartRepo.GetCartAsync(cartId); // get items from the product repo var items = new List <OrderItem>(); foreach (var item in cart.CartItems) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.CartItemId); var itemOrdered = new ProductItemOrdered(productItem.ProductId, productItem.ProductName, productItem.PictureUrl); var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } // get delivery method from repo var deliveryMethode = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); // calculate subtotal var subtotal = items.Sum(item => item.Price * item.Quantity); // check to see if order exists var specification = new OrderByPaymentIdWithSpecification(cart.PaymentIntentId); var existingOrder = await _unitOfWork.Repository <Order>().GetWithSpecification(specification); if (existingOrder != null) { _unitOfWork.Repository <Order>().Delete(existingOrder); await _paymentService.CreateOrUpdatePaymentIntent(cart.PaymentIntentId); } // create order var order = new Order(items, buyerEmail, shippingAddress, deliveryMethode, subtotal, cart.PaymentIntentId); _unitOfWork.Repository <Order>().Add(order); // save to db var result = await _unitOfWork.Complete(); // if nothing saved to the db if (result <= 0) { return(null); } // return order return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { // получаем корзину из репо var basket = await _basketRepo.GetBasketAsync(basketId); // получаем товары из репозитория товаров var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); // Создать снимок вовремя var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.Photos.FirstOrDefault(x => x.IsMain)?.PictureUrl); var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } // получаем способ доставки из репо var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); // вычисляем промежуточный итог // Проверяем, есть ли существующий ордер 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); } var subtotal = items.Sum(items => items.Price * items.Quantity); // create order var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal, basket.PaymentIntentId); _unitOfWork.Repository <Order>().Add(order); // Сохраняем в db // Если что-то не удается, все изменения будут отменены, и будет отправлено сообщение об ошибке // Так что частичных обновлений больше нет var result = await _unitOfWork.Complete(); if (result <= 0) { return(null); } // return the 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 ProductItemOrdered(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); // create order var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal); _unitOfWork.Repository <Order>().Add(order); // save to db var result = await _unitOfWork.Complete(); if (result <= 0) { return(null); } // delete basket await _basketRepo.DeleteBasketAsync(basketId); // return order return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { // Create order steps: // 1) get basket from the repo var basket = await _basketRepo.GetBasketAsync(basketId); // 2) get items from the repo var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); var itemOrdered = new ProductItemOrdered(productItemId: productItem.Id, productName: item.ProductName, pictureUrl: item.PictureUrl); var orderItem = new OrderItem(itemOrdered: itemOrdered, price: productItem.Price, quantity: item.Quantity); items.Add(orderItem); } // 3) get delivery method from the repo var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); // 4) calc subtotal var subtotal = items.Sum(x => x.Price * x.Quantity); // 5) create order var order = new Order( orderItems: items, buyerEmail: buyerEmail, shipToAddress: shippingAddress, deliveryMethod: deliveryMethod, subtotal: subtotal); _unitOfWork.Repository <Order>().Add(order); //6) save to db var result = await _unitOfWork.Complete(); // 6.1) Return null if failst to save to the database if (result <= 0) { return(null); } // 7) delete basket await _basketRepo.DeleteBasketAsync(basketId); // 8) 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 _basketRepository.GetBasketAsync(basketId); // get items from product repo var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync((int)item.Id); var itemOrdered = new ProductItemOrdered((int)productItem.Id, productItem.Name, productItem.PictureUrl); var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); var subTotal = items.Sum(item => item.Price * item.Quantity); // check if order exists with payment intent id var spec = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId); var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec); //If we have existing order then delete it if (existingOrder != null) { _unitOfWork.Repository <Order>().Delete(existingOrder); await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId); } // create new order var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subTotal, basket.PaymentIntentId); _unitOfWork.Repository <Order>().Add(order); // saving to db // If this fails, entire changes taken place above will be rolled back. // Hence, no partial update. 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) { var basket = await _basketRepo.GetBasketAsync(basketId); var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl); var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); var subtotal = items.Sum(i => i.Price * i.Quantity); var spec = new OrderByPaymentIntentIdWithItemsSpecification(basket.PaymentIntentId); var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpecs(spec); if (existingOrder != null) { _unitOfWork.Repository <Order>().Delete(existingOrder); await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId); } var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal, basket.PaymentIntentId); var copyOfOrder = order; copyOfOrder.DeliveryMethod = null; _unitOfWork.Repository <Order>().Add(copyOfOrder); var result = await _unitOfWork.Complete(); if (result <= 0) { return(null); } order.DeliveryMethod = deliveryMethod; return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { // get basket from repo // get items from basket repo // get delivery method from repp // calc subtotal // create order // save to db // return order var basket = await _basketRepo.GetBasketAsync(basketId); var items = new List <OrderItem>(); foreach (var item in basket.Items) { // var productItem = await _productRepo.GetByIdAsync(item.Id); var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl); var OrderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(OrderItem); } // var deliveryMethod = await _dmRepo.GetByIdAsync(deliveryMethodId); var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); var subTotal = items.Sum(item => item.Price * item.Qunatity); var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subTotal); // TODO: Save to the database -- We will use UNIT OF WORK for handling multiple repositories; _unitOfWork.Repository <Order>().Add(order); var result = await _unitOfWork.Complete(); if (result <= 0) { return(null); } // delete basket await _basketRepo.DeleteBasketAsync(basketId); // return order; return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { // get basket from the repo/memurai database 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); //snapshot var itemOrdered = new ProductItemOrdered (productItem.Id, productItem.Name, productItem.PictureUrl); //we are getting price from our products data, ignoring the price in the basket 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); // create order var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal); _unitOfWork.Repository <Order>().Add(order); //save to db //either all of the changes to the database are going to be applied or none of them are var result = await _unitOfWork.Complete(); //this means nothing has been saved to the database if (result <= 0) { return(null); } //delete basket await _basketRepo.DeleteBasketAsync(basketId); // return order return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, string paymentMethod, double deliveryCost, string basketId, OrderAddress address) { var basket = await _basket.GetBasketAsync(basketId); var items = new List <OrderItem>(); var inValidProduct = false; foreach (var item in basket.Items) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); if (item.Quantity > productItem.Quantity) { inValidProduct = true; break; } if (!item.IsActive) { inValidProduct = true; break; } var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.Photos.FirstOrDefault(p => p.IsMain)?.PictureUrl); var orderItem = new OrderItem(itemOrdered, productItem.Price, productItem.NetPrice, item.Quantity); items.Add(orderItem); productItem.Quantity -= item.Quantity; _unitOfWork.Repository <Product>().Update(productItem); } if (!inValidProduct) { var subTotal = items.Sum(it => it.Price * it.Quantity); var order = new Order(items, buyerEmail, subTotal, deliveryCost, address, paymentMethod == "COD" ? PaymentMethod.COD : paymentMethod == "COD**" ? PaymentMethod.COD_ : PaymentMethod.BankTransfer, paymentMethod == "COD" ? OrderStatus.WaitingConfirmation : paymentMethod == "COD**" ? OrderStatus.WaitingConfirmation : OrderStatus.WaitingTransfer); _unitOfWork.Repository <Order>().Add(order); var result = await _unitOfWork.Complete(); if (result <= 0) { return(null); } return(order); } return(null); }
public async Task <Order> CreateOrderAsync(string userName, int deliveryMethodId, string basketId, Adress shippingAdress) { // get basket from the basket repo var basket = await _basketRepository.GetBasketAsync(basketId); // get delivery from delivery repo var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); // check the order is exist OrderByPaymentIntentIdWithSpecification spec = new OrderByPaymentIntentIdWithSpecification(basket.PaymentIntentId); var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpecAsync(spec); if (existingOrder != default(Order)) { _unitOfWork.Repository <Order>().Delete(existingOrder); _ = await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId); } // create order var order = new Order(userName, shippingAdress, deliveryMethod, basket.PaymentIntentId); // get product from product repo foreach (var basketItem in basket.Items) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(basketItem.Id); var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl); var x = new[] { 1, 2, 3, 4 }; order.AddOrderItem(itemOrdered, productItem.Price, basketItem.Quantity); } _unitOfWork.Repository <Order>().Add(order); var result = await _unitOfWork.SaveAsync(new System.Threading.CancellationToken()); if (result <= 0) { return(null); } // return order return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress, decimal dortadim) { // Basketeki ürünleri repodan çekiyoruz. var basket = await _basketRepo.GetBasketAsync(basketId); // basketteki itemleri product repodan çekiyoruz var items = new List <OrderItem>(); foreach (var item in basket.Items) { // Product itemleri buradan Ordered Item set etiriyoruz. var productItem = await __unitOfWork.Repository <Product>().GetByIdAsync(item.Id); var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl); var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } // gönderim tipini repon geliyor. var deliveryMethod = await __unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); // cupon codu repodan gleiyor // var couponCode = await _couponRepo.GetCopunCode(couponCode); // kampana tipini repoddan geliyoe // var couponCode = await _couponRepo.GetCopunCode(couponCode); // calc subtotal var subTotal = items.Sum(item => item.Price * item.Quantity); var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subTotal, dortadim); __unitOfWork.Repository <Order>().Add(order); // create order TODOü var result = await __unitOfWork.Complete(); if (result <= 0) { return(null); } //Delete Basket await _basketRepo.DeleteBasketAsync(basketId); // Return Order return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { var basket = await _basketRepo.GetBasketAsync(basketId); var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.Photos.FirstOrDefault(x => x.IsMain)?.PictureUrl); var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); var subtotal = items.Sum(item => item.Price * item.Quantity); //check to see if order exists var spec = new OrderByPaymentIntentIdWithItemsSpecification(basket.PaymentIntentId); var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec); if (existingOrder != null) { _unitOfWork.Repository <Order>().Delete(existingOrder); await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId); } var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal, basket.PaymentIntentId); //here add menas to track the entity _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 customerEmail, int deliveryMethodId, string cartId, ShippingAddress shippingAddress) { // Get cart var cart = await _cartService.GetCartAsync(cartId); // Get order items var orderItems = new List <OrderItem>(); foreach (var cartItem in cart.Items) { var productItem = await _unitOfWork.Service <Product>().GetByIdAsync(cartItem.Id); var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureURL); var orderItem = new OrderItem(itemOrdered, productItem.Price, cartItem.Quantity); orderItems.Add(orderItem); } // Get delivery method var deliveryMethod = await _unitOfWork.Service <DeliveryMethod>().GetByIdAsync(deliveryMethodId); // Calculate subttotal var subTotal = orderItems.Sum(orderitem => orderitem.Price * orderitem.Quantity); // Create order var order = new Order(customerEmail, subTotal, shippingAddress, deliveryMethod, orderItems); // Save to database _unitOfWork.Service <Order>().Add(order); var orderSaved = await _unitOfWork.SaveChangesToDatabase(); // Sanity check if (orderSaved <= 0) { return(null); } // Empty cart await _cartService.EmptyCartAsync(cartId); return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { // Get the basket from repo var basket = await _basketRepo.GetBasketAsync(basketId); // Get items from prod Repo var productItems = await _unitOfWork.Repository <Product>().ListBySpecAsync(new ProductsByIdsSpec(basket.Items.Select(i => i.Id))); var items = new List <OrderItem>(); foreach (var productItem in productItems) { var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl); // Snapshot the ordered Item var orderItem = new OrderItem(itemOrdered, productItem.Price, basket.Items.First(i => i.Id == productItem.Id).Quantity); items.Add(orderItem); } // Get delivery method from Repo var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); // Calc Subtotals var subtotals = items.Sum(item => item.Price * item.Quantity); // Create Order var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotals); _unitOfWork.Repository <Order>().Add(order); // TODO: Save to DB var result = await _unitOfWork.Complete(); if (result <= 0) { return(null); } // Delete basket await _basketRepo.DeleteBasketAsync(basketId); return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shipingAddress) { var basket = await _basketRepo.GetBasketAsync(basketId); var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _productRepo.GetById(item.Id); var itemOrder = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl); var orderItem = new OrderItem(itemOrder, productItem.Price, item.Quantity); items.Add(orderItem); } var deliveryMethod = await _deliveryRepo.GetById(deliveryMethodId); var subTotal = items.Sum(item => item.Price * item.Quantity); var order = new Order(items, buyerEmail, shipingAddress, deliveryMethod, subTotal); return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethod, string basketId, Address shippingAddress) { //get basket from 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 itemOrderd = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.u); items.Add(productItem); } //get delivery method from repo //calc subtotal //create order //save to db //return order }
public async Task <Order> CreateOrderAdminAsync(string buyerEmail, int deliveryMethodId, List <BasketItem> orderItems, Address shippingAddress) { var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId); var items = new List <OrderItem>(); foreach (var item in orderItems) { var specProduct = new ProductWithTypesAndBrandsSpecification(item.Id); var productItem = await _unitOfWork.Repository <Product>().GetEntityWithSpec(specProduct); var itemOrdered = new ProductItemOrdered(productItem.Id, item.Size, productItem.Name, productItem.Photos.FirstOrDefault(x => x.IsMain)?.PictureUrl); var productSize = productItem.ProductSizes.FirstOrDefault(x => x.Size == item.Size); var orderItem = new OrderItem(); if (item.Quantity > productSize.Quantity) { orderItem = new OrderItem(itemOrdered, productItem.Price, productSize.Quantity); productSize.Quantity = 0; } else { orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); productSize.Quantity = productSize.Quantity - item.Quantity; } productItem.AddOrUpdateProductSize(productSize.Size, productSize.Quantity); _unitOfWork.Repository <Product>().Update(productItem); items.Add(orderItem); } var subtotal = items.Sum(item => item.Price * item.Quantity); var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal); order.Status = OrderStatus.PaymentRecevied; _unitOfWork.Repository <Order>().Add(order); var result = await _unitOfWork.Complete(); //return order return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { var basket = await _basketRepo.GetBasketAsync(basketId); var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl); // We don't trust any Clients!!! Take price in productDb!.. var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>() .GetByIdAsync(deliveryMethodId); var subtotal = items.Sum(item => item.Price * item.Quantity); var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal); _unitOfWork.Repository <Order>().Add(order); var result = await _unitOfWork.Complete(); if (result <= 0) { return(null); } await _basketRepo.DeleteBasketAsync(basketId); return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodid, string basketId, Address shippingAddress) { //Get the basket from the repo var basket = await _basketRepository.GetBasketAsync(basketId); //get Items from product Repo var items = new List <OrderItem>(); foreach (var item in basket.Items) { var ProductItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.ProductId); var itemOrdered = new ProductItemOrdered(ProductItem.Id, ProductItem.Name, ProductItem.PictureUrl); var orderItem = new OrderItem(itemOrdered, item.Quantity, ProductItem.Price); items.Add(orderItem); } //get deliveryMethod from Repo var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodid); //Creating Subtotal var subTotal = items.Sum(item => item.Price * item.Quantity); //createOrder var order = new Order(buyerEmail, shippingAddress, deliveryMethod, items, subTotal); _unitOfWork.Repository <Order>().Add(order); //Save to DB var result = await _unitOfWork.Complete(); if (result < 1) { return(null); } //Delete Basket await _basketRepository.DeleteBasketAsync(basketId); //return order return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, string basketId) { // 1 get basket from the repo var basket = await _basketRepo.GetBasketAsync(basketId); // 2 get items from 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 ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl); // seperate product Price because we need to retrieve it from databas for validation var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } // 4 calculate total var total = items.Sum(item => item.Price * item.Quantity); // 5 create order var order = new Order(items, buyerEmail, total); // UnitOfWork Add new Order _unitOfWork.Repository <Order>().Add(order); // 6 TODO save to db UnitOfWork var result = await _unitOfWork.Complete(); if (result <= 0) { return(null); // and let the controller response Error } // delete basket as order is completed await _basketRepo.DeleteBasketAsync(basketId); // 7 return order return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { var basket = await _basketRepo.GetBasketAsync(basketId); var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _productRepo.GetProductByIdAsync(item.Id); var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl); var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } var deliveryMethod = await _deliveryRepo.GetDeliveryMethod(deliveryMethodId); var subTotal = items.Sum(item => item.Price * item.Quantity); var existOrder = await _orderRepo.GetOrderByPaymentIntentId(basket.PaymentIntentId); if (existOrder != null) { await _orderRepo.DeleteOrder(existOrder.Id); await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId, buyerEmail); } var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subTotal, basket.PaymentIntentId); await _orderRepo.CreateOrder(order); order = await _paymentService.UpdateOrderPaymentSucceeded(basket.PaymentIntentId); await _basketRepo.DeleteBasketAsync(basketId); return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { var basket = await _bskrepo.GetBasketAsync(basketId); var orderItems = new List <OrderItem> (); foreach (var item in basket.Items) { var product = await _uow.Repository <Product> ().GetByIdAsync(item.Id); var productItemOrdered = new ProductItemOrdered(product.Id, product.Name, product.PhotoUrl); orderItems.Add(new OrderItem(productItemOrdered, product.Price, item.Quantity)); } var spec = new OrderWithPaymentIntentIdSpec(basket.PaymentIntentId); var existingOrder = await _uow.Repository <Order> ().GetEntityBySpecAsync(spec); if (existingOrder != null) { _uow.Repository <Order> ().Delete(existingOrder); await _service.CreateOrUpdatePayementIntent(basket.PaymentIntentId); } var order = new Order(orderItems, buyerEmail, orderItems.Sum(x => x.Price * x.Quantity), shippingAddress, await _uow.Repository <DeliveryMethod> ().GetByIdAsync(deliveryMethodId), basket.PaymentIntentId); _uow.Repository <Order> ().Add(order); var result = await _uow.Complete(); if (result <= 0) { return(null); } //await _bskrepo.DeleteBasketAsync (basketId); return(order); }
public OrderItem(ProductItemOrdered itemOrdered, decimal price, int quantity) { ItemOrdered = itemOrdered; Price = price; Quantity = quantity; }
public OrderItem(ProductItemOrdered itemOrdered, decimal price, int quantity) { this.ItemOrdered = itemOrdered; this.Price = price; this.Quantity = quantity; }
public OrderItem(ProductItemOrdered ItemOrdered, decimal price, int quantity) { this.ItemOrdered = ItemOrdered; Price = price; Quantity = quantity; }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { //get basket from the repo var basket = await _basketRepo.GetBasketAsync(basketId); //when the delivery method is not set then set it to free shipping if (deliveryMethodId == 0) { deliveryMethodId = 4; } //get the items from the product repo var items = new List <OrderItem>(); foreach (var item in basket.Items) { //Replacing generic repositories with UOW. Course item 219 var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl); //Get the quantity from the client but get the price from the product in the database var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } //get the delivery method 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. Course item 270 var spec = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId); var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec); if (existingOrder != null) { _unitOfWork.Repository <Order>().Delete(existingOrder); // Update the payment intent before creating a replacement order. Good practice when // adjusting order or even when changing items in basket await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId); } //create order var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal, basket.PaymentIntentId); // Create a log event with an event Id _logger.LogInformation(LogEvents.OrderCreation, "SERVICE ENTRY: Creating a new order: {Order}", order); _unitOfWork.Repository <Order>().Add(order); //save to db. Course item 219 var result = await _unitOfWork.Complete(); //Save was not successful. UOW takes care of rolling back the transaction if (result <= 0) { return(null); } //return order return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int delvieryMethod, string basketId, OrderAddress shippingAdress) { Order toAdd = new Order { BuyerEmail = buyerEmail, ShipToAddress = shippingAdress, DeliveryMethod = _unitOfWork._deliveryMethods.GetById(delvieryMethod) }; var basket = _basketRepo.GetBasketAsync(basketId).Result; var bCustomOrders = _basketRepo.GetBasketAsync(basketId).Result.CustomOrderItems; decimal price = 0; List <OrderItem> oItems = new List <OrderItem>(); foreach (var item in basket.BasketItems) { var prodItemOrdered = new ProductItemOrdered(); prodItemOrdered.PictureUrl = item.PictureUrl; prodItemOrdered.ProductItemId = item.Id; prodItemOrdered.ProductName = item.ProductName; var orderItem = new OrderItem(); orderItem.ItemOrdered = prodItemOrdered; orderItem.Price = _unitOfWork._products.GetById(item.Id).Price; orderItem.Quantity = item.Quantity; price += orderItem.Price * orderItem.Quantity; oItems.Add(orderItem); } List <CustomOrderItem> bcustomItems = new List <CustomOrderItem>(); foreach (var item in basket.CustomOrderItems) { var volume = await _modelVolumeRepository.getModelVolume(item.ModelName); var modelPrice = await _priceCalculatorService.calculatePrice(volume, item.FilamentId, (double)item.FilingPercent / 100); Console.WriteLine("CENA MODELU PRZED ZAOKRAGLENIEM"); Console.WriteLine(modelPrice); modelPrice = Math.Round(modelPrice, 2); Console.WriteLine("CENA MODELU PO:"); Console.WriteLine(modelPrice); CustomOrderItem newCustomItem = new CustomOrderItem { UsersId = item.UsersId, ModelName = item.ModelName, Quantity = item.Quantity, FilamentId = item.FilamentId, FilingPercent = item.FilingPercent, ColorId = item.ColorId, Price = Convert.ToDecimal(modelPrice) }; bcustomItems.Add(newCustomItem); price += Convert.ToDecimal(modelPrice * item.Quantity); } toAdd.CustomOrderItems = bcustomItems; toAdd.OrderItems = oItems; toAdd.Subtotal = price; _unitOfWork._orders.Add(toAdd); var result = await _unitOfWork.Complete(); if (result < 1) { return(null); } return(toAdd); }