Created
February 8, 2018 13:01
-
-
Save tusmester/3ea71f829e7139ee2c54079a368c696e to your computer and use it in GitHub Desktop.
#sn #mvc #controller #dashboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using SNCR = SenseNet.ContentRepository; | |
using SenseNet.ContentRepository.Storage; | |
using SenseNet.ContentRepository.Storage.Security; | |
using SnWebApplication.Models; | |
namespace SnWebApplication.Controllers | |
{ | |
public class DashboardController : Controller | |
{ | |
public string SitePath { get; } = "/Root/Sites/Default_Site"; | |
public string TaskListName { get; } = "Tasks"; | |
public string TaskListPath => SitePath + "/" + TaskListName; | |
// GET: Dashboard | |
public ActionResult Index() | |
{ | |
return View(new DashboardViewModel()); | |
} | |
// GET: Dashboard/Create | |
public ActionResult Create() | |
{ | |
return View(new TaskViewModel()); | |
} | |
// POST: Dashboard/Create | |
[HttpPost] | |
public ActionResult Create(TaskViewModel taskViewModel) | |
{ | |
try | |
{ | |
var parent = Node.LoadNode(TaskListPath); | |
if (parent == null) | |
{ | |
// create the container if does not exist | |
using (new SystemAccount()) | |
{ | |
var list = SNCR.Content.CreateNew("TaskList", Node.LoadNode(SitePath), TaskListName); | |
list.Save(); | |
parent = list.ContentHandler; | |
} | |
} | |
// build and save the new task | |
var task = SNCR.Content.CreateNew("Task", parent, null); | |
task.DisplayName = taskViewModel.DisplayName; | |
task["Description"] = taskViewModel.Description; | |
task["DueDate"] = DateTime.Now.AddDays(1); | |
task["AssignedTo"] = Node.LoadNode(taskViewModel.AssignedToId); | |
task.Save(); | |
return RedirectTo("Index"); | |
} | |
catch | |
{ | |
return View(); | |
} | |
} | |
// GET: Dashboard/Delete/5 | |
public ActionResult Delete(int id) | |
{ | |
// find the task using LINQ to sn | |
var task = SNCR.Content.All.FirstOrDefault(c => c.Id == id && c.TypeIs("Task")); | |
// display the confirm delete view | |
return task == null | |
? View() | |
: View(new TaskViewModel(task.ContentHandler as SNCR.Task)); | |
} | |
// POST: Dashboard/Delete/5 | |
[HttpPost] | |
[ActionName("Delete")] | |
public ActionResult DeletePost(int id) | |
{ | |
try | |
{ | |
if (id > 0) | |
SNCR.Content.DeletePhysical(id); | |
return RedirectTo("Index"); | |
} | |
catch | |
{ | |
return View(); | |
} | |
} | |
private ActionResult RedirectTo(string actionName) | |
{ | |
// this is a helper method for simplifying redirects | |
return RedirectToRoute("Default", new RouteValueDictionary | |
{ | |
{"controller", "Dashboard"}, | |
{"action", actionName} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment