实现了菜单的增删改查功能

This commit is contained in:
sunnian 2016-10-25 17:07:24 +08:00
parent 23ffb08004
commit f38ffa51be
6 changed files with 205 additions and 3 deletions

View File

@ -28,6 +28,7 @@
<add key="UserData-CodeUserHelper" interval="600" desc="用户信息缓存" /> <add key="UserData-CodeUserHelper" interval="600" desc="用户信息缓存" />
<add key="UserData-CodeUserHelper-" interval="600" desc="用户信息缓存" /> <add key="UserData-CodeUserHelper-" interval="600" desc="用户信息缓存" />
<add key="RoleData-CodeRoleHelper" interval="600" desc="角色信息缓存" /> <add key="RoleData-CodeRoleHelper" interval="600" desc="角色信息缓存" />
<add key="MenuData-CodeMenuHelper" interval="600" desc="菜单信息缓存" />
</cacheManager> </cacheManager>
<cacheManagerList> <cacheManagerList>

View File

@ -60,6 +60,7 @@
<Compile Include="Group.cs" /> <Compile Include="Group.cs" />
<Compile Include="GroupHelper.cs" /> <Compile Include="GroupHelper.cs" />
<Compile Include="Menu.cs" /> <Compile Include="Menu.cs" />
<Compile Include="MenuHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Role.cs" /> <Compile Include="Role.cs" />
<Compile Include="RoleHelper.cs" /> <Compile Include="RoleHelper.cs" />

View File

@ -8,18 +8,34 @@ namespace Bootstrap.DataAccess
public class Menu public class Menu
{ {
/// <summary> /// <summary>
/// /// 获得/设置 菜单主键ID
/// </summary>
public int ID { set; get; }
/// <summary>
/// 获得/设置 父级菜单ID
/// </summary>
public int ParentId { set; get; }
/// <summary>
/// 获得/设置 菜单名称
/// </summary> /// </summary>
public string Name { get; set; } public string Name { get; set; }
/// <summary> /// <summary>
/// /// 获得/设置 菜单序号
/// </summary>
public int Order { get; set; }
/// <summary>
/// 获得/设置 菜单图标
/// </summary> /// </summary>
public string Icon { get; set; } public string Icon { get; set; }
/// <summary> /// <summary>
/// /// 获得/设置 菜单URL地址
/// </summary> /// </summary>
public string Url { get; set; } public string Url { get; set; }
/// <summary> /// <summary>
/// 获得/设置 菜单分类
/// </summary>
public string Category { get; set; }
/// <summary>
/// ///
/// </summary> /// </summary>
public string Active { get; set; } public string Active { get; set; }

View File

@ -0,0 +1,121 @@
using Longbow;
using Longbow.Caching;
using Longbow.Caching.Configuration;
using Longbow.ExceptionManagement;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Linq;
namespace Bootstrap.DataAccess
{
public static class MenuHelper
{
private const string MenuDataKey = "MenuData-CodeMenuHelper";
/// <summary>
/// 查询所有菜单信息
/// </summary>
/// <param name="tId"></param>
/// <returns></returns>
public static IEnumerable<Menu> RetrieveMenus(string tId = null)
{
string sql = "select * from Navigations";
var ret = CacheManager.GetOrAdd(MenuDataKey, CacheSection.RetrieveIntervalByKey(MenuDataKey), key =>
{
List<Menu> Menus = new List<Menu>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
try
{
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
while (reader.Read())
{
Menus.Add(new Menu()
{
ID = (int)reader[0],
ParentId = (int)reader[1],
Name = (string)reader[2],
Order = LgbConvert.ReadValue((int)reader[3],0),
Icon = (string)reader[4],
Url = LgbConvert.ReadValue((string)reader[5],string.Empty),
Category = (string)reader[6]
});
}
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Menus;
}, CacheSection.RetrieveDescByKey(MenuDataKey));
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
}
/// <summary>
/// 删除菜单信息
/// </summary>
/// <param name="ids"></param>
public static bool DeleteMenu(string ids)
{
bool ret = false;
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret;
try
{
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Navigations where ID in ({0})", ids);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
ClearCache();
ret = true;
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
}
return ret;
}
/// <summary>
/// 保存新建/更新的菜单信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveMenu(Menu p)
{
if (p == null) throw new ArgumentNullException("p");
bool ret = false;
if (p.Name.Length > 50) p.Name.Substring(0, 50);
if (p.Icon.Length > 50) p.Icon.Substring(0, 50);
if (p.Url != null) { if (p.Url.Length > 50) p.Url.Substring(0, 50); }
if (p.Category.Length > 50) p.Category.Substring(0, 50);
string sql = p.ID == 0 ?
"Insert Into Navigations (ParentId, Name, [Order], Icon, Url, Category) Values (@ParentId, @Name, @Order, @Icon, @Url, @Category)" :
"Update Navigations set ParentId = @ParentId, Name = @Name, [Order] = @Order, Icon = @Icon, Url = @Url, Category = @Category where ID = @ID";
try
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.ID, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ParentId", p.ParentId, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Order", p.Order, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Icon", p.Icon, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Url", p.Url, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
ret = true;
ClearCache();
}
catch (DbException ex)
{
ExceptionManager.Publish(ex);
}
return ret;
}
// 更新缓存
private static void ClearCache()
{
CacheManager.Clear(key => key == MenuDataKey);
}
}
}

View File

@ -55,6 +55,7 @@
<Compile Include="RoleHelperTests.cs" /> <Compile Include="RoleHelperTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="GroupHelperTests.cs" /> <Compile Include="GroupHelperTests.cs" />
<Compile Include="MenuHelperTests.cs" />
<Compile Include="UserHelperTests.cs" /> <Compile Include="UserHelperTests.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,62 @@
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Bootstrap.DataAccess.Tests
{
[TestClass]
public class MenuHelperTests
{
[TestMethod]
public void RetrieveMenusTest()
{
var result = MenuHelper.RetrieveMenus("1");
Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的MenuHelper.RetrieveMenus方法调用失败请检查数据库连接或者数据库SQL语句");
result = MenuHelper.RetrieveMenus();
Assert.IsTrue(result.Count() >= 0, "不带参数的MenuHelper.RetrieveMenus方法调用失败请检查数据库连接或者数据库SQL语句");
}
[TestMethod]
public void SaveMenuTest()
{
Menu p = new Menu();
p.ParentId = 1;
p.Name = "测试菜单名称";
p.Order = 0;
p.Icon = "测试菜单Icon";
p.Url = "urlTestAdd";
p.Category = "测试菜单分组";
var result = MenuHelper.SaveMenu(p);
Assert.IsTrue(result, "增加菜单出错");
Menu p1 = new Menu();
p1.ID = 7;
p1.ParentId = 2;
p1.Name = "测试菜单名称1";
p1.Order = 0;
p1.Icon = "测试菜单Icon1";
p1.Url = "urlTestUpdate";
p1.Category = "测试菜单分组1";
result = MenuHelper.SaveMenu(p1);
Assert.IsTrue(result, "更新菜单出错");
}
[TestMethod]
public void DeleteMenuTest()
{
MenuHelper.SaveMenu(new Menu()
{
ID = 0,
ParentId = 1,
Name = "菜单删除测试",
Order = 0,
Icon = "测试菜单Icon1",
Url = "urlTestUpdate",
Category = "测试菜单分组1"
});
var menu = MenuHelper.RetrieveMenus().FirstOrDefault(m => m.Name == "菜单删除测试");
Assert.IsTrue(MenuHelper.DeleteMenu(menu.ID.ToString()),"删除菜单失败");
Assert.IsTrue(MenuHelper.DeleteMenu("1,2"), "带有参数的MenuHelper.DeleteMenu方法调用失败请检查数据库连接或者数据库SQL语句");
Assert.IsFalse(MenuHelper.DeleteMenu(string.Empty), "参数为空字符串的MenuHelper.DeleteMenu方法调用失败请检查数据库连接或者数据库SQL语句");
}
}
}