示例#1
0
        public override async Task <ActionResult <OrderDto> > Handle(SaveOrderCommand command)
        {
            var editOrderCategoryDtoResult = await _getCategoryByUrlQueryHandler.Handle(new GetCategoryByUrlCommand(command.RouteUrl, _workContext.WorkingLanguageId));

            if (editOrderCategoryDtoResult.Failure)
            {
                return(Error(editOrderCategoryDtoResult.ErrorMessages));
            }

            var editOrderCategoryDto = editOrderCategoryDtoResult.Object;
            var orderDto             = command.Order;

            var detailErrors = editOrderCategoryDto.Fieldsets.MapToFields(orderDto).ValidateFieldsets();

            if (!detailErrors.IsNullOrEmpty())
            {
                return(Error(detailErrors));
            }

            var order = await _getOrderQuery.Query(new GetOrderCommand(command.Order.Id));

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

            _mapper.Map(command.Order, order);

            var paymentMethods = await _listPaymentMethodsQuery.Query(new ListPaymentMethodsCommand());

            var shippingMethods = await _listShippingMethodsQuery.Query(new ListShippingMethodsCommand());

            var orderStatuses = await _listOrderStatusesQuery.Query(new ListOrderStatusesCommand());

            order.PaymentMethodId     = paymentMethods.FirstOrDefault(o => o.Code == orderDto.PaymentMethod).Id;
            order.PaymentMethodTypeId = paymentMethods.FirstOrDefault(o => o.Code == orderDto.PaymentMethod).PaymentMethodTypes.FirstOrDefault(o => o.Code == orderDto.PaymentMethodType)?.Id;
            order.ShippingMethodId    = shippingMethods.FirstOrDefault(o => o.Code == orderDto.ShippingMethod).Id;
            order.OrderStatusId       = orderStatuses.FirstOrDefault(o => o.Code == orderDto.OrderStatus).Id;

            _dbContext.AddOrUpdate(order);
            await _dbContext.SaveChangesAsync();

            return(Ok(orderDto));
        }
        public override async Task <ActionResult <OrderDto> > Handle(SubmitOrderCommand command)
        {
            var checkoutCategoryDtoResult = await _getCategoryByUrlQueryHandler.Handle(new GetCategoryByUrlCommand(command.RouteUrl, _workContext.WorkingLanguageId));

            if (checkoutCategoryDtoResult.Failure)
            {
                return(Error(checkoutCategoryDtoResult.ErrorMessages));
            }

            var checkoutCategoryDto = checkoutCategoryDtoResult.Object;
            var orderDto            = command.Order;

            if (orderDto.ShippingMethod == ShippingMethods.Shop)
            {
                checkoutCategoryDto.GetAnyField(nameof(orderDto.Address)).SetCharacteristic(CharacteristicCodes.IsRequired, false);
                checkoutCategoryDto.GetAnyField(nameof(orderDto.City)).SetCharacteristic(CharacteristicCodes.IsRequired, false);
                checkoutCategoryDto.GetAnyField(nameof(orderDto.PostCode)).SetCharacteristic(CharacteristicCodes.IsRequired, false);
            }

            if (orderDto.PaymentMethod == PaymentMethods.Cash)
            {
                checkoutCategoryDto.GetAnyField(nameof(orderDto.PaymentMethodType)).SetCharacteristic(CharacteristicCodes.IsRequired, false);
            }

            var detailErrors = checkoutCategoryDto.Fieldsets.MapToFields(orderDto).ValidateFieldsets();

            if (!detailErrors.IsNullOrEmpty())
            {
                return(Error(detailErrors));
            }

            var basket = await _createOrGetUnconfirmedBasketQuery.Query(new CreateOrGetUnconfirmedBasketCommand());

            if (basket.BasketItems.Count <= 0)
            {
                return(Error());
            }

            basket.IsConfirmed = true;
            var paymentMethods = await _listPaymentMethodsQuery.Query(new ListPaymentMethodsCommand());

            var shippingMethods = await _listShippingMethodsQuery.Query(new ListShippingMethodsCommand());

            var orderStatuses = await _listOrderStatusesQuery.Query(new ListOrderStatusesCommand());

            var order = new Order();

            _mapper.Map(command.Order, order);
            order.UserId              = basket.UserId;
            order.BasketId            = basket.Id;
            order.PaymentMethodId     = paymentMethods.FirstOrDefault(o => o.Code == orderDto.PaymentMethod).Id;
            order.PaymentMethodTypeId = paymentMethods.FirstOrDefault(o => o.Code == orderDto.PaymentMethod).PaymentMethodTypes.FirstOrDefault(o => o.Code == orderDto.PaymentMethodType)?.Id;
            order.ShippingMethodId    = shippingMethods.FirstOrDefault(o => o.Code == orderDto.ShippingMethod).Id;
            order.OrderStatusId       = orderStatuses.FirstOrDefault(o => o.Code == OrderStatuses.Unconfirmed).Id;

            _dbContext.AddOrUpdate(basket);
            _dbContext.AddOrUpdate(order);
            await _dbContext.SaveChangesAsync();

            return(Ok(orderDto));
        }