コード例 #1
0
        protected virtual async Task SaveStoreMappingsAsync(Topic topic, TopicModel model)
        {
            topic.LimitedToStores = model.SelectedStoreIds.Any();
            await _topicService.UpdateTopicAsync(topic);

            var existingStoreMappings = await _storeMappingService.GetStoreMappingsAsync(topic);

            var allStores = await _storeService.GetAllStoresAsync();

            foreach (var store in allStores)
            {
                if (model.SelectedStoreIds.Contains(store.Id))
                {
                    //new store
                    if (!existingStoreMappings.Any(sm => sm.StoreId == store.Id))
                    {
                        await _storeMappingService.InsertStoreMappingAsync(topic, store.Id);
                    }
                }
                else
                {
                    //remove store
                    var storeMappingToDelete = existingStoreMappings.FirstOrDefault(sm => sm.StoreId == store.Id);
                    if (storeMappingToDelete != null)
                    {
                        await _storeMappingService.DeleteStoreMappingAsync(storeMappingToDelete);
                    }
                }
            }
        }
コード例 #2
0
        /// <returns>A task that represents the asynchronous operation</returns>
        protected virtual async Task SaveStoreMappingsAsync(NewsItem newsItem, NewsItemModel model)
        {
            newsItem.LimitedToStores = model.SelectedStoreIds.Any();
            await _newsService.UpdateNewsAsync(newsItem);

            var existingStoreMappings = await _storeMappingService.GetStoreMappingsAsync(newsItem);

            var allStores = await _storeService.GetAllStoresAsync();

            foreach (var store in allStores)
            {
                if (model.SelectedStoreIds.Contains(store.Id))
                {
                    //new store
                    if (existingStoreMappings.Count(sm => sm.StoreId == store.Id) == 0)
                    {
                        await _storeMappingService.InsertStoreMappingAsync(newsItem, store.Id);
                    }
                }
                else
                {
                    //remove store
                    var storeMappingToDelete = existingStoreMappings.FirstOrDefault(sm => sm.StoreId == store.Id);
                    if (storeMappingToDelete != null)
                    {
                        await _storeMappingService.DeleteStoreMappingAsync(storeMappingToDelete);
                    }
                }
            }
        }
コード例 #3
0
        protected async Task UpdateStoreMappingsAsync <TEntity>(TEntity entity, List <int> passedStoreIds) where TEntity : BaseEntity, IStoreMappingSupported
        {
            if (passedStoreIds == null)
            {
                return;
            }

            entity.LimitedToStores = passedStoreIds.Any();

            var existingStoreMappings = await StoreMappingService.GetStoreMappingsAsync(entity);

            var allStores = await StoreService.GetAllStoresAsync();

            foreach (var store in allStores)
            {
                if (passedStoreIds.Contains(store.Id))
                {
                    //new store
                    if (existingStoreMappings.Count(sm => sm.StoreId == store.Id) == 0)
                    {
                        await StoreMappingService.InsertStoreMappingAsync(entity, store.Id);
                    }
                }
                else
                {
                    //remove store
                    var storeMappingToDelete = existingStoreMappings.FirstOrDefault(sm => sm.StoreId == store.Id);
                    if (storeMappingToDelete != null)
                    {
                        await StoreMappingService.DeleteStoreMappingAsync(storeMappingToDelete);
                    }
                }
            }
        }
コード例 #4
0
        private async Task SaveStoreMappingsAsync <TEntity, TModel>(TEntity entity, TModel model)
            where TEntity : BaseEntity, IStoreMappingSupported
            where TModel : BaseNopEntityModel, IStoreMappingSupportedModel
        {
            var existingStoreMappings = await _storeMappingService.GetStoreMappingsAsync(entity);

            var allStores = await _storeService.GetAllStoresAsync();

            foreach (var store in allStores)
            {
                if (model.SelectedStoreIds.Contains(store.Id))
                {
                    //new store
                    if (!existingStoreMappings.Any(sm => sm.StoreId == store.Id))
                    {
                        await _storeMappingService.InsertStoreMappingAsync(entity, store.Id);
                    }
                }
                else
                {
                    //remove store
                    var storeMappingToDelete = existingStoreMappings.FirstOrDefault(sm => sm.StoreId == store.Id);
                    if (storeMappingToDelete != null)
                    {
                        await _storeMappingService.DeleteStoreMappingAsync(storeMappingToDelete);
                    }
                }
            }
        }
コード例 #5
0
        /// <returns>A task that represents the asynchronous operation</returns>
        protected virtual async Task SaveStoreMappingsAsync(Poll poll, PollModel model)
        {
            poll.LimitedToStores = model.SelectedStoreIds.Any();
            await _pollService.UpdatePollAsync(poll);

            //manage store mappings
            var existingStoreMappings = await _storeMappingService.GetStoreMappingsAsync(poll);

            foreach (var store in await _storeService.GetAllStoresAsync())
            {
                var existingStoreMapping = existingStoreMappings.FirstOrDefault(storeMapping => storeMapping.StoreId == store.Id);

                //new store mapping
                if (model.SelectedStoreIds.Contains(store.Id))
                {
                    if (existingStoreMapping == null)
                    {
                        await _storeMappingService.InsertStoreMappingAsync(poll, store.Id);
                    }
                }
                //or remove existing one
                else if (existingStoreMapping != null)
                {
                    await _storeMappingService.DeleteStoreMappingAsync(existingStoreMapping);
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// Create a copy of product with all depended data
        /// </summary>
        /// <param name="product">The product to copy</param>
        /// <param name="newName">The name of product duplicate</param>
        /// <param name="isPublished">A value indicating whether the product duplicate should be published</param>
        /// <param name="copyImages">A value indicating whether the product images should be copied</param>
        /// <param name="copyAssociatedProducts">A value indicating whether the copy associated products</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the product copy
        /// </returns>
        public virtual async Task <Product> CopyProductAsync(Product product, string newName,
                                                             bool isPublished = true, bool copyImages = true, bool copyAssociatedProducts = true)
        {
            if (product == null)
            {
                throw new ArgumentNullException(nameof(product));
            }

            if (string.IsNullOrEmpty(newName))
            {
                throw new ArgumentException("Product name is required");
            }

            var productCopy = await CopyBaseProductDataAsync(product, newName, isPublished);

            //localization
            await CopyLocalizationDataAsync(product, productCopy);

            //copy product tags
            foreach (var productTag in await _productTagService.GetAllProductTagsByProductIdAsync(product.Id))
            {
                await _productTagService.InsertProductProductTagMappingAsync(new ProductProductTagMapping { ProductTagId = productTag.Id, ProductId = productCopy.Id });
            }

            await _productService.UpdateProductAsync(productCopy);

            //copy product pictures
            var originalNewPictureIdentifiers = await CopyProductPicturesAsync(product, newName, copyImages, productCopy);

            //quantity change history
            await _productService.AddStockQuantityHistoryEntryAsync(productCopy, product.StockQuantity, product.StockQuantity, product.WarehouseId,
                                                                    string.Format(await _localizationService.GetResourceAsync("Admin.StockQuantityHistory.Messages.CopyProduct"), product.Id));

            //product specifications
            await CopyProductSpecificationsAsync(product, productCopy);

            //product <-> warehouses mappings
            await CopyWarehousesMappingAsync(product, productCopy);

            //product <-> categories mappings
            await CopyCategoriesMappingAsync(product, productCopy);

            //product <-> manufacturers mappings
            await CopyManufacturersMappingAsync(product, productCopy);

            //product <-> related products mappings
            await CopyRelatedProductsMappingAsync(product, productCopy);

            //product <-> cross sells mappings
            await CopyCrossSellsMappingAsync(product, productCopy);

            //product <-> attributes mappings
            await CopyAttributesMappingAsync(product, productCopy, originalNewPictureIdentifiers);

            //product <-> discounts mapping
            await CopyDiscountsMappingAsync(product, productCopy);

            //store mapping
            var selectedStoreIds = await _storeMappingService.GetStoresIdsWithAccessAsync(product);

            foreach (var id in selectedStoreIds)
            {
                await _storeMappingService.InsertStoreMappingAsync(productCopy, id);
            }

            //tier prices
            await CopyTierPricesAsync(product, productCopy);

            //update "HasTierPrices" and "HasDiscountsApplied" properties
            await _productService.UpdateHasTierPricesPropertyAsync(productCopy);

            await _productService.UpdateHasDiscountsAppliedAsync(productCopy);

            //associated products
            await CopyAssociatedProductsAsync(product, isPublished, copyImages, copyAssociatedProducts, productCopy);

            return(productCopy);
        }
コード例 #7
0
        /// <summary>
        /// Create a copy of message template with all depended data
        /// </summary>
        /// <param name="messageTemplate">Message template</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the message template copy
        /// </returns>
        public virtual async Task <MessageTemplate> CopyMessageTemplateAsync(MessageTemplate messageTemplate)
        {
            if (messageTemplate == null)
            {
                throw new ArgumentNullException(nameof(messageTemplate));
            }

            var mtCopy = new MessageTemplate
            {
                Name = messageTemplate.Name,
                BccEmailAddresses = messageTemplate.BccEmailAddresses,
                Subject           = messageTemplate.Subject,
                Body               = messageTemplate.Body,
                IsActive           = messageTemplate.IsActive,
                AttachedDownloadId = messageTemplate.AttachedDownloadId,
                EmailAccountId     = messageTemplate.EmailAccountId,
                LimitedToStores    = messageTemplate.LimitedToStores,
                DelayBeforeSend    = messageTemplate.DelayBeforeSend,
                DelayPeriod        = messageTemplate.DelayPeriod
            };

            await InsertMessageTemplateAsync(mtCopy);

            var languages = await _languageService.GetAllLanguagesAsync(true);

            //localization
            foreach (var lang in languages)
            {
                var bccEmailAddresses = await _localizationService.GetLocalizedAsync(messageTemplate, x => x.BccEmailAddresses, lang.Id, false, false);

                if (!string.IsNullOrEmpty(bccEmailAddresses))
                {
                    await _localizedEntityService.SaveLocalizedValueAsync(mtCopy, x => x.BccEmailAddresses, bccEmailAddresses, lang.Id);
                }

                var subject = await _localizationService.GetLocalizedAsync(messageTemplate, x => x.Subject, lang.Id, false, false);

                if (!string.IsNullOrEmpty(subject))
                {
                    await _localizedEntityService.SaveLocalizedValueAsync(mtCopy, x => x.Subject, subject, lang.Id);
                }

                var body = await _localizationService.GetLocalizedAsync(messageTemplate, x => x.Body, lang.Id, false, false);

                if (!string.IsNullOrEmpty(body))
                {
                    await _localizedEntityService.SaveLocalizedValueAsync(mtCopy, x => x.Body, body, lang.Id);
                }

                var emailAccountId = await _localizationService.GetLocalizedAsync(messageTemplate, x => x.EmailAccountId, lang.Id, false, false);

                if (emailAccountId > 0)
                {
                    await _localizedEntityService.SaveLocalizedValueAsync(mtCopy, x => x.EmailAccountId, emailAccountId, lang.Id);
                }
            }

            //store mapping
            var selectedStoreIds = await _storeMappingService.GetStoresIdsWithAccessAsync(messageTemplate);

            foreach (var id in selectedStoreIds)
            {
                await _storeMappingService.InsertStoreMappingAsync(mtCopy, id);
            }

            return(mtCopy);
        }