Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modelsbuilder #15

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 7 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,11 @@ MigrationBackup/
FodyWeavers.xsd

# Custom project ignores
/UmbracoNineDemoSite.Web/umbraco/Data/*
/UmbracoNineDemoSite.Web/umbraco/Data/
/UmbracoNineDemoSite.Web/umbraco/config/
/UmbracoNineDemoSite.Web/umbraco/mediacache
/UmbracoNineDemoSite.Web/wwwroot/umbraco/lib/*
*.sdf
/UmbracoNineDemoSite.Web/wwwroot/umbraco/

/UmbracoNineDemoSite.Web/App_Plugins/usync/
*.sdf
/UmbracoNineDemoSite.Web/.config/dotnet-tools.json
4 changes: 4 additions & 0 deletions ModelsBuilder-Setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ModelsBuilder
After checking out branch ***modelsbuilder*** and building, go into back office ***settings > uSync*** section and import ***Settings*** and ***Content***.

Then goto ***settings : Models Builder*** and generate models (which should be outdated).
40 changes: 32 additions & 8 deletions UmbracoNineDemoSite.Core/Features/Home/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,40 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Extensions;
using UmbracoNineDemoSite.Core.Features.Shared.Components.Hero;
using UmbracoNineDemoSite.Core.Features.Shared.Extensions;
using generatedModels = UmbracoNineDemoSite.Core;

namespace UmbracoNineDemoSite.Core.Features.Home
{
public class HomeController : RenderController
{
public HomeController(ILogger<RenderController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor) : base(logger, compositeViewEngine, umbracoContextAccessor) { }
public class HomeController : RenderController
{
public HomeController(ILogger<RenderController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor) : base(logger, compositeViewEngine, umbracoContextAccessor) { }

public IActionResult Home(ContentModel model)
{
return View(new HomeViewModel(model.Content));
}
}
public IActionResult Home(ContentModel model)
{
var mbModel = model.Content as generatedModels.Home ?? new generatedModels.Home(model.Content, null);
var viewModel = new HomeViewModel()
{
Heading = mbModel.Heading,
Preamble = mbModel.Preamble,
BackgroundImage = mbModel.BackgroundImage,
CallToActionLabel = mbModel.CallToActionLabel,
CallToActionUrl = mbModel.CallToActionUrl?.Url(),
Blocks = mbModel.Blocks
};
viewModel.Hero = new HeroViewModel()
{
CallToActionUrl = viewModel.CallToActionUrl,
CallToActionLabel = viewModel.CallToActionLabel,
BackgroundImageUrl = viewModel.BackgroundImage,
Preamble = viewModel.Preamble,
Heading = viewModel.Heading
};
viewModel.MapSitePageBase(mbModel);

return View(viewModel);
}
}
}
26 changes: 8 additions & 18 deletions UmbracoNineDemoSite.Core/Features/Home/HomeViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
using Umbraco.Cms.Core.Models.Blocks;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Extensions;
using UmbracoNineDemoSite.Core.Features.Shared.Components.Hero;
using UmbracoNineDemoSite.Core.Features.Shared.Constants;
using UmbracoNineDemoSite.Core.Features.Shared.Content;

namespace UmbracoNineDemoSite.Core.Features.Home
{
public class HomeViewModel : SitePageBase, IHeadingPage
{
public HomeViewModel(IPublishedContent content) : base(content) { }
public HomeViewModel() : base() { }

public string Heading => this.Content.GetProperty(PropertyAlias.Heading).GetValue() as string;
public string Preamble => this.Content.GetProperty(PropertyAlias.Preamble).GetValue() as string;
public string BackgroundImage => this.Content.GetProperty(PropertyAlias.BackgroundImage).GetValue() as string;
public string CallToActionUrl => (this.Content.GetProperty(PropertyAlias.CallToActionUrl).GetValue() as IPublishedContent)?.Url();
public string CallToActionLabel => this.Content.GetProperty(PropertyAlias.CallToActionLabel).GetValue() as string;
public BlockListModel Blocks => this.Content.GetProperty(PropertyAlias.Blocks).GetValue() as BlockListModel;
public HeroViewModel Hero => new()
{
Heading = this.Heading,
Preamble = this.Preamble,
BackgroundImageUrl = this.BackgroundImage,
CallToActionUrl = this.CallToActionUrl,
CallToActionLabel = this.CallToActionLabel
};
public string Heading { get; set; }
public string Preamble { get; set; }
public string BackgroundImage { get; set; }
public string CallToActionUrl { get; set; }
public string CallToActionLabel { get; set; }
public BlockListModel Blocks { get; set; }
public HeroViewModel Hero { get; set; }
}
}
13 changes: 12 additions & 1 deletion UmbracoNineDemoSite.Core/Features/Page/PageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common.Controllers;
using UmbracoNineDemoSite.Core.Features.Shared.Extensions;
using generatedModels = UmbracoNineDemoSite.Core;

namespace UmbracoNineDemoSite.Core.Features.Page
{
Expand All @@ -13,7 +15,16 @@ public PageController(ILogger<RenderController> logger, ICompositeViewEngine com

public IActionResult Page(ContentModel model)
{
return View(new PageViewModel(model.Content));
var mbModel = model.Content as generatedModels.Page ?? new generatedModels.Page(model.Content, null);
var viewModel = new PageViewModel()
{
Heading = mbModel.Heading,
BodyText = mbModel.BodyText,
Blocks = mbModel.Blocks
};
viewModel.MapSitePageBase(mbModel);

return View(viewModel);
}
}
}
21 changes: 9 additions & 12 deletions UmbracoNineDemoSite.Core/Features/Page/PageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
using Microsoft.AspNetCore.Html;
using Umbraco.Cms.Core.Models.Blocks;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Models.Blocks;
using Umbraco.Cms.Core.Strings;
using UmbracoNineDemoSite.Core.Features.Shared.Constants;
using UmbracoNineDemoSite.Core.Features.Shared.Content;

namespace UmbracoNineDemoSite.Core.Features.Page
{
public class PageViewModel : SitePageBase, IHeadingPage
{
public PageViewModel(IPublishedContent content) : base(content) { }
public class PageViewModel : SitePageBase, IHeadingPage
{
public PageViewModel() : base() { }

public string Heading => this.Content.GetProperty(PropertyAlias.Heading).GetValue() as string;

public HtmlEncodedString BodyText => this.Content.GetProperty(PropertyAlias.BodyText).GetValue() as HtmlEncodedString;
public string Heading { get; set; }

public BlockListModel Blocks => this.Content.GetProperty(PropertyAlias.Blocks).GetValue() as BlockListModel;
}
public IHtmlEncodedString BodyText { get; set; }

public BlockListModel Blocks { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ namespace UmbracoNineDemoSite.Core.Features.Products
{
public class ProductPageViewModel : SitePageBase, IHeadingPage
{
private readonly IPublishedContent container;

public ProductPageViewModel(IProduct product, IPublishedContent container) : base(container)
public ProductPageViewModel(IProduct product) : base()
{
this.Id = product.Id;
this.Name = product.Name;
this.Description = product.Description;
this.PageDescription = product.ShortDescription;
this.ImageUrl = product.ImageUrl;
this.Price = product.Price;
this.container = container;
}

public new int Id { get; set; }
Expand All @@ -26,7 +25,8 @@ public ProductPageViewModel(IProduct product, IPublishedContent container) : bas
public int Price { get; set; }
public string UrlSegment => $"{Id}/{Name}";

public string Heading => this.container.GetProperty(PropertyAlias.Heading).GetValue() as string;
public string Heading { get; set; }
public override string PageTitle => this.Name;
public override string PageDescription { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,60 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Extensions;
using UmbracoNineDemoSite.Core.Features.Shared.Extensions;
using UmbracoNineDemoSite.Integrations.Products.Entities;
using UmbracoNineDemoSite.Integrations.Products.Services;
using generatedModels = UmbracoNineDemoSite.Core;

namespace UmbracoNineDemoSite.Core.Features.Products
{
public class ProductsContainerController : RenderController
{
private readonly IProductService productService;
public class ProductsContainerController : RenderController
{
private readonly IProductService productService;

public ProductsContainerController(ILogger<RenderController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor, IProductService productService) : base(logger, compositeViewEngine, umbracoContextAccessor)
{
this.productService = productService;
}
public ProductsContainerController(ILogger<RenderController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor, IProductService productService) : base(logger, compositeViewEngine, umbracoContextAccessor)
{
this.productService = productService;
}

public IActionResult ProductsContainer(ContentModel model)
{
if (CurrentProduct == null)
{
var viewModel = new ProductsContainerViewModel(model.Content)
{
Products = this.productService.GetAll().Select(product => new ProductPageViewModel(product, model.Content))
};
public IActionResult ProductsContainer(ContentModel model)
{
var mbModel = model.Content as generatedModels.ProductsContainer ?? new generatedModels.ProductsContainer(model.Content, null);
if (CurrentProduct == null)
{
var viewModel = new ProductsContainerViewModel()
{
Products = this.productService.GetAll().Select(product => new ProductPageViewModel(product)),
Heading = mbModel.Heading
};
viewModel.MapSitePageBase(mbModel);

return View("ProductsContainer", viewModel);
}
else
{
var viewModel = new ProductPageViewModel(CurrentProduct, model.Content);
return View("ProductPage", viewModel);
}
}
return View("ProductsContainer", viewModel);
}
else
{
var viewModel = new ProductPageViewModel(CurrentProduct)
{
Heading = mbModel.Heading,
SiteName = mbModel?.Root().Name
};

public IProduct CurrentProduct {
get {
var segments = Request.Path.Value.Split("/");
if (!int.TryParse(segments[2], out var id))
{
return null;
}
return this.productService.Get(id);
}
}
}
return View("ProductPage", viewModel);
}
}

public IProduct CurrentProduct
{
get
{
var segments = Request.Path.Value.Split("/");
if (!int.TryParse(segments[2], out var id))
{
return null;
}
return this.productService.Get(id);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System.Collections.Generic;
using Umbraco.Cms.Core.Models.PublishedContent;
using UmbracoNineDemoSite.Core.Features.Shared.Constants;
using UmbracoNineDemoSite.Core.Features.Shared.Content;

namespace UmbracoNineDemoSite.Core.Features.Products
{
public class ProductsContainerViewModel : SitePageBase, IHeadingPage
{
public ProductsContainerViewModel(IPublishedContent content) : base(content) { }
public class ProductsContainerViewModel : SitePageBase, IHeadingPage
{
public ProductsContainerViewModel() : base() { }

public string Heading => this.Content.GetProperty(PropertyAlias.Heading).GetValue() as string;
public string Heading { get; set; }

public IEnumerable<ProductPageViewModel> Products { get; set; }
}
public IEnumerable<ProductPageViewModel> Products { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,49 @@

namespace UmbracoNineDemoSite.Core.Features.Search.Controllers
{
public class SearchController : SurfaceController
{
private readonly SearchService _searchService;
public SearchController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory,
ServiceContext services, AppCaches appCaches,
IProfilingLogger profilingLogger, IPublishedUrlProvider publishedUrlProvider,
SearchService searchService) : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
{
_searchService = searchService;
}
public class SearchController : SurfaceController
{
private readonly SearchService _searchService;
private readonly IPublishedUrlProvider _publishedUrlProvider;
public SearchController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory,
ServiceContext services, AppCaches appCaches,
IProfilingLogger profilingLogger, IPublishedUrlProvider publishedUrlProvider,
SearchService searchService) : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
{
_searchService = searchService;
_publishedUrlProvider = publishedUrlProvider;
}

public RedirectToUmbracoPageResult Search(SearchFormModel model)
{
var criteria = new BaseSearchCriteria()
{
SearchTerm = model.SearchTerm,
Skip = model.Skip,
Take = model.Take
};
public RedirectToUmbracoPageResult Search(SearchFormModel model)
{
var criteria = new BaseSearchCriteria()
{
SearchTerm = model.SearchTerm,
Skip = model.Skip,
Take = model.Take
};

var viewModel = new SearchResultViewModel()
{
Pages = _searchService.Search(criteria),
Products = _searchService.SearchProducts(criteria)
};
var viewModel = new SearchResultViewModel()
{
Pages = FixContentUrl(_searchService.Search(criteria)),
Products = _searchService.SearchProducts(criteria)
};

TempData.Add(TempDataKey.SearchResults, JsonConvert.SerializeObject(viewModel));
return RedirectToCurrentUmbracoPage();
}
}
TempData.Add(TempDataKey.SearchResults, JsonConvert.SerializeObject(viewModel));
return RedirectToCurrentUmbracoPage();
}
private SearchResults FixContentUrl(SearchResults results)
{
foreach (var result in results.Results)
{
if (!string.IsNullOrEmpty(result.Url)) continue;

if (int.TryParse(result.Id, out int id))
{
result.Url = _publishedUrlProvider.GetUrl(id);
}
}
return results;
}
}
}
Loading