Пример #1
0
        public void MergeWith(UrlRecordCollection other)
        {
            Guard.NotNull(other, nameof(other));

            if (!this._entityName.EqualsNoCase(other._entityName))
            {
                throw new InvalidOperationException("Expected group '{0}', but was '{1}'".FormatInvariant(this._entityName, other._entityName));
            }

            // Merge dictionary
            other._dict.Merge(this._dict, true);

            // Merge requested set (entity ids)
            if (this._requestedSet != null)
            {
                if (other._requestedSet == null)
                {
                    other._requestedSet = new HashSet <int>(this._requestedSet);
                }
                else
                {
                    other._requestedSet.AddRange(this._requestedSet);
                }
            }
        }
Пример #2
0
        protected internal virtual async Task <UrlRecord> ApplySlugAsync(ValidateSlugResult result, UrlRecordCollection prefetchedCollection, bool save = false)
        {
            if (!result.WasValidated)
            {
                throw new ArgumentException("Unvalidated slugs cannot be applied. Consider obtaining 'ValidateSlugResult' from 'ValidateSlugAsync()' method.", nameof(result));
            }

            if (string.IsNullOrWhiteSpace(result.Slug))
            {
                return(null);
            }

            var dirty      = false;
            var entry      = result.Found;
            var languageId = result.LanguageId ?? 0;

            if (entry != null && result.FoundIsSelf)
            {
                // Found record refers to requested entity
                if (entry.IsActive)
                {
                    // ...and is active. Do nothing, 'cause nothing changed.
                }
                else
                {
                    // ...and is inactive. Make it active
                    entry.IsActive = true;
                    dirty          = true;

                    // ...and make the current active one(s) inactive.
                    var currentActive = await GetActiveEntryFromStoreAsync();

                    if (currentActive != null)
                    {
                        currentActive.IsActive = false;
                    }
                }
            }

            if (entry == null || !result.FoundIsSelf)
            {
                // Create new entry because no entry was found or found one refers to another entity.
                // Because unvalidated slugs cannot be passed to this method we assume slug uniqueness.
                entry = new UrlRecord
                {
                    EntityId   = result.Source.Id,
                    EntityName = result.EntityName,
                    Slug       = result.Slug,
                    LanguageId = languageId,
                    IsActive   = true,
                };
            }

            if (entry != null && entry.IsTransientRecord())
            {
                // It's a freshly created record, add to set.
                _db.UrlRecords.Add(entry);

                // When we gonna save deferred, adding the new entry to our extra lookup
                // will ensure that subsequent validation does not miss new records.
                _extraSlugLookup[entry.Slug] = entry;

                dirty = true;
            }

            if (dirty && save)
            {
                await _db.SaveChangesAsync();
            }

            return(entry);

            async Task <UrlRecord> GetActiveEntryFromStoreAsync()
            {
                if (result.Source.Id > 0)
                {
                    if (prefetchedCollection != null)
                    {
                        var record = prefetchedCollection.Find(languageId, result.Source.Id);
                        if (record != null)
                        {
                            // Transient: was requested from store, but does not exist.
                            return(record.IsTransientRecord() ? null : record);
                        }
                    }

                    return(await _db.UrlRecords
                           .ApplyEntityFilter(result.Source, languageId, true)
                           .FirstOrDefaultAsync());
                }

                return(null);
            }
        }
Пример #3
0
        public virtual XmlSitemapNode CreateNode(LinkGenerator linkGenerator, string baseUrl, NamedEntity entity, UrlRecordCollection slugs, Language language)
        {
            var slug = slugs.GetSlug(language.Id, entity.Id, true);

            //var path = linkGenerator.GetPathByRouteValues(entity.EntityName, new { SeName = slug }).EmptyNull().TrimStart('/');
            //var loc = baseUrl + path;

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

            return(new XmlSitemapNode
            {
                LastMod = entity.LastMod,
                Loc = baseUrl + slug.EmptyNull().TrimStart('/')
            });
        }