예제 #1
0
        public async Task SaveSlugAsync <T>(T entity, string slug, CancellationToken cancellationToken = default)
            where T : ISlugSupported
        {
            Check.NotNull(entity, nameof(entity));

            var entityId   = entity.Id;
            var entityName = entity.GetType().Name;

            var query = from ur in _urlRecordRepository.GetList()
                        where ur.EntityId == entityId &&
                        ur.EntityName == entityName
                        orderby ur.Id descending
                        select ur;

            var allUrlRecords   = query.ToList();
            var activeUrlRecord = allUrlRecords.FirstOrDefault(x => x.IsActive);

            UrlRecord nonActiveRecordWithSpecifiedSlug;

            if (activeUrlRecord == null && !string.IsNullOrWhiteSpace(slug))
            {
                //find in non-active records with the specified slug
                nonActiveRecordWithSpecifiedSlug = allUrlRecords
                                                   .FirstOrDefault(
                    x => x.Slug.Equals(slug, StringComparison.InvariantCultureIgnoreCase) && !x.IsActive);

                if (nonActiveRecordWithSpecifiedSlug != null)
                {
                    //mark non-active record as active
                    nonActiveRecordWithSpecifiedSlug.IsActive = true;
                    await _urlRecordRepository.UpdateAsync(nonActiveRecordWithSpecifiedSlug, cancellationToken : cancellationToken);
                }
                else
                {
                    //new record
                    var urlRecord = new UrlRecord(
                        GuidGenerator.Create(),
                        entityId,
                        entityName,
                        slug, true);

                    await _urlRecordRepository.InsertAsync(urlRecord,
                                                           cancellationToken : cancellationToken);
                }
            }

            if (activeUrlRecord != null && string.IsNullOrWhiteSpace(slug))
            {
                //disable the previous active URL record
                activeUrlRecord.IsActive = false;
                await _urlRecordRepository.UpdateAsync(activeUrlRecord, cancellationToken : cancellationToken);
            }

            if (activeUrlRecord == null || string.IsNullOrWhiteSpace(slug))
            {
                return;
            }

            //it should not be the same slug as in active URL record
            if (activeUrlRecord.Slug.Equals(slug, StringComparison.InvariantCultureIgnoreCase))
            {
                return;
            }

            //find in non-active records with the specified slug
            nonActiveRecordWithSpecifiedSlug = allUrlRecords
                                               .FirstOrDefault(x => x.Slug.Equals(slug, StringComparison.InvariantCultureIgnoreCase) && !x.IsActive);

            if (nonActiveRecordWithSpecifiedSlug != null)
            {
                //mark non-active record as active
                nonActiveRecordWithSpecifiedSlug.IsActive = true;
                await _urlRecordRepository.UpdateAsync(nonActiveRecordWithSpecifiedSlug, cancellationToken : cancellationToken);

                //disable the previous active URL record
                activeUrlRecord.IsActive = false;
                await _urlRecordRepository.UpdateAsync(activeUrlRecord, cancellationToken : cancellationToken);
            }
            else
            {
                //insert new record
                //we do not update the existing record because we should track all previously entered slugs
                //to ensure that URLs will work fine
                var urlRecord = new UrlRecord(
                    GuidGenerator.Create(),
                    entityId,
                    entityName,
                    slug,
                    true
                    );

                await _urlRecordRepository.InsertAsync(urlRecord, cancellationToken : cancellationToken);

                //disable the previous active URL record
                activeUrlRecord.IsActive = false;
                await _urlRecordRepository.UpdateAsync(activeUrlRecord, cancellationToken : cancellationToken);
            }
        }
예제 #2
0
        public async Task InsertUrlRecordAsync(UrlRecord urlRecord, CancellationToken cancellationToken = default)
        {
            Check.NotNull(urlRecord, nameof(urlRecord));

            await _urlRecordRepository.InsertAsync(urlRecord, cancellationToken : cancellationToken);
        }
예제 #3
0
        public async Task UpdateUrlRecordAsync(UrlRecord urlRecord, CancellationToken cancellationToken = default)
        {
            Check.NotNull(urlRecord, nameof(urlRecord));

            await _urlRecordRepository.UpdateAsync(urlRecord, autoSave : true, cancellationToken : cancellationToken);
        }
예제 #4
0
 public async Task DeleteUrlRecordAsync(UrlRecord urlRecord, CancellationToken cancellationToken = default)
 {
     await _urlRecordRepository.DeleteAsync(urlRecord.Id, cancellationToken : cancellationToken);
 }