重构代码:移动Menu缓存处理到MenuHelper类中

This commit is contained in:
Argo-MacBookPro 2018-10-31 12:08:57 +08:00
parent a0803b5179
commit 06728127d3
8 changed files with 125 additions and 23 deletions

View File

@ -20,7 +20,7 @@ namespace Bootstrap.Admin.Controllers.Api
/// <param name="value"></param>
/// <returns></returns>
[HttpGet]
public QueryData<BootstrapMenu> Get(QueryMenuOption value)
public QueryData<object> Get(QueryMenuOption value)
{
return value.RetrieveData(User.Identity.Name);
}

View File

@ -1,5 +1,4 @@
using Bootstrap.DataAccess;
using Bootstrap.Security;
using Longbow.Web.Mvc;
using System.Linq;
@ -31,7 +30,7 @@ namespace Bootstrap.Admin.Query
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public QueryData<BootstrapMenu> RetrieveData(string userName)
public QueryData<object> RetrieveData(string userName)
{
var data = MenuHelper.RetrieveMenusByUserName(userName);
if (!string.IsNullOrEmpty(ParentName))
@ -50,7 +49,7 @@ namespace Bootstrap.Admin.Query
{
data = data.Where(t => t.IsResource.ToString() == IsResource);
}
var ret = new QueryData<BootstrapMenu>();
var ret = new QueryData<object>();
ret.total = data.Count();
switch (Sort)
{
@ -78,7 +77,21 @@ namespace Bootstrap.Admin.Query
default:
break;
}
ret.rows = data.Skip(Offset).Take(Limit);
ret.rows = data.Skip(Offset).Take(Limit).Select(p => new
{
p.Id,
p.ParentId,
p.ParentName,
p.Name,
p.Order,
p.Icon,
p.Url,
p.Category,
p.CategoryName,
p.Target,
p.IsResource,
p.ApplicationCode
});
return ret;
}
}

View File

@ -75,7 +75,11 @@
sortName: 'Order',
queryParams: function (params) { return $.extend(params, { parentName: $('#txt_parent_menus_name').val(), name: $("#txt_menus_name").val(), category: $('#sel_menus_category').val(), isresource: $('#sel_menus_res').val() }); }, //传递参数(*
columns: [
{ title: "父级菜单", field: "ParentName", sortable: true },
{
title: "父级菜单", field: "ParentName", sortable: true, formatter: function (value, row, index) {
return (value === "0" || value === null) ? "" : value;
}
},
{ title: "菜单名称", field: "Name", sortable: true },
{ title: "菜单序号", field: "Order", sortable: true },
{

View File

@ -1,6 +1,7 @@
using Bootstrap.Security;
using MongoDB.Driver;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess.MongoDB
{
@ -16,8 +17,60 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns>
public override IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName)
{
var menus = MongoDbAccessManager.DBAccess.GetCollection<BootstrapMenu>("Navigations");
return menus.Find(FilterDefinition<BootstrapMenu>.Empty).ToList();
var dicts = DictHelper.RetrieveDicts().Where(m => m.Category == "菜单");
var menus = MongoDbAccessManager.Menus.Find(FilterDefinition<BootstrapMenu>.Empty).ToList();
menus.ForEach(m =>
{
m.CategoryName = dicts.FirstOrDefault(d => d.Code == m.Category)?.Name;
if (m.ParentId != "0") m.ParentName = menus.FirstOrDefault(p => p.Id == m.ParentId)?.Name;
});
return menus;
}
/// <summary>
///
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public override bool SaveMenu(BootstrapMenu p)
{
if (p.Id == "0")
{
p.Id = null;
MongoDbAccessManager.Menus.InsertOne(p);
return true;
}
else
{
var update = Builders<BootstrapMenu>.Update.Set(md => md.ParentId, p.ParentId)
.Set(md => md.Name, p.Name)
.Set(md => md.Order, p.Order)
.Set(md => md.Icon, p.Icon)
.Set(md => md.Url, p.Url)
.Set(md => md.Category, p.Category)
.Set(md => md.Target, p.Target)
.Set(md => md.IsResource, p.IsResource)
.Set(md => md.ApplicationCode, p.ApplicationCode);
MongoDbAccessManager.Menus.UpdateOne(md => md.Id == p.Id, update);
return true;
}
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public override bool DeleteMenu(IEnumerable<string> value)
{
var list = new List<WriteModel<BootstrapMenu>>();
foreach (var id in value)
{
list.Add(new DeleteOneModel<BootstrapMenu>(Builders<BootstrapMenu>.Filter.Eq(g => g.Id, id)));
}
MongoDbAccessManager.Menus.BulkWrite(list);
return true;
}
}
}

View File

@ -103,6 +103,17 @@ namespace Bootstrap.DataAccess.MongoDB
}
}
/// <summary>
///
/// </summary>
public static IMongoCollection<BootstrapMenu> Menus
{
get
{
return DBAccess.GetCollection<BootstrapMenu>("Navigations");
}
}
private static void InitDb()
{
var connectString = DbAdapterManager.GetConnectionString("ba");

View File

@ -32,7 +32,6 @@ namespace Bootstrap.DataAccess.SQLite
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
transaction.CommitTransaction();
CacheCleanUtility.ClearCache(menuIds: value);
ret = true;
}
catch (Exception ex)
@ -44,6 +43,7 @@ namespace Bootstrap.DataAccess.SQLite
}
return ret;
}
/// <summary>
/// <summary>
/// 通过角色ID保存当前授权菜单
@ -71,7 +71,6 @@ namespace Bootstrap.DataAccess.SQLite
});
transaction.CommitTransaction();
}
CacheCleanUtility.ClearCache(menuIds: menuIds, roleIds: new List<string>() { roleId });
ret = true;
}
catch (Exception ex)

View File

@ -16,41 +16,63 @@ namespace Bootstrap.DataAccess
///
/// </summary>
public const string RetrieveMenusByRoleIdDataKey = "MenuHelper-RetrieveMenusByRoleId";
/// <summary>
///
/// </summary>
public const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus";
/// <summary>
///
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveMenu(BootstrapMenu p)
{
var ret = DbAdapterManager.Create<Menu>().SaveMenu(p);
if (ret) CacheCleanUtility.ClearCache(menuIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string>() { p.Id });
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool SaveMenu(BootstrapMenu value) => DbAdapterManager.Create<Menu>().SaveMenu(value);
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool DeleteMenu(IEnumerable<string> value) => DbAdapterManager.Create<Menu>().DeleteMenu(value);
public static bool DeleteMenu(IEnumerable<string> value)
{
var ret = DbAdapterManager.Create<Menu>().DeleteMenu(value);
if (ret) CacheCleanUtility.ClearCache(menuIds: value);
return ret;
}
/// <summary>
/// 通过用户名获得所有菜单
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveMenusByUserName(string userName) => RetrieveAllMenus(userName);
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveMenusByRoleId(string roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbAdapterManager.Create<Menu>().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey);
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="value"></param>
/// <param name="roleId"></param>
/// <param name="menuIds"></param>
/// <returns></returns>
public static bool SaveMenusByRoleId(string id, IEnumerable<string> value) => DbAdapterManager.Create<Menu>().SaveMenusByRoleId(id, value);
public static bool SaveMenusByRoleId(string roleId, IEnumerable<string> menuIds)
{
var ret = DbAdapterManager.Create<Menu>().SaveMenusByRoleId(roleId, menuIds);
if (ret) CacheCleanUtility.ClearCache(menuIds: menuIds, roleIds: new List<string>() { roleId });
return ret;
}
/// <summary>
///
/// </summary>
@ -66,6 +88,7 @@ namespace Bootstrap.DataAccess
DbHelper.CascadeMenus(menus, root);
return root;
}
/// <summary>
/// 通过当前用户名获得后台菜单,层次化后集合
/// </summary>
@ -82,6 +105,7 @@ namespace Bootstrap.DataAccess
DbHelper.CascadeMenus(menus, root);
return root;
}
/// <summary>
/// 通过当前用户名获得所有菜单,层次化后集合
/// </summary>
@ -94,6 +118,7 @@ namespace Bootstrap.DataAccess
DbHelper.CascadeMenus(menus, root);
return root;
}
/// <summary>
/// 通过用户获得所有菜单
/// </summary>

View File

@ -28,7 +28,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ids", ids));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1;
}
CacheCleanUtility.ClearCache(menuIds: value);
return ret;
}
/// <summary>
@ -60,7 +59,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ApplicationCode", p.ApplicationCode));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
}
CacheCleanUtility.ClearCache(menuIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string>() { p.Id });
return ret;
}
/// <summary>
@ -120,7 +118,6 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
CacheCleanUtility.ClearCache(menuIds: menuIds, roleIds: new List<string>() { roleId });
ret = true;
}
catch (Exception ex)