public static void UpdateSeoMetaForEntity <T>(this ISeoMetaService seoMetaService, T entity, SeoMetaModel seoMetaModel) where T : FoundationEntity { var seoMeta = seoMetaService.FirstOrDefault( x => x.EntityId == entity.Id && x.EntityName == typeof(T).Name); if (seoMeta != null) { seoMeta.PageTitle = seoMetaModel.PageTitle; seoMeta.MetaKeywords = seoMetaModel.MetaKeywords; seoMeta.MetaDescription = seoMetaModel.MetaDescription; if (!seoMetaModel.Slug.IsNullEmptyOrWhiteSpace()) { //check if slug is safe to use, modify if required var slug = seoMetaModel.Slug; SeoMeta savedSeoMeta = null; var index = 1; while ((savedSeoMeta = seoMetaService.FirstOrDefault(x => x.EntityName == typeof(T).Name && x.Slug == slug && x.Id != seoMeta.Id)) != null) { slug = savedSeoMeta.Slug + (index++); } seoMeta.Slug = slug; } seoMetaService.Update(seoMeta); } }
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); }