Example #1
0
        public async Task <OrderCreateViewModel> CreateAsync(OrderCreateInputModel orderFromView, string username, ModelStateDictionary modelState)
        {
            if (!modelState.IsValid)
            {
                var errorModel = await this.customersService.GetByIdAsync <OrderCreateViewModel>(orderFromView.CustomerId);

                errorModel.IsExpress             = orderFromView.IsExpress;
                errorModel.HasFlavor             = orderFromView.HasFlavor;
                errorModel.ItemQuantitySetByUser = orderFromView.ItemQuantitySetByUser;
                return(errorModel);
            }

            var hasAnyCustomerWithId = await this.customersService.IsCustomerExistAsync(orderFromView.CustomerId);

            // If customer with Id NOT exists return existing view model
            if (!hasAnyCustomerWithId)
            {
                throw new ArgumentNullException(nameof(orderFromView.CustomerId), string.Format(OrderConstants.NullReferenceCustomerId, orderFromView.CustomerId));
            }

            var orderToDb = orderFromView.To <Order>();

            orderToDb.StatusId = await this.orderStatusService.GetIdByNameAsync(OrderConstants.StatusPickUpArrangeDayWaiting);

            orderToDb.CreatorId = await this.employeesService.GetIdByUserNameAsync(username);

            await this.orderRepository.AddAsync(orderToDb);

            await this.orderRepository.SaveChangesAsync();

            return(orderToDb.To <OrderCreateViewModel>());
        }
Example #2
0
        public async Task CreateUproccessedOrder(OrderCreateInputModel orderCreateInputModel, string shoppingCartId)
        {
            var shoppingCartProducts = await this.shoppingCartProductsService.GetAllProductsAsync <ShoppingCartProductInputModel>(shoppingCartId);

            if (shoppingCartProducts == null || shoppingCartProducts.Count() == 0)
            {
                throw new ArgumentNullException("ShoppingCartId was null or empty");
            }


            decimal totalPrice    = 0m;
            decimal deliveryPrice = 0m;

            var order = orderCreateInputModel.To <Order>();

            await this.orderRepository.AddAsync(order);


            foreach (var product in shoppingCartProducts)
            {
                order.OrderProducts.Add(new OrderProduct()
                {
                    OrderId   = order.Id,
                    ProductId = product.ProductId,
                    Quantity  = product.Quantity
                });
            }

            totalPrice = shoppingCartProducts.Sum(x => x.Quantity * x.ProductPrice);

            if (orderCreateInputModel.PromoCodeId != null)
            {
                totalPrice = await this.promoCodeService.DeductPercentageFromPrice(totalPrice, orderCreateInputModel.PromoCodeId);
            }

            deliveryPrice = totalPrice >= 20m ? 0m : 3.5m;

            order.OrderStatus   = OrderStatus.UnProccessed;
            order.DeliveryPrice = deliveryPrice;
            order.TotalPrice    = totalPrice + deliveryPrice;
            order.OrderDate     = DateTime.UtcNow;

            await this.shoppingCartProductsService.DeleteAll(shoppingCartId);

            await this.orderRepository.SaveChangesAsync();
        }