예제 #1
0
        public virtual void UpdateCustomPageUrl(CustomPageUrl urlRecord)
        {
            if (urlRecord == null)
            {
                throw new ArgumentNullException("Custom Page URL");
            }

            _customPageUrlRepository.Update(urlRecord);
        }
예제 #2
0
        public virtual void SaveSlug <T>(T entity, string slug) where T : BaseEntity, ISlugSupported
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            int    entityId   = entity.Id;
            string entityName = typeof(T).Name;

            var query = from ur in _customPageUrlRepository.Table
                        where ur.EntityId == entityId &&
                        ur.EntityName == entityName
                        orderby ur.Id descending
                        select ur;
            var allCustomPageUrls   = query.ToList();
            var activeCustomPageUrl = allCustomPageUrls.FirstOrDefault(x => x.IsActive);

            if (activeCustomPageUrl == null && !string.IsNullOrWhiteSpace(slug))
            {
                //find in non-active records with the specified slug
                var nonActiveRecordWithSpecifiedSlug = allCustomPageUrls
                                                       .FirstOrDefault(x => x.Slug.Equals(slug, StringComparison.InvariantCultureIgnoreCase) && !x.IsActive);
                if (nonActiveRecordWithSpecifiedSlug != null)
                {
                    //mark non-active record as active
                    nonActiveRecordWithSpecifiedSlug.IsActive = true;
                    UpdateCustomPageUrl(nonActiveRecordWithSpecifiedSlug);
                }
                else
                {
                    //new record
                    var urlRecord = new CustomPageUrl
                    {
                        EntityId   = entityId,
                        EntityName = entityName,
                        Slug       = slug,
                        IsActive   = true,
                        CreatedOn  = DateTime.Now,
                        ModifiedOn = DateTime.Now,
                        UserId     = entity.UserId
                    };
                    InsertCustomPageUrl(urlRecord);
                }
            }

            if (activeCustomPageUrl != null && string.IsNullOrWhiteSpace(slug))
            {
                //disable the previous active URL record
                activeCustomPageUrl.IsActive = false;
                UpdateCustomPageUrl(activeCustomPageUrl);
            }

            if (activeCustomPageUrl != null && !string.IsNullOrWhiteSpace(slug))
            {
                //it should not be the same slug as in active URL record
                if (!activeCustomPageUrl.Slug.Equals(slug, StringComparison.InvariantCultureIgnoreCase))
                {
                    //find in non-active records with the specified slug
                    var nonActiveRecordWithSpecifiedSlug = allCustomPageUrls
                                                           .FirstOrDefault(x => x.Slug.Equals(slug, StringComparison.InvariantCultureIgnoreCase) && !x.IsActive);
                    if (nonActiveRecordWithSpecifiedSlug != null)
                    {
                        //mark non-active record as active
                        nonActiveRecordWithSpecifiedSlug.IsActive = true;
                        UpdateCustomPageUrl(nonActiveRecordWithSpecifiedSlug);

                        //disable the previous active URL record
                        activeCustomPageUrl.IsActive = false;
                        UpdateCustomPageUrl(activeCustomPageUrl);
                    }
                    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 CustomPageUrl
                        {
                            EntityId   = entityId,
                            EntityName = entityName,
                            Slug       = slug,
                            IsActive   = true,
                            CreatedOn  = DateTime.Now,
                            ModifiedOn = DateTime.Now,
                            UserId     = entity.UserId
                        };
                        InsertCustomPageUrl(urlRecord);

                        //disable the previous active URL record
                        activeCustomPageUrl.IsActive = false;
                        UpdateCustomPageUrl(activeCustomPageUrl);
                    }
                }
            }
        }