Пример #1
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress)
        {
            // Get basket from repo.
            var basket = await _basketRepository.GetBasketAsync(basketId);

            // We can't trust the price in the basket, we'll have to take the price from the product.
            // Get items from product repo
            var items = new List <OrderItem>();

            foreach (var item in basket.Items)
            {
                // Get the prodcut from the repository
                var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id);

                // Create the snapshot of hte item at this give moment in time.
                var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl);

                // Create the order item, taking the price and quantity fro mthe proudct in the database not the basket.
                var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity);

                items.Add(orderItem);
            }

            // Get the delivery method from repo
            var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId);

            // Calculat subtotal
            var subtotal = items.Sum(item => item.Price * item.Quantity);

            // Check if a order exitsts with a payemtnIntnetId
            var spec = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId);

            var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

            // If an order exits then delete that order so we cna create a new one.
            if (existingOrder != null)
            {
                _unitOfWork.Repository <Order>().Delete(existingOrder);

                //Update the payemtn intent to make sure that the order is accurate
                await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId);
            }

            // Create order
            var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal, basket.PaymentIntentId);


            // Save the Order
            _unitOfWork.Repository <Order>().Add(order);

            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(null);
            }

            // Return the order
            return(order);
        }
Пример #2
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress)
        {
            // get basket from repo
            var basket = await _basketRepo.GetBasketAsync(basketId);

            // get items from produt repo
            var                items = new List <OrderItem>();
            Product            productItem;
            ProductItemOrdered itemOrdered;
            OrderItem          orderItem;

            foreach (var item in basket.Items)
            {
                productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id);

                itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl);
                orderItem   = new OrderItem(itemOrdered, productItem.Price, item.Quantity);

                items.Add(orderItem);
            }

            // get devilery 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 ordem
            var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subTotal, basket.PaymentIntentId);

            _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
            return(order);
        }
Пример #3
0
        public async Task <Order> CreatOrderAsync(string buyerEmail, int deliveryMethodId, string basketId,
                                                  Address shippingAddress)
        {
            // Get Basket

            var basket = await _basketRepo.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.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 deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId);

            // Calculate Subtotal

            var subtotal = items.Sum(x => x.Price * x.Quantity);

            //Check 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 the order

            var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal, basket.PaymentIntentId);

            // Save the order
            _unitOfWork.Repository <Order>().Add(order);
            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(null);
            }


            // Return the created order

            return(order);
        }
Пример #4
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress)
        {
            //get basket from repo
            var basket = await _basketRepo.GetBasketAsync(basketId);

            //get items from product repo
            var items = new List <OrderItem>();

            //looping through basket to get the details of items
            foreach (var item in basket.Items)
            {
                var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id);

                var itemOrder = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl);
                var orderItem = new OrderItem(itemOrder, productItem.Price, item.Quantity);
                items.Add(orderItem);
            }

            //get delivery method from repo
            var devliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId);

            // calculate subtotal
            var subtotal = items.Sum(item => item.Price * item.Quantity);

            #region 21.271.3 Checking if there is exist ing order -> PaymentService
            var spec          = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId);
            var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

            // if payment failed deleting the previous order and creating a new one
            if (existingOrder != null)
            {
                _unitOfWork.Repository <Order>().Delete(existingOrder);
                await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId);
            }
            #endregion

            //create order
            // 21.270.1 Add paymentIntentId ->OrderByPaymentIntentIdWithItemsSpecification
            var order = new Order(items, buyerEmail, shippingAddress, devliveryMethod, subtotal, basket.PaymentIntentId);

            #region 18.219
            _unitOfWork.Repository <Order>().Add(order);
            //save to db
            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(null);
            }

            //delete basket
            //await _basketRepo.DeleteBasketAsync(basketId);

            #endregion
            //return the order
            return(order);
        }
Пример #5
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress)
        {
            // 1. get basket from the repo
            var basket = await _basketRepo.GetBasketAsync(basketId);

            // 2. get the items from the repo
            // we trust what's in the basket as the quantity of items
            // we don't trust prices in the basket, we need to check them in database
            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 ignore whatever price was set inside basket
                var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity);
                items.Add(orderItem);
            }


            // 3. get delivery method
            var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId);

            // 4. calc subtotal
            var subtotal = items.Sum(item => item.Price * item.Quantity);

            // 4.1 check if order exists
            var spec          = new OrderByPaymentIntentIdSpecification(basket.PaimentIntentId);
            var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

            if (existingOrder != null)
            {
                _unitOfWork.Repository <Order>().Delete(existingOrder);
                // just to make sure that the order is accurate
                await _paymentService.CreateOrUpdatePaymentIntent(basket.PaimentIntentId);
            }

            // 5. create the order
            var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal,
                                  basket.PaimentIntentId);

            _unitOfWork.Repository <Order>().Add(order);

            // 6. save to db
            var result = await _unitOfWork.Complete();

            // 6.1 if saving is not successful return null
            if (result <= 0)
            {
                return(null);
            }

            // 7. return the order
            return(order);
        }
Пример #6
0
        public async Task <Order> UpdateOrderPaymentFailed(string paymentIntentId)
        {
            var spec  = new OrderByPaymentIntentIdSpecification(paymentIntentId);
            var order = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

            order.Status = OrderStatus.PaymentFailed;
            await _unitOfWork.Complete();

            return(order);
        }
Пример #7
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress)
        {
            // get basket from the repo
            // var basket = await _basketRepo.GetBasketAsync(basketId);
            var apiPath = _config.GetSection("APIPaths:BasketService").Value + "?id=" + basketId;
            var basket  = await InvokeBasketService.GetCustomerBasketAsync(apiPath);

            // get items from the product repo
            var items          = new List <OrderItem>();
            var apiPathProduct = _config.GetSection("APIPaths:ProductService").Value;  // + "/" + item.Id;

            foreach (var item in basket.Items)
            {
                //var productItem = await _unitOfWork.Repository<Product>().GetByIdAsync(item.Id);
                apiPathProduct = apiPathProduct + "/" + item.Id;
                var productItem = await InvokeProductService.GetProductAsync(apiPathProduct);

                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);

            // 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
            return(order);
        }
Пример #8
0
        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);

            // •	Calculate 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 nothing has been saved to the database
            if (result <= 0)
            {
                return(null);
            }

            // If save was successfull - then delete basket
            await _basketRepo.DeleteBasketAsync(basketId);

            // •	Return order
            return(order);
        }
Пример #9
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId,
                                                   Address shippingAddress)
        {
            // get basket from repo -> in order to get the actual price from db we dont trust the client
            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.Photos.FirstOrDefault(x => x.IsMain)?.PictureUrl);
                var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity);
                items.Add(orderItem);
            }

            // get delivery method
            var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId);

            // calculate subtotal
            var subtotal = items.Sum(item => item.Quantity * item.Price);

            // 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);
                //update payment intent
                await _paymentService.CreateOrUpdatePaymentIntent(basketId);
            }

            // create order
            var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal,
                                  basket.PaymentIntentId);

            _unitOfWork.Repository <Order>().Add(order);

            // TODO: Save to db
            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(null);
            }

            // return order
            return(order);
        }
Пример #10
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId,
                                                   Address shippingAddress)
        {
            // get basket from basket 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);

            // calculate subtotal
            var subtotal = items.Sum(item => item.Price * item.Quantity);

            // chec 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
            // any changes tracked by ef saved in db
            // if this fails then any changes taken place in method above are rollbacked
            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(null);
            }

            // return order
            return(order);
        }
Пример #11
0
        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);
        }
Пример #12
0
        public async Task <Order> UpdateOrderPaymentSucceeded(string paymentIntentId)
        {
            var spec  = new OrderByPaymentIntentIdSpecification(paymentIntentId);
            var order = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

            if (order == null)
            {
                return(null);
            }
            order.Status = OrderStatus.PaymentReceived;
            _unitOfWork.Repository <Order>().Update(order);
            return(order);
        }
Пример #13
0
        public async Task <Core.Entities.OrderAggregate.Order> UpdateOrderPaymentFailed(string paymentIntentId)
        {
            var spec  = new OrderByPaymentIntentIdSpecification(paymentIntentId);
            var order = await _unitOfWork.Repository <Core.Entities.OrderAggregate.Order>().GetEntityWithSpec(spec);

            if (order == null)
            {
                return(null);
            }
            order.Status = OrderStatus.PaymentFailed;
            await _unitOfWork.Complete();

            return(order);
        }
Пример #14
0
        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);
        }
Пример #15
0
        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);
        }
Пример #16
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string shoppingCartId, Address shippingAddress)
        {
            // get a shopping cart from the repo
            var cart = await _shoppingCartRepo.GetShoppingCartAsync(shoppingCartId);

            // get the items from the product repo
            var items = new List <OrderItem>();

            foreach (var item in cart.Items)
            {
                var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id);

                var itemOrderd = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl);
                var orderItem  = new OrderItem(itemOrderd, productItem.Price, item.Quantity);
                items.Add(orderItem);
            }

            // get delivery method from the 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(cart.PaymentIntentId);
            var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

            if (existingOrder != null)
            {
                _unitOfWork.Repository <Order>().Delete(existingOrder);
                await _paymentService.CreateOrUpdatePaymentIntent(cart.PaymentIntentId);
            }

            // create order
            var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subTotal, cart.PaymentIntentId);

            _unitOfWork.Repository <Order>().Add(order);

            // save to db
            var result = await _unitOfWork.CompleteAsync();

            if (result <= 0)
            {
                return(null);
            }

            // return order
            return(order);
        }
Пример #17
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress)
        {
            // get basket from repo
            var basket = await _basketRepository.GetBasketAsync(basketId);

            // get items from product repo
            var items = new List <OrderItem>();

            foreach (var item in basket.Items)
            {
                var product = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id);

                var itemOrdered = new ProductItemOrdered(product.Id, product.Name, product.PictureUrl);
                var orderItem   = new OrderItem(itemOrdered, product.Price, item.Quantity);
                items.Add(orderItem);
            }
            // get delivery method from repo
            var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId);

            // calculate subtotal
            var subtotal = items.Sum(i => i.Price * i.Quantity);

            // check if order existed
            var spec          = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId);
            var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

            if (existingOrder != null)
            {
                _unitOfWork.Repository <Order>().Delete(existingOrder);
            }

            // create order
            var order = new Order(buyerEmail, deliveryMethod, shippingAddress, items, subtotal, basket.PaymentIntentId);

            // save to db
            // TODO:
            _unitOfWork.Repository <Order>().Add(order);
            var result = await _unitOfWork.Complete();

            // nothing return from db
            if (result <= 0)
            {
                return(null);
            }

            // return order
            return(order);
        }
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippinhAddress)
        {
            //GET BASKET FROM REPO
            var basket = await _basketRepo.GetBasketAsync(basketId);

            //get item 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 total
            var subtotal = items.Sum(item => item.Price * item.Quantity);
            //check if order exist
            var spec          = new OrderByPaymentIntentIdSpecification(basket.PaymentIntent);
            var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

            if (existingOrder != null)
            {
                _unitOfWork.Repository <Order>().Delete(existingOrder);
                await _paymentService.createOrUpdatePaymentIntent(basket.PaymentIntent);
            }

            //create order
            var order = new Order(items, buyerEmail, shippinhAddress, deliveryMethod, subtotal, basket.PaymentIntent);

            //saving to db
            _unitOfWork.Repository <Order>().Add(order);
            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(null);
            }
            //delete basket
            // await _basketRepo.DeleteBaseketAsync(basketId);

            return(order);
        }
Пример #19
0
        public async Task <OrderAggregate> CreateOrderAsync(string buyerEmail, string basketId)
        {
            // get basket from the repo
            var basket = await _basketRepo.GetBasketAsync(basketId);

            // get items from the product repo
            var items = new List <MenuItem>();

            foreach (var item in basket.Items)
            {
                var productItem = await _unitOfWork.Repository <Menu>().GetByIdAsync(item.Id);

                var itemOrdered = new MenuItemOrdered(productItem.Id, productItem.FoodFirst, productItem.Day, productItem.Month, productItem.Year, item.SchoolName, item.DinnerTime);
                var orderItem   = new MenuItem(itemOrdered, productItem.Price);
                items.Add(orderItem);
            }


            // calc subtotal
            var subtotal = items.Sum(item => item.Price);

            // check to see if order exists
            var spec          = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId);
            var existingOrder = await _unitOfWork.Repository <OrderAggregate>().GetEntityWithSpec(spec);

            if (existingOrder != null)
            {
                _unitOfWork.Repository <OrderAggregate>().Delete(existingOrder);
                await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId);
            }

            // create order
            var order = new OrderAggregate(items, subtotal, buyerEmail, basket.PaymentIntentId);

            _unitOfWork.Repository <OrderAggregate>().Add(order);

            // save to db
            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(null);
            }

            // return order
            return(order);
        }
Пример #20
0
        public async Task <Order> UpdateOrderPaymentFailed(string paymentIntentId)
        {
            var spec  = new OrderByPaymentIntentIdSpecification(paymentIntentId);
            var order = await _unitOfWork.Repository <Order>().GetBySpecification(spec);

            if (order is null)
            {
                return(null);
            }

            order.Status = OrderStatus.PaymentFailed;

            _unitOfWork.Repository <Order>().Update(order);
            await _unitOfWork.Complete();

            return(order);
        }
Пример #21
0
        public async Task <Order> UpdateOrderPaymentStatus(string paymentIntentId, OrderStatus status)
        {
            var spec  = new OrderByPaymentIntentIdSpecification(paymentIntentId);
            var order = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

            if (order == null)
            {
                return(null);
            }

            order.status = status;
            _unitOfWork.Repository <Order>().Update(order);

            await _unitOfWork.Complete();

            return(order);
        }
Пример #22
0
        public async Task<Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress)
        {
            // get basket from the repo
            var basket = await this.basketRepo.GetBasketAsync(basketId);

            // get the items from the product repo
            var items = new List<OrderItem>();
            foreach (var item in basket.Items)
            {
                var productItem =await this.unitOfWork.productRepository().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);
            }

            // get the delivery method from repo
            var deliveryMethod = await this.unitOfWork.Repository<DeliveryMethod>().GetByIdAsync(deliveryMethodId);

            // calc subtotal
            var subtotal = items.Sum(item => item.Price * item.Quantity);

            // check if order exists
            var spec = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId);
            var existingOrder = await this.unitOfWork.Repository<Order>().GetEntityWithSpec(spec);

            if (existingOrder != null)
            {
                this.unitOfWork.Repository<Order>().Delete(existingOrder);
                await this.paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId);
            }


            // create order
            var order = new Order(
                items, buyerEmail, shippingAddress, deliveryMethod, subtotal, basket.PaymentIntentId);
            this.unitOfWork.Repository<Order>().Add(order);

            // save to db
            var result = await this.unitOfWork.Complete();

            if (result <= 0) return null;

            // return order
            return order;
        }
Пример #23
0
        /**
         * This method will create an order in the following way:
         *  - get deliver method from repo
         *  - calculate subtotal
         *  - create order
         *  - save to db
         *  - return order
         */
        public async Task <Order> CreatOrderAsync(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(items => items.Price * items.Quantity);
            var spec          = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId);
            var existingOrder = await _unitOfWork.Repository <Order>().GetEntitiyWithSpec(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);

            _unitOfWork.Repository <Order>().Add(order);

            var results = await _unitOfWork.Complete();

            if (results <= 0)
            {
                return(null);
            }

            return(order);
        }
Пример #24
0
        // 213-2
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress)
        {
            // get basket from the repo
            var basket = await _basketRepo.GetBasketAsync(basketId);

            // TODO: Handle non existing basket

            // get items from the product repo
            var items = new List <OrderItem>();

            foreach (var item in basket.Items)
            {
                // 219-2 update to unit of work as repository
                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);
            }

            // 219-3 update to unit of work as repository
            // get delivery method from repo
            var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId);

            // calc subtotal
            var subtotal = items.Sum(item => item.Price * item.Quantity);

            // 270-4 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);
            }

            // 270-2 add paymentIntentId parameter
            // create order
            var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal, basket.PaymentIntentId);

            // 219-4
            _unitOfWork.Repository <Order>().Add(order);

            // 219-5
            // save to db
            var result = await _unitOfWork.Complete();

            // 219-6
            if (result <= 0)
            {
                return(null);
            }

            // 219-7
            //delete basket
            // 270-6 remove Delete  the basket
            // await _basketRepo.DeleteBasketAsync(basketId);

            // return order
            return(order);

            // => use it at controller.
        }
Пример #25
0
        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);
        }
Пример #26
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId,
                                                   string shippingAddress)
        {
            // get basket from repo
            var basket = await _basketRepo.GetBasketAsync(basketId);

            // get items from the product and part repo
            var items = new List <OrderItem>();

            foreach (var item in basket.ProductItems)
            {
                var spec        = new ProductWithPhotosSpecification(item.Id);
                var productItem = await _unitOfWork.Repository <Product>().GetEntityWithSpecAsync(spec);

                var itemOrdered = new ItemOrdered(productItem.Id, productItem.Name,
                                                  productItem.Photos.FirstOrDefault(x => x.IsMain).Url);
                var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity);
                items.Add(orderItem);
            }
            foreach (var item in basket.PartItems)
            {
                var spec     = new PartsSpecification(item.Id);
                var partItem = await _unitOfWork.Repository <Part>().GetEntityWithSpecAsync(spec);

                var itemOrdered = new ItemOrdered(partItem.Id, partItem.Name,
                                                  partItem.Photos.FirstOrDefault(x => x.IsMain).Url);
                var orderItem = new OrderItem(itemOrdered, partItem.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 orderSpec      = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId);
            var exisitingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpecAsync(orderSpec);

            if (exisitingOrder != null)
            {
                _unitOfWork.Repository <Order>().Delete(exisitingOrder);
                await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId);
            }

            // create order
            var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal,
                                  basket.PaymentIntentId);

            _unitOfWork.Repository <Order>().Add(order);

            // TODO: save to db
            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(null);
            }



            // return order
            return(order);
        }
Пример #27
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId,
                                                   string basketId, Address shippingAddress)
        {
            // get basket form the basket repo
            var basket = await _basketRepo.GetBasketAsync(basketId);

            // get items from the product repo
            var items = new List <OrderItem>();

            foreach (var item in basket.Items)
            {
                // is part of IGenericRepository to which unitofwork has access
                var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id);

                var itemOrdered = new ProductItemOrderd(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 <DeleveryMethod>().GetByIdAsync(deliveryMethodId);

            // calculate subtotal
            var subtotal = items.Sum(item => item.Price * item.Quantity);

            // before order is created the order will be checked to see if we have already an existing order
            var spec          = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId);
            var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpec(spec);

            // if we have existing order we will delete that order
            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);


            // nothing saves in database at this line
            _unitOfWork.Repository <Order>().Add(order);

            // save to db

            var result = await _unitOfWork.Complete();

            /*
             *  if this fails then any changes that have taken place inside method here are going
             *  to be rolled back and will send error.
             *  Its the way to protect from partial ubdated on database
             */
            if (result <= 0)
            {
                return(null);
            }


            // return order
            return(order);
        }
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress, bool paymentMethod, DateTimeOffset shippingDate)
        {
            // get basket
            var basket = await _basketRepo.GetBasketAsync(basketId);

            // get items from meal repo
            var items = new List <OrderItem>();

            foreach (var item in basket.Items)
            {
                // Get meal - but just for Id since it won't grab photos
                var mealItem = await _unitOfWork.Repository <Meal>().GetByIdAsync(item.Id);

                // Create Spec cause we need to get Meals with Photos or Photo is null when viewed in Orders
                var mealspec = new MealsWithTypesAndMenusSpecification(mealItem.Id);
                // Get meal with Photos and other entitys
                var meal = await _unitOfWork.Repository <Meal>().GetEnitityWithSpec(mealspec);

                // Update Stock
                meal.Stock -= item.Quantity;
                _unitOfWork.Repository <Meal>().Update(meal);
                // Create Item Ordered
                var itemOrdered = new MealItemOrdered(meal.Id, meal.Name, meal.Photos.FirstOrDefault(x => x.IsMain)?.PictureUrl);
                // Add Ingrediants and Quantity
                var itemIngrediantsOrdered = new List <OrderItemIngrediant>();
                foreach (var ingrediant in item.Ingrediants)
                {
                    // If ingrediant Quantity is greater than default value then add to list
                    if (ingrediant.Quantity != meal.Ingrediants.FirstOrDefault(x => x.Id == ingrediant.Id).Quantity)
                    {
                        // Get ingrediant from repo
                        var repoIngrediant = meal.Ingrediants.FirstOrDefault(x => x.Id == ingrediant.Id);
                        // Get Quantity
                        var quantity        = ingrediant.Quantity;
                        var ingrediantToAdd = new OrderItemIngrediant(quantity, repoIngrediant.Price, repoIngrediant.Name);
                        itemIngrediantsOrdered.Add(ingrediantToAdd);
                    }
                }
                // Create orderItem
                var orderItem = new OrderItem(itemOrdered, meal.Price, item.Quantity, itemIngrediantsOrdered);
                // Add extra ingrediant price to total order Price
                foreach (var ingrediant in orderItem.Ingrediants)
                {
                    if (ingrediant.Quantity > 1)
                    {
                        orderItem.Price += ingrediant.Price * (ingrediant.Quantity);
                    }
                    else
                    {
                        orderItem.Price -= ingrediant.Price;
                    }
                }
                // Add to list
                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 for orders done by credit card
            if (paymentMethod)
            {
                var spec          = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId);
                var existingOrder = await _unitOfWork.Repository <Order>().GetEnitityWithSpec(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, paymentMethod, shippingDate);

            // edit status for payroll
            if (!paymentMethod)
            {
                order.Status = OrderStatus.PayRollPending;
            }

            _unitOfWork.Repository <Order>().Add(order);

            // save to db
            var result = await _unitOfWork.Complete();

            if (result < 0)
            {
                return(null);
            }

            // return order
            return(order);
        }