Ejemplo n.º 1
0
        public virtual VirtualPathData GetVirtualPathData(IRouter router, VirtualPathContext context)
        {
            var categoryPath     = "";
            var seName           = "";
            var id               = "0";
            var date             = default(DateTime);
            var parentEntityPath = "";

            if (context.Values.ContainsKey("id"))
            {
                id = context.Values["id"].ToString();
            }

            if (context.Values.ContainsKey("seName"))
            {
                seName = context.Values["seName"]?.ToString();
            }

            if (context.Values.ContainsKey("categoryPath"))
            {
                categoryPath = context.Values["categoryPath"]?.ToString();
            }

            if (context.Values.ContainsKey("parentEntityPath"))
            {
                parentEntityPath = context.Values["parentEntityPath"]?.ToString();
            }

            if (context.Values.ContainsKey("date"))
            {
                date = Convert.ToDateTime(context.Values["date"]);
            }

            if (id.IsNullEmptyOrWhiteSpace() && seName.IsNullEmptyOrWhiteSpace())
            {
                throw new Exception("At least seName or id must be provided to generate the route");
            }
            var url          = "";
            var entityName   = "";
            var dynamicRoute = DynamicRoutes.FirstOrDefault(x => x.RouteName == context.RouteName);

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

            url        = dynamicRoute.Template;
            entityName = dynamicRoute.SeoEntityName;

            if (id.IsNullEmptyOrWhiteSpace() && id != "0")
            {
                var seoMeta = _seoMetaService.FirstOrDefault(x => x.Slug == seName && x.EntityName == entityName);
                if (seoMeta == null)
                {
                    return(null);
                }
                id = seoMeta.EntityId.ToString();
            }
            var idAsInt = 0;

            if (seName.IsNullEmptyOrWhiteSpace())
            {
                idAsInt = Convert.ToInt32(id);
                var seoMeta = _seoMetaService.FirstOrDefault(x => x.EntityId == idAsInt && x.EntityName == entityName);
                if (seoMeta == null)
                {
                    return(null);
                }
                seName = seoMeta.Slug;
            }

            if (idAsInt == 0)
            {
                int.TryParse(id, out idAsInt);
            }

            if (categoryPath.IsNullEmptyOrWhiteSpace() && url.Contains("{CategoryPath}"))
            {
                switch (entityName)
                {
                case nameof(Category):
                    var allCategories   = _categoryService.GetFullCategoryTree();
                    var primaryCategory = allCategories.FirstOrDefault(x => x.Id == idAsInt);
                    categoryPath = primaryCategory.GetCategoryPath();
                    break;
                }
            }

            if (parentEntityPath.IsNullEmptyOrWhiteSpace() && url.Contains("{ParentEntityPath}"))
            {
                switch (entityName)
                {
                case nameof(ContentPage):
                    var contentPage = _contentPageService.GetWithTree(idAsInt);
                    parentEntityPath = contentPage.GetParentPath();
                    break;
                }
            }
            url = url.Replace("{Id}", id)
                  .Replace("{SeName}", seName)
                  .Replace("{CategoryPath}", categoryPath)
                  .Replace("{ParentEntityPath}", parentEntityPath)
                  .Replace("{Day}", date.Day.ToString())
                  .Replace("{Month}", date.Month.ToString())
                  .Replace("{Year}", date.Year.ToString())
                  .Replace("//", "/");

            var vpd = new VirtualPathData(router, url);

            return(vpd);
        }