Ejemplo n.º 1
0
 public OrderItem(MealItemOrdered mealItemOrdered, decimal price, int quantity, int grams, decimal calories, decimal proteins, decimal carbohydrates, decimal fats)
 {
     MealItemOrdered = mealItemOrdered;
     Price           = price;
     Quantity        = quantity;
     Grams           = grams;
     Calories        = calories;
     Proteins        = proteins;
     Carbohydrates   = carbohydrates;
     Fats            = fats;
 }
Ejemplo n.º 2
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address deliveryAddress)
        {
            //get the user from the repo
            var user = await _userManager.FindByEmailAsync(buyerEmail);

            // get basket from the repo
            var basket = await _basketRepo.GetBasketAsync(basketId);

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

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

                var mealOrdered = new MealItemOrdered(mealItem.Id, mealItem.Name, mealItem.PictureUrl);
                var orderItem   = new OrderItem(mealOrdered, mealItem.Price, item.Quantity, item.Grams, item.Calories, item.Proteins, item.Carbohydrates, item.Fats);
                items.Add(orderItem);
            }

            //update the user according to the items he ordered
            foreach (var item in items)
            {
                user.DailyCalories      = user.DailyCalories - (item.Quantity * item.Calories);
                user.DailyProteins      = user.DailyProteins - (item.Quantity * item.Proteins);
                user.DailyCarbohydrates = user.DailyCarbohydrates - (item.Quantity * item.Carbohydrates);
                user.DailyFats          = user.DailyFats - (item.Quantity * item.Fats);
            }

            _identityDbContext.Set <AppUser>().Attach(user);
            _identityDbContext.Entry(user).State = EntityState.Modified;



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

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

            //check to see if orders exists
            var spec          = new OrdersWithItemsAndOrderingSpecification(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(buyerEmail, subtotal, deliveryAddress, deliveryMethod, items, basket.PaymentIntentId);

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

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

            await _identityDbContext.SaveChangesAsync();

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