Ejemplo n.º 1
0
        public async Task<IHttpActionResult> Validate(ClientOrderModel order)
        {
            if (order == null)
            {
                Logger.Current.LogWarning("Orders.Validate: null");

                return this.BadRequest("Empty order");
            }

            if (!this.ModelState.IsValid)
            {
                Logger.Current.LogWarning("Orders.Validate: invalid model state");

                return this.BadRequest(this.ModelState);
            }

            if (order.Items.Count() == 0)
            {
                Logger.Current.LogWarning("Orders.Validate: no items");

                return this.BadRequest("Empty order");
            }

            OrderItemsGrouper itemsGrouper = new OrderItemsGrouper();
            IList<OrderedItemModel> normalizedItems = itemsGrouper.NormalizeOrderedItems(order.Items.ToList());
            if (normalizedItems == null)
            {
                Logger.Current.LogWarning("Orders.Validate: grouping error");

                return this.BadRequest("Different prices of the same item");
            }

            OrderValidator orderValidator = new OrderValidator();
            bool isOrderValid = await orderValidator.ValidateOrderItems(normalizedItems);
            
            if (!isOrderValid)
            {
                Logger.Current.LogWarning("Orders.Validate: invalid quantities");

                return this.BadRequest("Invalid quantities");
            }

            return this.Ok();
        }
Ejemplo n.º 2
0
        public async Task<IHttpActionResult> ValidateAndSaveOrder(FullOrderModel order)
        {
            if (!this.ModelState.IsValid)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid model state");

                return this.BadRequest(this.ModelState);
            }

            if (order.Items.Count() == 0)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: no items");

                return this.BadRequest("Empty order");
            }

            OrderItemsGrouper itemsGrouper = new OrderItemsGrouper();
            IList<OrderedItemModel> normalizedItems = itemsGrouper.NormalizeOrderedItems(order.Items.ToList());
            if (normalizedItems == null)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: grouping error");

                return this.BadRequest("Different prices of the same item");
            }

            OrderValidator orderValidator = new OrderValidator();
            bool areOrderItemsValid = await orderValidator.ValidateOrderItems(normalizedItems);

            if (!areOrderItemsValid)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid quantities");

                return this.BadRequest("Invalid quantities");
            }

            bool freeShipping = order.Coupones != null && order.Coupones.Any(c => c.FreeShipping.Value);

            IModelTracker<DeliveryMethodModel> deliveryMethodModelTracker = new DeliveryMethodsTrackerFactory().GetDeliveryMethodsTracker(freeShipping);
            IModelTracker<PaymentMethodModel> paymentMethodModelTracker = new PaymentMethodsTrackerFactory().GetPaymentMethodsTracker(freeShipping);
            bool isLoyal = deliveryMethodModelTracker is FreeShippingDeliveryMethodsTracker;
            DeliveryMethodsProvider deliveryMethodsProvider = new DeliveryMethodsProvider(deliveryMethodModelTracker);
            PaymentMethodsProvider paymentMethodsProvider = new PaymentMethodsProvider(paymentMethodModelTracker);

            if (order.Coupones != null)
            {
                bool areCouponesValid = await orderValidator.ValidateCoupones(this.User.Identity.GetUserId(), order);
                if (!areCouponesValid)
                {
                    Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid coupones");

                    return this.BadRequest("Invalid coupones");
                }
            }

            bool areOrderPricesValid = await orderValidator.ValidatePrices(order, deliveryMethodsProvider, paymentMethodsProvider);
            if (!areOrderPricesValid)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid prive");

                return this.BadRequest("Invalid price");
            }

            StocksProvider stocksProvider = new StocksProvider();
            bool stockQuantitiesUpdatedSuccessfully = await stocksProvider.UpdateStocks(
                normalizedItems.Select(o => new StockChangeModel(o.ArticleId.Value, o.SizeId.Value, o.ColorId, -o.Quantity.Value)),
                Logger.Current);
            if (!stockQuantitiesUpdatedSuccessfully)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid quantities (update stocks)");

                return this.BadRequest("Invalid quantities");
            }

            
            Task<DeliveryMethodModel> getDeliveryMethodTask = deliveryMethodsProvider.GetDeliveryMethodById(order.DeliveryMethodId.Value);
            Task<PaymentMethodModel> getPaymentMethodTask = paymentMethodsProvider.GetPaymentMethodById(order.PaymentMethodId.Value);

            DeliveryMethodModel deliveryMethod = await getDeliveryMethodTask;
            PaymentMethodModel paymentMethod = await getPaymentMethodTask;

            Order newOrder = new Order();
            newOrder.UserId = new Guid(this.User.Identity.GetUserId());
            newOrder.DeliveryMerchant = deliveryMethod.Name;
            newOrder.DeliveryPrice = deliveryMethod.DeliveryPrice;
            newOrder.PaymentMethodId = paymentMethod.Id;
            newOrder.HasCommission = paymentMethod.ApplyDeliveryTax;
            newOrder.CommissionPercents = deliveryMethod.CODTax;
            newOrder.ShippingPrice = order.Total.Shipping.Value;
            newOrder.ItemsPrice = order.Total.Order.Value;
            newOrder.TotalPrice = order.Total.Full.Value;
            newOrder.StatusId = 1;
            newOrder.DateCreated = DateTime.UtcNow;

            List<OrderDetail> orderDetails = new List<OrderDetail>();
            foreach (OrderedItemModel orderedItem in normalizedItems)
            {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.ItemId = orderedItem.ArticleId.Value;
                orderDetail.SizeId = orderedItem.SizeId.Value;
                orderDetail.ColorId = orderedItem.ColorId;
                orderDetail.Quantity = orderedItem.Quantity.Value;
                orderDetail.ItemPrice = orderedItem.Price.Value;

                orderDetails.Add(orderDetail);
            }

            OrdersProvider ordersProvider = new OrdersProvider();
            int newOrderId = await ordersProvider.SaveOrder(newOrder, orderDetails);

            MailSendingFacade mailSender = new MailSendingFacade();
            mailSender.SendNewOrderMail(newOrderId);

            return this.Ok(newOrderId);
        }