- Create/Read/Update/Delete actions for the Roles module.

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4039504
This commit is contained in:
suhacan 2009-11-11 00:25:29 +00:00
parent 51c4cec177
commit eb9cfcbda2
10 changed files with 94 additions and 41 deletions

View File

@ -1,43 +1,94 @@
using System.Linq;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using Orchard.Data;
using Orchard.Notify;
using Orchard.Roles.Models;
using Orchard.Roles.Services;
using Orchard.Roles.ViewModels;
namespace Orchard.Roles.Controllers {
[ValidateInput(false)]
public class AdminController : Controller {
private readonly IRepository<RoleRecord> _roleRepository;
private readonly IRoleService _roleService;
private readonly INotifier _notifier;
public AdminController(IRepository<RoleRecord> roleRepository, INotifier notifier) {
_roleRepository = roleRepository;
public AdminController(IRoleService roleService, INotifier notifier) {
_roleService = roleService;
_notifier = notifier;
}
public ActionResult Index() {
var model = new RolesIndexViewModel {
Rows = _roleRepository.Fetch(x => x.Name != null)
.Select(x => new RolesIndexViewModel.Row {
Role = x
})
.ToList()
};
var model = new RolesIndexViewModel { Rows = _roleService.GetRoles() as IList<RoleRecord> };
return View(model);
}
//TODO: NYI
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection input) {
try {
foreach (string key in input.Keys) {
if (key.StartsWith("Checkbox.") && input[key] == "true") {
int roleId = Convert.ToInt32(key.Substring("Checkbox.".Length));
_roleService.DeleteRole(roleId);
}
}
return RedirectToAction("Index");
}
catch (Exception exception) {
_notifier.Error("Deleting Role failed: " + exception.Message);
return View();
}
}
public ActionResult Create() {
var model = new RoleCreateViewModel();
return View(model);
}
//TODO: NYI
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection input) {
var viewModel = new RoleCreateViewModel();
try {
UpdateModel(viewModel, input.ToValueProvider());
_roleService.CreateRole(viewModel.Name);
return RedirectToAction("Index");
}
catch (Exception exception) {
_notifier.Error("Creating Role failed: " + exception.Message);
return View(viewModel);
}
}
public ActionResult Edit(int id) {
var model = new RoleEditViewModel();
var role = _roleService.GetRole(id);
if (role == null) {
//TODO: Error message
throw new HttpException(404, "page with id " + id + " was not found");
}
var model = new RoleEditViewModel { Name = role.Name, Id = role.Id };
return View(model);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(FormCollection input) {
var viewModel = new RoleEditViewModel();
try {
UpdateModel(viewModel, input.ToValueProvider());
// Save
if (!String.IsNullOrEmpty(HttpContext.Request.Form["submit.Save"])) {
_roleService.UpdateRole(viewModel.Id, viewModel.Name);
}
else if (!String.IsNullOrEmpty(HttpContext.Request.Form["submit.Delete"])) {
_roleService.DeleteRole(viewModel.Id);
}
return RedirectToAction("Index");
}
catch (Exception exception) {
_notifier.Error("Editing media file failed: " + exception.Message);
return View(viewModel);
}
}
}
}

View File

@ -66,6 +66,7 @@
<Compile Include="Models\RoleRecord.cs" />
<Compile Include="Models\RolesPermissions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\RoleService.cs" />
<Compile Include="ViewModels\RoleCreateViewModel.cs" />
<Compile Include="ViewModels\RoleEditViewModel.cs" />
<Compile Include="ViewModels\RolesIndexViewModel.cs" />

View File

@ -1,6 +1,9 @@
using Orchard.Mvc.ViewModels;
using System.ComponentModel.DataAnnotations;
using Orchard.Mvc.ViewModels;
namespace Orchard.Roles.ViewModels {
public class RoleCreateViewModel : AdminViewModel {
[Required]
public string Name { get; set; }
}
}

View File

@ -1,6 +1,10 @@
using Orchard.Mvc.ViewModels;
using System.ComponentModel.DataAnnotations;
using Orchard.Mvc.ViewModels;
namespace Orchard.Roles.ViewModels {
public class RoleEditViewModel : AdminViewModel {
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
}

View File

@ -4,10 +4,6 @@ using Orchard.Roles.Models;
namespace Orchard.Roles.ViewModels {
public class RolesIndexViewModel : AdminViewModel {
public class Row {
public RoleRecord Role { get; set; }
}
public IList<Row> Rows { get; set; }
public IList<RoleRecord> Rows { get; set; }
}
}

View File

@ -10,11 +10,13 @@
</head>
<body>
<% Html.Include("Header"); %>
<% Html.BeginForm(); %>
<%= Html.ValidationSummary() %>
<div class="yui-g">
<h2 class="separator">Add a Role</h2>
<h3>Information</h3>
<label for="pageTitle">Role Name:</label>
<input id="pageTitle" class="inputText inputTextLarge" name="pageTitle" type="text" />
<input id="Name" class="inputText inputTextLarge" name="Name" type="text" value="<%= Model.Name %>" />
<h3>Permissions</h3>
@ -43,9 +45,9 @@
<td><input type="checkbox" value="" /></td>
</tr>
</table>
<input type="button" class="button" value="Save" />
<input type="button" class="button" value="Delete" />
<input type="submit" class="button" value="Save" />
</div>
<% Html.EndForm(); %>
<% Html.Include("Footer"); %>
</body>
</html>

View File

@ -10,12 +10,14 @@
</head>
<body>
<% Html.Include("Header"); %>
<% Html.BeginForm(); %>
<%= Html.ValidationSummary() %>
<div class="yui-g">
<h2 class="separator">Edit a Role</h2>
<h3>Information</h3>
<label for="pageTitle">Role Name:</label>
<input id="pageTitle" class="inputText inputTextLarge" name="pageTitle" type="text" />
<input id="Name" class="inputText inputTextLarge" name="Name" type="text" value="<%=Model.Name %>"/>
<input type="hidden" value="<%= Model.Id %>" name="Id" />
<h3>Permissions</h3>
<h4>Pages Module</h4>
@ -43,9 +45,10 @@
<td><input type="checkbox" value="" /></td>
</tr>
</table>
<input type="button" class="button" value="Save" />
<input type="button" class="button" value="Delete" />
<input type="submit" class="button" name="submit.Save" value="Save" />
<input type="submit" class="button" name="submit.Delete" value="Delete" />
</div>
<% Html.EndForm(); %>
<% Html.Include("Footer"); %>
</body>
</html>

View File

@ -39,9 +39,9 @@
</thead>
<%foreach (var row in Model.Rows) { %>
<tr>
<td><input type="checkbox" value="true" name="<%= "Checkbox." + row.Role.Name %>"/></td>
<td><%=Html.Encode(row.Role.Name) %></td>
<td><%=Html.ActionLink("Edit", "Edit", new { row.Role.Id })%></td>
<td><input type="checkbox" value="true" name="<%= "Checkbox." + row.Id %>"/></td>
<td><%=Html.Encode(row.Name) %></td>
<td><%=Html.ActionLink("Edit", "Edit", new { row.Id })%></td>
</tr>
<%}%>
</table>

View File

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using Autofac.Builder;
using Autofac.Builder;
namespace Orchard.Data {
public class DataModule : Module {

View File

@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Web;
using FluentNHibernate.Automapping;
@ -29,6 +26,7 @@ namespace Orchard.Data {
var automaps = new[] {
CreatePersistenceModel(Assembly.Load("Orchard.CmsPages")),
CreatePersistenceModel(Assembly.Load("Orchard.Users")),
CreatePersistenceModel(Assembly.Load("Orchard.Roles")),
CreatePersistenceModel(Assembly.Load("Orchard")),
};