Exemplo n.º 1
0
        public async Task <TResult> Query(TCommand command)
        {
            if (!command.IsFromCache)
            {
                return(await _decoratedRemoteQuery.Query(command));
            }

            var commandCacheKey = typeof(TCommand).FullName + command.ToJson();
            var cachedItem      = _cache.Get <TResult>(commandCacheKey, command.Region);

            if (cachedItem == null)
            {
                var item = await _decoratedRemoteQuery.Query(command);

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

                var cacheItem = new CacheItem <object>(commandCacheKey, command.Region, item, (ExpirationMode)command.ExpirationMode, command.Timeout);

                _cache.Add(cacheItem);

                return(item);
            }

            return(cachedItem);
        }
Exemplo n.º 2
0
        public override async Task <ActionResult <PagedList <OrderDto> > > Handle(ListOrdersCommand command)
        {
            var orders = await _listOrdersQuery.Query(command);

            var shippingMethodsDtoResult = await _listShippingMethodsQueryHandler.Handle(new ListShippingMethodsCommand());

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

            var orderStatusesDtoResult = await _listOrderStatusesQueryHandler.Handle(new ListOrderStatusesCommand());

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


            // TODO: fix workingLanguageId
            var ordersDto = _mapper.Map(orders, new PagedList <OrderDto>(), opt => opt.Items["WorkingLanguageId"] = 1);

            foreach (var orderDto in ordersDto.Items)
            {
                orderDto.OrderStatus    = orderStatusesDtoResult.Object.FirstOrDefault(o => o.Value.ToString() == orderDto.OrderStatus).Text;
                orderDto.ShippingMethod = shippingMethodsDtoResult.Object.FirstOrDefault(o => o.Value.ToString() == orderDto.ShippingMethod).Text;
            }

            return(Ok(ordersDto));
        }
        public override async Task <ActionResult <RouteDto> > Handle(GetRouteByComponentCommand command)
        {
            // TODO: pagalvoti ar sitas bera reikalingas.
            var routes = await _listRoutesQuery.Query(new ListRoutesCommand());

            return(Ok(null));
        }
Exemplo n.º 4
0
        public override async Task <ActionResult <RouteDto> > Handle(GetRouteByUrlCommand command)
        {
            command.Url = command.Url?.TrimStart('/') ?? "";

            var lastUrlChain = command.Url.Split('/').ToList().Last();
            var routes       = await _listRoutesQuery.Query(new ListRoutesCommand(lastUrlChain));

            foreach (var route in routes)
            {
                if (route.Product != null && route.Product.Id > 0)
                {
                    return(Ok(_mapper.Map(route, new RouteDto())));
                }
                else if (route.Category != null && route.Category.Id > 0)
                {
                    var category = await _getCategoryQuery.Query(new GetCategoryCommand(route.Category.Id, command.LanguageId));

                    if (category.GetCategoryChainedNavigation() == command.Url)
                    {
                        return(Ok(_mapper.Map(route, new RouteDto())));
                    }
                }
            }

            return(Ok(null));
        }
        public override async Task <ActionResult <List <ShippingMethodDto> > > Handle(ListShippingMethodsCommand command)
        {
            var shippingMethods = await _listShippingMethodsQuery.Query(command);

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

            var phrases = await _listPhrasesByNameQuery.Query(new ListPhrasesByNameCommand(shippingMethods.Select(o => o.Code)));

            var shippingMethodsDto = new List <ShippingMethodDto>();

            foreach (var shippingMethod in shippingMethods)
            {
                shippingMethodsDto.Add(new ShippingMethodDto()
                {
                    Price = shippingMethod.Price,
                    Text  = (phrases.FirstOrDefault(o => o.Code == shippingMethod.Code)?.Value ?? shippingMethod.Code) + $" + {shippingMethod.Price.ToCurrencyString()}€",
                    Value = shippingMethod.Code
                });
            }

            return(Ok(shippingMethodsDto));
        }
Exemplo n.º 6
0
        public override async Task <ActionResult <BasketDto> > Handle(CreateOrGetUnconfirmedBasketCommand command)
        {
            var basket = await _createOrGetUnconfirmedBasketQuery.Query(command);

            if (basket == null)
            {
                // if basket is null then send user is not authenticated error
                // TODO: take this info from phrases
                return(Error());
            }

            var basketDto = new BasketDto();

            basketDto.Id = basket.Id;

            if (basket.BasketItems.IsNullOrEmpty())
            {
                return(Ok(basketDto));
            }

            foreach (var basketItem in basket.BasketItems)
            {
                var basketItemDto = new BasketItemDto();
                var productDto    = (await _getProductQueryHandler.Handle(new GetProductCommand(basketItem.ProductId.Value, _workContext.WorkingLanguageId))).Object;
                basketItemDto.Id              = basketItem.Id;
                basketItemDto.Product         = productDto;
                basketItemDto.Price           = basketItem.Price;
                basketItemDto.ProductDetailId = basketItem.ProductDetailId;
                basketItemDto.Quantity        = basketItem.Quantity;

                basketDto.BasketItems.Add(basketItemDto);
            }

            return(Ok(basketDto));
        }
        public override async Task <ActionResult <ProductDto> > Handle(AddProductToBasketCommand command)
        {
            var productDtoResult = await _getProductQueryHandler.Handle(new GetProductCommand(command.ProductId, _workContext.WorkingLanguageId));

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

            var productDto   = productDtoResult.Object;
            var detailErrors = productDto.Fieldsets.MapToFields(command).ValidateFieldsets();

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

            var basket = await _addProductToBasketQuery.Query(command);

            if (basket == null)
            {
                // if basket is null then send user is not authenticated error
                // TODO: take this info from phrases
                return(Error(new Dictionary <string, DetailError>()
                {
                    { "baseProductBody" + "." + "basketAdd", new DetailError(DetailErrorTypes.Field, "Prašome pirmiau prisijungti.") }
                }));
            }

            return(Ok(productDto));
        }
Exemplo n.º 8
0
        public override async Task <Category> Query(DeleteCategoryCommand command)
        {
            var category = await DbContext.Categories.Include(o => o.Route).FirstOrDefaultAsync(o => o.Id == command.CategoryId);

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

            DbContext.ActionByEntityState(category.Route, EntityState.Deleted);
            await DbContext.SaveChangesAsync();

            var routes = await _listRoutesQuery.Query(new ListRoutesCommand());

            foreach (var route in routes.Where(o => o.Category == null && o.Product == null))
            {
                DbContext.ActionByEntityState(route, EntityState.Deleted);
            }
            await DbContext.SaveChangesAsync();

            _cache.ClearRegion(CacheRegions.Category);
            _cache.ClearRegion(CacheRegions.Product);

            return(category);
        }
Exemplo n.º 9
0
        public override async Task <ActionResult <SubmitCheckoutDto> > Handle(SubmitCheckoutCommand command)
        {
            var checkoutCategoryDtoResult = await _getCategoryByUrlQueryHandler.Handle(new GetCategoryByUrlCommand(command.RouteUrl));

            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 order = new Order();


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

            _dbContext.ActionByEntityState(basket, EntityState.Modified);
            _dbContext.ActionByEntityState(order, EntityState.Added);
            await _dbContext.SaveChangesAsync();

            return(Ok(new SubmitCheckoutDto()));
        }
Exemplo n.º 10
0
        public override async Task <ActionResult <PagedList <CategoryDto> > > Handle(ListCategoriesCommand command)
        {
            var pagedCategories = await _listCategoriesQuery.Query(command);

            var pagedCategoriesDto = _mapper.Map(pagedCategories, new PagedList <CategoryDto>(), opt => opt.Items["WorkingLanguageId"] = command.LanguageId);

            return(Ok(pagedCategoriesDto.Items.ToPagedList(command)));
        }
Exemplo n.º 11
0
        public override async Task <ActionResult <PagedList <ProductDto> > > Handle(ListProductsCommand command)
        {
            var products = await _listProductsQuery.Query(command);

            var productsDto = _mapper.Map(products, new PagedList <ProductDto>(), opt => opt.Items["WorkingLanguageId"] = command.LanguageId);

            return(Ok(productsDto));
        }
Exemplo n.º 12
0
        public async Task <ClassifierStore> Get()
        {
            var classifierStore = new ClassifierStore();

            classifierStore.AngularComponents = await _listAngularComponentsQuery.Query(new ListAngularComponentsCommand());

            classifierStore.CategoryTypes = await _listCategoryTypesQuery.Query(new ListCategoryTypesCommand());

            return(classifierStore);
        }
Exemplo n.º 13
0
        public override async Task <ActionResult <BasketItemDto> > Handle(DeleteBasketItemCommand command)
        {
            var basketItem = await _deleteBasketItemQuery.Query(command);

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

            return(Ok(new BasketItemDto()));
        }
Exemplo n.º 14
0
        public override async Task <ActionResult <CategoryDto> > Handle(DeleteCategoryCommand command)
        {
            var category = await _deleteCategoryQuery.Query(command);

            if (category == null)
            {
                // Nothing to delete
                return(Error());
            }

            return(Ok(null));
        }
Exemplo n.º 15
0
        public override async Task <ActionResult <ProductDto> > Handle(DeleteProductCommand command)
        {
            var product = await _deleteProductQuery.Query(command);

            if (product == null)
            {
                // Nothing to delete
                return(Error());
            }

            return(Ok(null));
        }
Exemplo n.º 16
0
        public override async Task <ActionResult <LanguageDto> > Handle(GetLanguageCommand command)
        {
            var language = await _getLanguageQuery.Query(command);

            if (language == null)
            {
                ActionResult <LanguageDto> .Error();
            }

            var languageDto = _mapper.Map <LanguageDto>(language);

            return(ActionResult <LanguageDto> .Ok(languageDto));
        }
Exemplo n.º 17
0
        public override async Task <ActionResult <OptionDto> > Handle(GetSystemSettingCommand command)
        {
            var systemSettingQuery = await _getSystemSettingQuery.Query(command);

            if (systemSettingQuery == null)
            {
                ActionResult <OptionDto> .Error();
            }

            var inputFormDto = _mapper.Map <OptionDto>(systemSettingQuery);

            return(ActionResult <OptionDto> .Ok(inputFormDto));
        }
Exemplo n.º 18
0
        public override async Task <ActionResult <List <PhraseDto> > > Handle(ListPhrasesByTermCommand command)
        {
            var phrases = await _listPhrasesQuery.Query(command);

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

            var phrasesDto = _mapper.Map(phrases, new List <PhraseDto>());

            return(Ok(phrasesDto));
        }
Exemplo n.º 19
0
        public override async Task <ActionResult <RouteDto> > Handle(GetRouteCommand command)
        {
            var route = await _getRouteQuery.Query(command);

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

            var routeDto = _mapper.Map(route, new RouteDto());

            return(Ok(routeDto));
        }
Exemplo n.º 20
0
        public override async Task <ActionResult <InputFormDto> > Handle(GetInputFormCommand command)
        {
            var inputFormQuery = await _getInputFormQuery.Query(command);

            if (inputFormQuery == null)
            {
                ActionResult <InputFormDto> .Error();
            }

            var inputFormDto = _mapper.Map <InputFormDto>(inputFormQuery);

            return(ActionResult <InputFormDto> .Ok(inputFormDto));
        }
Exemplo n.º 21
0
        public override async Task <ActionResult <CategoryDto> > Handle(GetCategoryCommand command)
        {
            var category = await _getCategoryQuery.Query(command);

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

            var categoryDto = _mapper.Map(category, new CategoryDto(), opt => opt.Items["WorkingLanguageId"] = command.LanguageId);

            return(Ok(categoryDto));
        }
Exemplo n.º 22
0
        public override async Task <InputForm> Query(GetInputFormCommand command)
        {
            var query = DbContext.InputForms
                        .Include(o => o.InputFormOptions);

            if (string.IsNullOrEmpty(command.InputFormName))
            {
                var activeInputForm = await _getSystemSettingQuery.Query(new GetSystemSettingCommand(SystemSettingOptions.ActiveInputForm.Name));

                command.InputFormName = activeInputForm.Value;
            }

            return(await query.Where(o => o.Code == command.InputFormName).FirstOrDefaultAsync());
        }
Exemplo n.º 23
0
        public override async Task <ActionResult <OrderDto> > Handle(GetOrderCommand command)
        {
            var order = await _getOrderQuery.Query(command);

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

            // TODO: fix WorkingLanguageId
            var orderDto = _mapper.Map(order, new OrderDto(), opt => opt.Items["WorkingLanguageId"] = 1);

            return(Ok(orderDto));
        }
Exemplo n.º 24
0
        public override async Task <ActionResult <ProductDto> > Handle(GetProductCommand command)
        {
            var product = await _getProductQuery.Query(command);

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

            var productDto = _mapper.Map(product, new ProductDto(), opt => opt.Items["WorkingLanguageId"] = command.LanguageId);

            productDto.Navigations.LastOrDefault().Parameters.Add("IsLastBreadcrumbUrlActive", true);

            return(Ok(productDto));
        }
Exemplo n.º 25
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));
        }
Exemplo n.º 26
0
        public override async Task <BasketItem> Query(DeleteBasketItemCommand command)
        {
            var basket = await _createOrGetUnconfirmedBasketQuery.Query(new CreateOrGetUnconfirmedBasketCommand());

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

            var basketItem = basket.BasketItems.FirstOrDefault(o => o.Id == command.BasketItemId);

            if (basketItem != null)
            {
                DbContext.ActionByEntityState(basketItem, EntityState.Deleted);
                await DbContext.SaveChangesAsync();
            }

            return(basketItem);
        }
Exemplo n.º 27
0
        public override async Task <ActionResult <CategoryDto> > Handle(SaveCategoryCommand command)
        {
            var saveCategoryDtoResult = await _getCategoryByUrlQueryHandler.Handle(new GetCategoryByUrlCommand(command.SaveCategory.RouteUrl, command.LanguageId));

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

            var saveCategoryDto = saveCategoryDtoResult.Object;
            var detailErrors    = saveCategoryDto.Fieldsets.MapToFields(command.SaveCategory.Category).ValidateFieldsets();

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

            var classifierStore = await _classifierStoresRepository.Get();

            var availableCharacteristics = await _listCharacteristicsQuery.Query(new ListCharacteristicsCommand());

            var category = await _getCategoryQuery.Query(new GetCategoryCommand(command.SaveCategory.Category.Id, command.LanguageId, true)
            {
                IsFromCache = false
            }) ?? new Category();

            var categoryDto = command.SaveCategory.Category;

            categoryDto = await CategoryDtoDefaults(categoryDto);

            _mapper.Map(categoryDto, category);

            category.Route.AngularComponentId = classifierStore.AngularComponents.First(o => o.Code == categoryDto.AngularComponentCode).Id;
            category.CategoryTypeId           = classifierStore.CategoryTypes.First(o => o.Code == categoryDto.CategoryTypeCode).Id;

            category.Images.MergeImages(availableCharacteristics, categoryDto.Images);
            category.Characteristics.MergeCharacteristics(availableCharacteristics, categoryDto.Characteristics);

            await _saveCategoryQuery.Query(new SaveCategoryQueryCommand(category));

            return(Ok(categoryDto));
        }
Exemplo n.º 28
0
        public override async Task <ActionResult <ProductDto> > Handle(SaveProductCommand command)
        {
            var saveProductDtoResult = await _getCategoryByUrlQueryHandler.Handle(new GetCategoryByUrlCommand(command.SaveProduct.RouteUrl, command.LanguageId));

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

            var saveProductDto = saveProductDtoResult.Object;
            var detailErrors   = saveProductDto.Fieldsets.MapToFields(command.SaveProduct.Product).ValidateFieldsets();

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

            var classifierStore = await _classifierStoresRepository.Get();

            var availableCharacteristics = await _listCharacteristicsQuery.Query(new ListCharacteristicsCommand());

            var product = await _getProductQuery.Query(new GetProductCommand(command.SaveProduct.Product.Id, command.LanguageId)
            {
                IsFromCache = false
            }) ?? new Product();

            var productDto = command.SaveProduct.Product;

            _mapper.Map(productDto, product);

            product.Route.AngularComponentId = classifierStore.AngularComponents.First(o => o.Code == productDto.AngularComponentCode).Id;

            product.Images.MergeImages(availableCharacteristics, productDto.Images);
            product.Characteristics.MergeCharacteristics(availableCharacteristics, productDto.Characteristics);

            product = await BeforeProductSave(product);

            await _saveProductQuery.Query(new SaveProductQueryCommand(product));

            return(Ok(productDto));
        }
Exemplo n.º 29
0
        public override async Task <Basket> Query(AddProductToBasketCommand command)
        {
            var product = await _getProductQuery.Query(new GetProductCommand(command.ProductId, _workContext.WorkingLanguageId));

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

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

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

            var basketItem = basket.BasketItems.FirstOrDefault(o => o.ProductId == command.ProductId && o.ProductDetailId == command.ProductDetailId);

            if (basketItem == null)
            {
                basketItem                 = new BasketItem();
                basketItem.BasketId        = basket.Id;
                basketItem.ProductDetailId = command.ProductDetailId;
                basketItem.ProductId       = product.Id;
                basketItem.Price           = product.GetTotalPrice();
                basketItem.Quantity        = command.Quantity;
            }
            else
            {
                basketItem.Quantity += command.Quantity;
            }


            DbContext.AddOrUpdate(basketItem);
            await DbContext.SaveChangesAsync();

            return(basket);
        }
Exemplo n.º 30
0
        public override async Task <ActionResult <List <PaymentMethodDto> > > Handle(ListPaymentMethodsCommand command)
        {
            var paymentMethods = await _listPaymentMethodsQuery.Query(command);

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

            var phrases = await _listPhrasesByNameQuery.Query(new ListPhrasesByNameCommand(paymentMethods.Select(o => o.Code).Concat(paymentMethods.SelectMany(o => o.PaymentMethodTypes.Select(i => i.Code)))));

            var paymentMethodsDto = new List <PaymentMethodDto>();

            foreach (var paymentMethod in paymentMethods)
            {
                var paymentMethodDto = new PaymentMethodDto()
                {
                    Text  = phrases.FirstOrDefault(o => o.Code == paymentMethod.Code)?.Value ?? paymentMethod.Code,
                    Value = paymentMethod.Code
                };

                foreach (var paymentMethodType in paymentMethod.PaymentMethodTypes)
                {
                    var paymentMethodTypeDto = new PaymentMethodTypeDto()
                    {
                        Text  = phrases.FirstOrDefault(o => o.Code == paymentMethodType.Code)?.Value ?? paymentMethodType.Code,
                        Value = paymentMethodType.Code
                    };

                    paymentMethodDto.PaymentMethodTypes.Add(paymentMethodTypeDto);
                }

                paymentMethodsDto.Add(paymentMethodDto);
            }

            return(Ok(paymentMethodsDto));
        }