更改RetrieveDicts接口参数为int
This commit is contained in:
parent
cb3b178059
commit
be81d04f33
|
@ -1,40 +1,40 @@
|
|||
using Bootstrap.DataAccess;
|
||||
using Longbow.Web.Mvc;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bootstrap.Admin.Models
|
||||
{
|
||||
public class QueryDictOption : PaginationOption
|
||||
{
|
||||
/// <summary>
|
||||
/// 字典名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
/// <summary>
|
||||
/// 字典种类
|
||||
/// </summary>
|
||||
public string Category { get; set; }
|
||||
/// <summary>
|
||||
/// 字典表查询
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public QueryData<Dict> RetrieveData()
|
||||
{
|
||||
var data = DictHelper.RetrieveDicts(string.Empty);
|
||||
if (!string.IsNullOrEmpty(Name))
|
||||
{
|
||||
data = data.Where(t => t.Name.Contains(Name));
|
||||
}
|
||||
if (!string.IsNullOrEmpty(Category))
|
||||
{
|
||||
data = data.Where(t => t.Category.Contains(Category));
|
||||
}
|
||||
var ret = new QueryData<Dict>();
|
||||
ret.total = data.Count();
|
||||
// 通过option.Sort属性判断对那列进行排序,现在对字典表Category列排序
|
||||
data = Order == "asc" ? data.OrderBy(t => t.Category) : data.OrderByDescending(t => t.Category);
|
||||
ret.rows = data.Skip(Offset).Take(Limit);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
using Bootstrap.DataAccess;
|
||||
using Longbow.Web.Mvc;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bootstrap.Admin.Models
|
||||
{
|
||||
public class QueryDictOption : PaginationOption
|
||||
{
|
||||
/// <summary>
|
||||
/// 字典名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
/// <summary>
|
||||
/// 字典种类
|
||||
/// </summary>
|
||||
public string Category { get; set; }
|
||||
/// <summary>
|
||||
/// 字典表查询
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public QueryData<Dict> RetrieveData()
|
||||
{
|
||||
var data = DictHelper.RetrieveDicts();
|
||||
if (!string.IsNullOrEmpty(Name))
|
||||
{
|
||||
data = data.Where(t => t.Name.Contains(Name));
|
||||
}
|
||||
if (!string.IsNullOrEmpty(Category))
|
||||
{
|
||||
data = data.Where(t => t.Category.Contains(Category));
|
||||
}
|
||||
var ret = new QueryData<Dict>();
|
||||
ret.total = data.Count();
|
||||
// 通过option.Sort属性判断对那列进行排序,现在对字典表Category列排序
|
||||
data = Order == "asc" ? data.OrderBy(t => t.Category) : data.OrderByDescending(t => t.Category);
|
||||
ret.rows = data.Skip(Offset).Take(Limit);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,122 +1,122 @@
|
|||
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 class DictHelper
|
||||
{
|
||||
private const string DictDataKey = "DictData-CodeDictHelper";
|
||||
|
||||
/// <summary>
|
||||
/// 查询所有字典信息
|
||||
/// </summary>
|
||||
/// <param name="tId"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Dict> RetrieveDicts(string tId = null)
|
||||
{
|
||||
string sql = "select * from Dicts";
|
||||
var ret = CacheManager.GetOrAdd(DictDataKey, CacheSection.RetrieveIntervalByKey(DictDataKey), key =>
|
||||
{
|
||||
List<Dict> Dicts = new List<Dict>();
|
||||
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
||||
try
|
||||
{
|
||||
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
Dicts.Add(new Dict()
|
||||
{
|
||||
ID = (int)reader[0],
|
||||
Category = (string)reader[1],
|
||||
Name = (string)reader[2],
|
||||
Code = (string)reader[3],
|
||||
Define = (int)reader[4]
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
||||
return Dicts;
|
||||
}, CacheSection.RetrieveDescByKey(DictDataKey));
|
||||
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除字典中的数据
|
||||
/// </summary>
|
||||
/// <param name="ids">需要删除的IDs</param>
|
||||
/// <returns></returns>
|
||||
public static bool DeleteDict(string ids)
|
||||
{
|
||||
var ret = false;
|
||||
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret;
|
||||
try
|
||||
{
|
||||
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Dicts 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 SaveDict(Dict p)
|
||||
{
|
||||
if (p == null) throw new ArgumentNullException("p");
|
||||
bool ret = false;
|
||||
if (p.Category.Length > 50) p.Category.Substring(0, 50);
|
||||
if (p.Name.Length > 50) p.Name.Substring(0, 50);
|
||||
if (p.Code.Length > 50) p.Code.Substring(0, 50);
|
||||
string sql = p.ID == 0 ?
|
||||
"Insert Into Dicts (Category, Name, Code ,Define) Values (@Category, @Name, @Code, @Define)" :
|
||||
"Update Dicts set Category = @Category, Name = @Name, @Code = Code ,@Define = Define 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("@Category", p.Category, ParameterDirection.Input));
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name, ParameterDirection.Input));
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Code", p.Code, ParameterDirection.Input));
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Define", p.Define, ParameterDirection.Input));
|
||||
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
||||
}
|
||||
ret = true;
|
||||
ClearCache();
|
||||
}
|
||||
catch (DbException ex)
|
||||
{
|
||||
ExceptionManager.Publish(ex);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新缓存
|
||||
/// </summary>
|
||||
private static void ClearCache()
|
||||
{
|
||||
CacheManager.Clear(key => key == DictDataKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
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 class DictHelper
|
||||
{
|
||||
private const string DictDataKey = "DictData-CodeDictHelper";
|
||||
|
||||
/// <summary>
|
||||
/// 查询所有字典信息
|
||||
/// </summary>
|
||||
/// <param name="tId"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Dict> RetrieveDicts(int id = 0)
|
||||
{
|
||||
var ret = CacheManager.GetOrAdd(DictDataKey, CacheSection.RetrieveIntervalByKey(DictDataKey), key =>
|
||||
{
|
||||
string sql = "select * from Dicts";
|
||||
List<Dict> Dicts = new List<Dict>();
|
||||
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
||||
try
|
||||
{
|
||||
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
Dicts.Add(new Dict()
|
||||
{
|
||||
ID = (int)reader[0],
|
||||
Category = (string)reader[1],
|
||||
Name = (string)reader[2],
|
||||
Code = (string)reader[3],
|
||||
Define = (int)reader[4]
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
||||
return Dicts;
|
||||
}, CacheSection.RetrieveDescByKey(DictDataKey));
|
||||
return id == 0 ? ret : ret.Where(t => id == t.ID);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除字典中的数据
|
||||
/// </summary>
|
||||
/// <param name="ids">需要删除的IDs</param>
|
||||
/// <returns></returns>
|
||||
public static bool DeleteDict(string ids)
|
||||
{
|
||||
var ret = false;
|
||||
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret;
|
||||
try
|
||||
{
|
||||
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Dicts 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 SaveDict(Dict p)
|
||||
{
|
||||
if (p == null) throw new ArgumentNullException("p");
|
||||
bool ret = false;
|
||||
if (p.Category.Length > 50) p.Category.Substring(0, 50);
|
||||
if (p.Name.Length > 50) p.Name.Substring(0, 50);
|
||||
if (p.Code.Length > 50) p.Code.Substring(0, 50);
|
||||
string sql = p.ID == 0 ?
|
||||
"Insert Into Dicts (Category, Name, Code ,Define) Values (@Category, @Name, @Code, @Define)" :
|
||||
"Update Dicts set Category = @Category, Name = @Name, @Code = Code ,@Define = Define 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("@Category", p.Category, ParameterDirection.Input));
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name, ParameterDirection.Input));
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Code", p.Code, ParameterDirection.Input));
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Define", p.Define, ParameterDirection.Input));
|
||||
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
||||
}
|
||||
ret = true;
|
||||
ClearCache();
|
||||
}
|
||||
catch (DbException ex)
|
||||
{
|
||||
ExceptionManager.Publish(ex);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新缓存
|
||||
/// </summary>
|
||||
private static void ClearCache()
|
||||
{
|
||||
CacheManager.Clear(key => key == DictDataKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,62 +1,62 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bootstrap.DataAccess.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class DictHelperTests
|
||||
{
|
||||
|
||||
private Dict Dict { get; set; }
|
||||
|
||||
[TestInitialize]
|
||||
public void Initialized()
|
||||
{
|
||||
Dict = new Dict() { Category = "__测试菜单__", Name = "__测试子菜单1__", Code = "2",Define = 0 };
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, "Delete from Dicts where Category = '__测试菜单__'"))
|
||||
{
|
||||
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RetrieveDictsTest()
|
||||
{
|
||||
Assert.IsTrue(DictHelper.RetrieveDicts().Count() > 1, "不带参数的DictHelper.RetrieveDicts方法调用失败");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SaveDictTest()
|
||||
{
|
||||
// 测试插入字典记录方法 ID = 0
|
||||
Assert.IsTrue(DictHelper.SaveDict(Dict), "插入字典记录操作失败,请检查 DictHelper.SaveDict 方法");
|
||||
var dicts = DictHelper.RetrieveDicts();
|
||||
Assert.IsTrue(dicts.Count() > 0, "插入字典记录操作失败,请检查 DictHelper.SaveDict 方法");
|
||||
|
||||
// 测试更新字典记录方法 ID != 0
|
||||
var dict = dicts.FirstOrDefault(d => d.Category == Dict.Category);
|
||||
dict.Name = "__测试子菜单2__";
|
||||
Assert.IsTrue(DictHelper.SaveDict(dict), string.Format("更新字典记录ID = {0} 操作失败,请检查 DictHelper.SaveDict 方法", dict.ID));
|
||||
var dest = DictHelper.RetrieveDicts(dict.ID.ToString());
|
||||
Assert.IsTrue(dest.Count() == 1, "带参数的DictHelper.RetrieveDicts方法调用失败");
|
||||
Assert.AreEqual(dict.Name, dest.First().Name, string.Format("更新字典记录ID = {0} 操作失败,请检查 DictHelper.SaveDict 方法", dict.ID));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeleteDictTest()
|
||||
{
|
||||
// 先判断数据环境是否可以删除,没有数据先伪造数据
|
||||
var dict = DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == Dict.Category);
|
||||
if (dict == null) DictHelper.SaveDict(Dict);
|
||||
dict = DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == Dict.Category);
|
||||
Assert.IsTrue(DictHelper.DeleteDict(dict.ID.ToString()), "DictHelper.DeleteDict 方法调用失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bootstrap.DataAccess.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class DictHelperTests
|
||||
{
|
||||
|
||||
private Dict Dict { get; set; }
|
||||
|
||||
[TestInitialize]
|
||||
public void Initialized()
|
||||
{
|
||||
Dict = new Dict() { Category = "__测试菜单__", Name = "__测试子菜单1__", Code = "2",Define = 0 };
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, "Delete from Dicts where Category = '__测试菜单__'"))
|
||||
{
|
||||
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RetrieveDictsTest()
|
||||
{
|
||||
Assert.IsTrue(DictHelper.RetrieveDicts().Count() > 1, "不带参数的DictHelper.RetrieveDicts方法调用失败");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SaveDictTest()
|
||||
{
|
||||
// 测试插入字典记录方法 ID = 0
|
||||
Assert.IsTrue(DictHelper.SaveDict(Dict), "插入字典记录操作失败,请检查 DictHelper.SaveDict 方法");
|
||||
var dicts = DictHelper.RetrieveDicts();
|
||||
Assert.IsTrue(dicts.Count() > 0, "插入字典记录操作失败,请检查 DictHelper.SaveDict 方法");
|
||||
|
||||
// 测试更新字典记录方法 ID != 0
|
||||
var dict = dicts.FirstOrDefault(d => d.Category == Dict.Category);
|
||||
dict.Name = "__测试子菜单2__";
|
||||
Assert.IsTrue(DictHelper.SaveDict(dict), string.Format("更新字典记录ID = {0} 操作失败,请检查 DictHelper.SaveDict 方法", dict.ID));
|
||||
var dest = DictHelper.RetrieveDicts(dict.ID);
|
||||
Assert.IsTrue(dest.Count() == 1, "带参数的DictHelper.RetrieveDicts方法调用失败");
|
||||
Assert.AreEqual(dict.Name, dest.First().Name, string.Format("更新字典记录ID = {0} 操作失败,请检查 DictHelper.SaveDict 方法", dict.ID));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeleteDictTest()
|
||||
{
|
||||
// 先判断数据环境是否可以删除,没有数据先伪造数据
|
||||
var dict = DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == Dict.Category);
|
||||
if (dict == null) DictHelper.SaveDict(Dict);
|
||||
dict = DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == Dict.Category);
|
||||
Assert.IsTrue(DictHelper.DeleteDict(dict.ID.ToString()), "DictHelper.DeleteDict 方法调用失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue