新增测试文件,数据库脚本,增加Users及相关功能,新建User.cs和UserHelper.cs,UserHelperTests.cs文件

This commit is contained in:
lq_avenue 2016-10-22 10:38:40 +08:00
parent e5cba6bf50
commit 23be34d753
8 changed files with 196 additions and 0 deletions

View File

@ -23,7 +23,11 @@
<cacheManager>
<add key="TerminalData-CodeTerminalHelper" interval="600" desc="输入口信息缓存" />
<add key="GroupData-CodeGroupHelper" interval="600" desc="群组信息缓存" />
<add key="UserData-CodeUserHelper" interval="600" desc="用户信息缓存" />
</cacheManager>
<cacheManagerList>

View File

@ -63,6 +63,8 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Terminal.cs" />
<Compile Include="TerminalHelper.cs" />
<Compile Include="User.cs" />
<Compile Include="UserHelper.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Longbow.Utility.snk" />

View File

@ -0,0 +1,22 @@
namespace Bootstrap.DataAccess
{
public class User
{
/// <summary>
/// 获得/设置 用户主键ID
/// </summary>
public int ID { get; set; }
/// <summary>
/// 获得/设置 用户名称
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 获取/设置 密码
/// </summary>
public string Password { get; set; }
/// <summary>
/// 获取/设置 验证信息
/// </summary>
public string PassSalt { get; set; }
}
}

View File

@ -0,0 +1,108 @@
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 UserHelper
{
private const string UserDataKey = "UserData-CodeUserHelper";
/// <summary>
/// 查询所有用户
/// </summary>
/// <param name="pIds"></param>
/// <returns></returns>
public static IEnumerable<User> RetrieveUsers(string tId = null)
{
string sql = "select * from Users";
var ret = CacheManager.GetOrAdd(UserDataKey, CacheSection.RetrieveIntervalByKey(UserDataKey), key =>
{
List<User> Users = new List<User>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
try
{
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
while (reader.Read())
{
Users.Add(new User()
{
ID = (int)reader[0],
UserName = (string)reader[1],
Password = (string)reader[2],
PassSalt = (string)reader[3],
});
}
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Users;
}, CacheSection.RetrieveDescByKey(UserDataKey));
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
}
/// <summary>
/// 删除用户
/// </summary>
/// <param name="ids"></param>
public static void DeleteUser(string ids)
{
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return;
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Users where ID in ({0})", ids);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
ClearCache();
}
}
/// <summary>
/// 保存新建/更新的用户信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveUser(User p)
{
if (p == null) throw new ArgumentNullException("p");
bool ret = false;
if (p.UserName.Length > 50) p.UserName.Substring(0, 50);
if (p.Password.Length > 50) p.Password.Substring(0, 50);
if (p.PassSalt.Length > 50) p.PassSalt.Substring(0, 50);
string sql = p.ID == 0 ?
"Insert Into Users (UserName, Password, PassSalt) Values (@UserName, @Password, @PassSalt)" :
"Update Users set UserName = @UserName, Password = @Password, PassSalt = @PassSalt 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("@UserName", p.UserName, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Password", p.Password, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@PassSalt", p.PassSalt, 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.Contains("TerminalData-"));
}
}
}

View File

@ -54,7 +54,11 @@
<ItemGroup>
<Compile Include="TerminalHelperTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="GroupHelperTests.cs" />
<Compile Include="UserHelperTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Bootstrap.DataAccess\Bootstrap.DataAccess.csproj">

View File

@ -0,0 +1,55 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
namespace Bootstrap.DataAccess.Tests
{
[TestClass]
public class UserHelperTests
{
[TestMethod]
public void RetrieveUsersTest()
{
var result = UserHelper.RetrieveUsers("1");
Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的UserHelper.RetrieveUsers方法调用失败请检查数据库连接或者数据库SQL语句");
result = UserHelper.RetrieveUsers();
Assert.IsTrue(result.Count() >= 0, "不带参数的UserHelper.RetrieveUsers方法调用失败请检查数据库连接或者数据库SQL语句");
}
[TestMethod]
public void DeleteUserTest()
{
string ids = "2";
try
{
UserHelper.DeleteUser(ids);
Assert.IsTrue(true);
}
catch
{
Assert.IsTrue(false,"带有参数的UserHelper.DeleteUserTest方法调用失败请检查数据库连接或者数据库SQL语句");
}
}
[TestMethod]
public void SaveUserTest()
{
User users = new User();
users.ID = 0;
users.UserName = "liqi";
users.Password = "123";
users.PassSalt = "123";
var result = UserHelper.SaveUser(users);
Assert.IsTrue(result == true, "带有参数的UserHelper.SaveUser方法中新建用户信息失败请检查数据库连接或者数据库SQL语句");
User users1 = new User();
users1.ID = 1;
users1.UserName = "Lily";
users1.Password = "123456";
users1.PassSalt = "123456";
result = UserHelper.SaveUser(users1);
Assert.IsTrue(result == true, "带有参数的UserHelper.SaveUser方法中更新用户信息失败请检查数据库连接或者数据库SQL语句");
}
}
}

View File

@ -7,6 +7,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bootstrap.DataAccess", "Boo
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sql", "sql", "{87319AF5-7C40-4362-B67C-35F9DD737DB4}"
ProjectSection(SolutionItems) = preProject
DatabaseScripts\ExtendedChecker.sql = DatabaseScripts\ExtendedChecker.sql
DatabaseScripts\Install.sql = DatabaseScripts\Install.sql
DatabaseScripts\Proc_StartTerminal.sql = DatabaseScripts\Proc_StartTerminal.sql
DatabaseScripts\Proc_StopTerminal.sql = DatabaseScripts\Proc_StopTerminal.sql

Binary file not shown.