Exemple #1
0
        public async Task SaveSeoInfosAsync(SeoInfo[] seoinfos)
        {
            var pkMap = new PrimaryKeyResolvingMap();

            using (var repository = _repositoryFactory())
            {
                var target = new List <SeoUrlKeywordEntity>(await repository.GetSeoByIdsAsync(seoinfos.Select(x => x.Id).ToArray()));
                var source = new List <SeoUrlKeywordEntity>(seoinfos.Select(x => AbstractTypeFactory <SeoUrlKeywordEntity> .TryCreateInstance().FromModel(x, pkMap)));

                source.CompareTo(target, EqualityComparer <SeoUrlKeywordEntity> .Default, (state, sourceEntity, targetEntity) =>
                {
                    if (state == EntryState.Added)
                    {
                        repository.Add(sourceEntity);
                    }
                    if (state == EntryState.Modified)
                    {
                        sourceEntity.Patch(targetEntity);
                    }
                });

                await repository.UnitOfWork.CommitAsync();

                pkMap.ResolvePrimaryKeys();
                SeoInfoCacheRegion.ExpireSeoInfos(source.Select(x => x.Id));
            }
        }
Exemple #2
0
        public async Task DeleteSeoForObjectAsync(ISeoSupport seoSupportObject)
        {
            if (seoSupportObject == null)
            {
                throw new ArgumentNullException(nameof(seoSupportObject));
            }

            if (seoSupportObject.Id != null)
            {
                var changedEntries = new List <GenericChangedEntry <SeoInfo> >();

                using (var repository = _repositoryFactory())
                {
                    var objectType     = seoSupportObject.SeoObjectType;
                    var objectId       = seoSupportObject.Id;
                    var seoUrlKeywords = await repository.GetObjectSeoUrlKeywordsAsync(objectType, objectId);

                    foreach (var seoUrlKeyword in seoUrlKeywords)
                    {
                        repository.Remove(seoUrlKeyword);
                    }
                    await repository.UnitOfWork.CommitAsync();

                    SeoInfoCacheRegion.ExpireSeoInfos(seoUrlKeywords.Select(x => x.Id));
                }
            }
        }
Exemple #3
0
        public async Task SaveSeoForObjectsAsync(ISeoSupport[] seoSupportObjects)
        {
            if (seoSupportObjects == null)
            {
                throw new ArgumentNullException(nameof(seoSupportObjects));
            }

            var changedEntries = new List <GenericChangedEntry <SeoInfo> >();
            var pkMap          = new PrimaryKeyResolvingMap();

            foreach (var seoObject in seoSupportObjects.Where(x => x.Id != null))
            {
                var objectType = seoObject.SeoObjectType;

                using (var repository = _repositoryFactory())
                {
                    if (seoObject.SeoInfos != null)
                    {
                        // Normalize seoInfo
                        foreach (var seoInfo in seoObject.SeoInfos)
                        {
                            if (seoInfo.ObjectId == null)
                            {
                                seoInfo.ObjectId = seoObject.Id;
                            }

                            if (seoInfo.ObjectType == null)
                            {
                                seoInfo.ObjectType = objectType;
                            }
                        }
                    }

                    if (seoObject.SeoInfos != null)
                    {
                        var target = new List <SeoUrlKeywordEntity>(await repository.GetObjectSeoUrlKeywordsAsync(objectType, seoObject.Id));
                        var source = new List <SeoUrlKeywordEntity>(seoObject.SeoInfos.Select(x => AbstractTypeFactory <SeoUrlKeywordEntity> .TryCreateInstance().FromModel(x, pkMap)));

                        var seoComparer = AnonymousComparer.Create((SeoUrlKeywordEntity x) => x.Id ?? string.Join(":", x.StoreId, x.ObjectId, x.ObjectType, x.Language));
                        source.CompareTo(target, seoComparer, (state, sourceEntity, targetEntity) =>
                        {
                            if (state == EntryState.Added)
                            {
                                repository.Add(sourceEntity);
                            }
                            if (state == EntryState.Deleted)
                            {
                                repository.Remove(sourceEntity);
                            }
                            if (state == EntryState.Modified)
                            {
                                sourceEntity.Patch(targetEntity);
                            }
                        });
                        await repository.UnitOfWork.CommitAsync();

                        pkMap.ResolvePrimaryKeys();
                        SeoInfoCacheRegion.ExpireSeoInfos(source.Concat(target).Distinct().Select(x => x.Id));
                    }
                }
            }
        }