Skip to content

Commit

Permalink
Did some refactoring on how we handle loading of pages in edit mode, …
Browse files Browse the repository at this point in the history
…now using StructureInfo
  • Loading branch information
Marcus Lindblom committed Jan 6, 2013
1 parent 1b7516e commit 57d0fe8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ namespace BrickPile.UI.Controllers {

[Authorize]
public class PagesController : Controller {
private dynamic _model;
private readonly IStructureInfo _structureInfo;
private dynamic _model;
private readonly IDocumentSession _session;
/// <summary>
/// Default action
Expand All @@ -50,19 +51,12 @@ public ActionResult Index(bool deleted = false) {
return View("Start", new NewModel());
}

var id = (string)_model.Id;
var parentId = _model.Parent != null ? (string)_model.Parent.Id : null;

var viewModel = new IndexViewModel
{
RootModel = _session.Query<IPageModel>().SingleOrDefault(model => model.Parent == null),
CurrentModel = _model,
ParentModel = parentId != null ? _session.Load<IPageModel>(parentId) : null,
Children = _session.Query<IPageModel, DocumentsByParent>()
.Where(model => model.Parent.Id == id)
.Where(model => model.Metadata.IsDeleted == deleted)
.OrderBy(model => model.Metadata.SortOrder)
.ToList()
RootModel = _structureInfo.StartPage,
CurrentModel = _structureInfo.CurrentPage,
ParentModel = _structureInfo.ParentPage,
Children = _structureInfo.NavigationContext.Where(x => _structureInfo.CurrentPage.Children.Contains(x.Id))
};

return View("Index", viewModel);
Expand All @@ -73,19 +67,17 @@ public ActionResult Index(bool deleted = false) {
/// </summary>
/// <returns></returns>
public ActionResult Edit() {
IPageModel parent = _model.Parent != null ? _session.Load<IPageModel>(_model.Parent.Id) : null;

var viewModel = new EditViewModel
{
RootModel = _session.Query<IPageModel>().SingleOrDefault(model => model.Parent == null),
CurrentModel = _model,
ParentModel = parent,
IlligalSlugs = parent != null ? Newtonsoft.Json.JsonConvert.SerializeObject(_session.Load<IPageModel>(parent.Children).Select(x => x .Metadata.Slug)) : null
RootModel = _structureInfo.StartPage,
CurrentModel = _structureInfo.CurrentPage,
ParentModel = _structureInfo.ParentPage,
IlligalSlugs = _structureInfo.ParentPage != null ?
Newtonsoft.Json.JsonConvert.SerializeObject(_session.Load<IPageModel>(_structureInfo.ParentPage.Children).Select(x => x.Metadata.Slug)) :
null
};

if (Request.IsAjaxRequest()) {
return PartialView("Edit", viewModel);
}

ViewBag.Class = "edit";
return View(viewModel);
}
Expand All @@ -98,13 +90,14 @@ public ActionResult Edit() {
public virtual ActionResult Update() {

if (!TryUpdateModel(_model, "CurrentModel")) {
var parentId = _model.Parent != null ? (string)_model.Parent.Id : null;

var viewModel = new EditViewModel
{
RootModel = _session.Query<IPageModel>().SingleOrDefault(model => model.Parent == null),
CurrentModel = _model,
ParentModel = parentId != null ? _session.Load<IPageModel>(parentId) : null,
RootModel = _structureInfo.StartPage,
CurrentModel = _structureInfo.CurrentPage,
ParentModel = _structureInfo.ParentPage,
};

return View("edit", viewModel);
}

Expand All @@ -117,8 +110,9 @@ public virtual ActionResult Update() {

var page = _model as IPageModel;

if (page.Parent != null) {
_model = _session.Load<IPageModel>(page.Parent.Id);
if (page.Parent != null)
{
_model = _structureInfo.ParentPage;
}

return RedirectToAction("index", new { model = _model });
Expand Down Expand Up @@ -175,16 +169,15 @@ public ActionResult New(NewModel newModel) {
page.Metadata.Published = DateTime.Now;

var viewModel = new NewPageViewModel
{
RootModel = _session.Query<IPageModel>().SingleOrDefault(model => model.Parent == null),
ParentModel = parent,
NewPageModel = page,
SlugsInUse = parent != null ? Newtonsoft.Json.JsonConvert.SerializeObject(_session.Load<IPageModel>(parent.Children).Select(x => x.Metadata.Slug)) : null
};
{
RootModel = _session.Query<IPageModel>().SingleOrDefault(model => model.Parent == null),
ParentModel = parent,
NewPageModel = page,
SlugsInUse = parent != null ? Newtonsoft.Json.JsonConvert.SerializeObject(_session.Load<IPageModel>(parent.Children).Select(x => x.Metadata.Slug)) : null
};

ViewBag.Class = "edit";
return View("new", viewModel);
//return PartialView("new", viewModel);

}

Expand All @@ -198,12 +191,14 @@ public ActionResult New(NewModel newModel) {
[ValidateInput(false)]
public virtual ActionResult Save() {

if (ModelState.IsValid) {
if (ModelState.IsValid)
{
var parent = _model as PageModel;
// create a new page from the new model
var page = Activator.CreateInstance(Type.GetType(Request.Form["AssemblyQualifiedName"])) as dynamic;

if(!TryUpdateModel(page,"NewPageModel")) {
if (!TryUpdateModel(page, "NewPageModel"))
{
var viewModel = new NewPageViewModel
{
RootModel = _session.Query<IPageModel>().SingleOrDefault(model => model.Parent == null),
Expand All @@ -219,7 +214,8 @@ public virtual ActionResult Save() {
// Store the new page
_session.Store(page);
// Set the parent if it's not the start page
if (parent != null) {
if (parent != null)
{
page.Parent = _model;
parent.Children.Add(page.Id);

Expand All @@ -230,7 +226,7 @@ public virtual ActionResult Save() {
// Set changed date
page.Metadata.Changed = DateTime.Now;
page.Metadata.ChangedBy = HttpContext.User.Identity.Name;

// Add page to repository and save changes
_session.SaveChanges();

Expand Down Expand Up @@ -285,12 +281,15 @@ where page.Metadata.Name.StartsWith(term)

return Json(r, JsonRequestBehavior.AllowGet);
}
/// <summary>
/// Initializes a new instance of the <b>PagesController</b> class.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="session">The session.</param>
public PagesController(IPageModel model, IDocumentSession session) {

/// <summary>
/// Initializes a new instance of the <b>PagesController</b> class.
/// </summary>
/// <param name="structureInfo"></param>
/// <param name="model">The model.</param>
/// <param name="session">The session.</param>
public PagesController(IStructureInfo structureInfo, IPageModel model, IDocumentSession session) {
_structureInfo = structureInfo;
_model = model;
_session = session;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
Expand Down Expand Up @@ -143,7 +144,13 @@ public override RouteData GetRouteData(HttpContextBase httpContext) {
}

routeData.ApplyCurrentPage(DefaultControllerName, pathData.Action, pathData.CurrentPage);

routeData.ApplyCurrentStructureInfo(new StructureInfo
{
NavigationContext = pathData.NavigationContext.OrderBy(x => x.Metadata.SortOrder),
CurrentPage = pathData.CurrentPage,
StartPage = pathData.NavigationContext.Single(x => x.Parent == null),
ParentPage = pathData.CurrentPage.Parent != null ? pathData.NavigationContext.SingleOrDefault(x => x.Id == pathData.CurrentPage.Parent.Id) : null
});
return routeData;
}
/// <summary>
Expand Down

0 comments on commit 57d0fe8

Please sign in to comment.