From 6abbaa60fd15f9be37df26152510760627fcdea0 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Tue, 25 Oct 2016 18:47:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E8=A7=92=E8=89=B2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/Bootstrap.Admin.csproj | 1 + Bootstrap.Admin/Content/css/site.css | 9 + Bootstrap.Admin/Content/js/Longbow.Common.js | 11 + Bootstrap.Admin/Content/js/framework.js | 113 ++++++-- .../Controllers/RolesController.cs | 15 +- Bootstrap.Admin/Models/QueryRoleOption.cs | 2 +- Bootstrap.Admin/Scripts/Users.js | 29 ++ Bootstrap.Admin/Views/Admin/Users.cshtml | 13 +- .../Views/Shared/RoleConfig.cshtml | 18 ++ Bootstrap.Admin/Views/Shared/_Admin.cshtml | 1 + Bootstrap.Admin/Views/Shared/_Default.cshtml | 4 + Bootstrap.DataAccess/RoleHelper.cs | 250 ++++++++++-------- Bootstrap.DataAccessTests/RoleHelperTests.cs | 98 +++---- 13 files changed, 381 insertions(+), 183 deletions(-) create mode 100644 Bootstrap.Admin/Views/Shared/RoleConfig.cshtml diff --git a/Bootstrap.Admin/Bootstrap.Admin.csproj b/Bootstrap.Admin/Bootstrap.Admin.csproj index aac95394..066b8244 100644 --- a/Bootstrap.Admin/Bootstrap.Admin.csproj +++ b/Bootstrap.Admin/Bootstrap.Admin.csproj @@ -221,6 +221,7 @@ + Web.config diff --git a/Bootstrap.Admin/Content/css/site.css b/Bootstrap.Admin/Content/css/site.css index a2249fba..105c0943 100644 --- a/Bootstrap.Admin/Content/css/site.css +++ b/Bootstrap.Admin/Content/css/site.css @@ -18,6 +18,10 @@ ul li { outline: none; } + .btn span.fa, .btn span.glyphicon { + margin-right: 6px; + } + a, a:hover, a:focus { text-decoration: none; outline: none; @@ -433,6 +437,11 @@ a.logo { clear: both; } + .modal-footer .btn { + padding-left: 22px; + padding-right: 22px; + } + .fixed-table-loading { padding-top: 5px; } diff --git a/Bootstrap.Admin/Content/js/Longbow.Common.js b/Bootstrap.Admin/Content/js/Longbow.Common.js index 39ea7890..44004b4b 100644 --- a/Bootstrap.Admin/Content/js/Longbow.Common.js +++ b/Bootstrap.Admin/Content/js/Longbow.Common.js @@ -93,6 +93,17 @@ } }); + // enhance window.console.log + if (!window.console) { + window.console = { + log: function () { + + } + }; + } + window.console = window.console || {}; + console.log || (console.log = opera.postError); + $.fn.extend({ autoValidate: function (options) { // validate diff --git a/Bootstrap.Admin/Content/js/framework.js b/Bootstrap.Admin/Content/js/framework.js index dcc548cd..e19bb468 100644 --- a/Bootstrap.Admin/Content/js/framework.js +++ b/Bootstrap.Admin/Content/js/framework.js @@ -1,6 +1,7 @@ (function ($) { BootstrapAdmin = function (options) { var that = this; + if (options.click !== undefined && options.click.constructor === Object) { options.click = $.extend({}, BootstrapAdmin.settings.click, options.click); } this.options = $.extend({}, BootstrapAdmin.settings, options); this.dataEntity = options.dataEntity; @@ -10,12 +11,21 @@ // handler click event for (name in this.options.click) { - var source = $("#" + this.options.click[name]); - source.data('click', name); - $("#" + this.options.click[name]).click(function () { - var method = $(this).data('click'); - BootstrapAdmin.prototype[method].apply(that); - }); + var ele = this.options.click[name]; + var cId = ele; + var event = null; + if ($.isArray(ele)) { + for (index in ele) { + if (ele[index].id === undefined) { + window.console.log('options.click.assign[{0}].{1}.id 未设置控件id', index, name); + continue; + } + cId = ele[index]['id']; + event = ele[index]['click']; + handler(cId, event); + } + } + else handler(cId, event); } // handler modal window show event @@ -28,6 +38,16 @@ } }); } + + function handler(cid, event) { + var source = $("#" + cId); + source.data('click', name); + if (event !== null) source.data('event', event); + source.click(function () { + var method = $(this).data('click'); + BootstrapAdmin.prototype[method].call(that, this, $(this).data('event')); + }); + } }; BootstrapAdmin.VERSION = "1.0"; @@ -44,7 +64,8 @@ create: 'btn_add', edit: 'btn_edit', del: 'btn_delete', - save: 'btnSubmit' + save: 'btnSubmit', + assign: [] } }; @@ -56,14 +77,15 @@ constructor: BootstrapAdmin, idEvents: function () { var op = { - dataEntity: this.options.dataEntity, + dataEntity: $.extend({}, this.options.dataEntity), table: this.options.bootstrapTable, modal: this.options.modal }; return { 'click .edit': function (e, value, row, index) { op.dataEntity.load(row); - $('#' + op.table).bootstrapTable('uncheckAll').bootstrapTable('check', index); + $(op.table).bootstrapTable('uncheckAll'); + $(op.table).bootstrapTable('check', index); $('#' + op.modal).modal("show"); } } @@ -74,23 +96,23 @@ }, create: function () { - this.dataEntity.reset(); + if (this.dataEntity instanceof DataEntity) this.dataEntity.reset(); if (this.options.modal.constructor === String) $('#' + this.options.modal).modal("show"); if (this.options.bootstrapTable.constructor === String) $(this.options.bootstrapTable).bootstrapTable('uncheckAll'); }, edit: function () { - options = this.options; + var options = this.options; if (options.bootstrapTable.constructor !== String) return; var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections'); if (arrselections.length == 0) { - swal('请选择要编辑的条目', "编辑操作", "warning"); + swal('请选择要编辑的数据', "编辑操作", "warning"); } else if (arrselections.length > 1) { - swal('请选择一个要编辑的条目', "编辑操作", "warning"); + swal('请选择一个要编辑的数据', "编辑操作", "warning"); } else { - this.dataEntity.load(arrselections[0]); + if (this.dataEntity instanceof DataEntity) this.dataEntity.load(arrselections[0]); if (options.modal.constructor === String) $('#' + options.modal).modal("show"); } }, @@ -100,7 +122,7 @@ if (options.bootstrapTable.constructor !== String) return; var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections'); if (arrselections.length == 0) { - swal('请选择要删除的条目', "删除操作", "warning"); + swal('请选择要删除的数据', "删除操作", "warning"); return; } else { @@ -134,7 +156,8 @@ }, save: function () { - var options = $.extend({}, this.options, { data: this.dataEntity.get() }); + var options = $.extend({ data: {} }, this.options); + if (this.dataEntity instanceof DataEntity) options = $.extend(options, { data: this.dataEntity.get() }); if (options.validateForm.constructor === String && !$("#" + options.validateForm).valid()) return; $.ajax({ url: options.url, @@ -170,6 +193,62 @@ swal("失败", "保存数据失败", "error"); } }); - } + }, + + assign: function (eventSrc, callback) { + var options = this.options; + if (options.bootstrapTable.constructor !== String) return; + var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections'); + if (arrselections.length == 0) { + swal('请选择要编辑的数据', "编辑操作", "warning"); + } + else if (arrselections.length > 1) { + swal('请选择一个要编辑的数据', "编辑操作", "warning"); + } + else { + if ($.isFunction(callback)) { + callback.call(eventSrc, arrselections[0]); + } + } + }, + + }; + + Role = {}; + Role.getRolesByUserId = function (userId, callback) { + $.ajax({ + url: '../api/Roles/' + userId, + type: 'GET', + success: function (result) { + callback(result); + }, + error: function (XMLHttpRequest, textStatus, errorThrown) { + callback(); + } + }); + }; + Role.getRolesByGroupId = function (groupId) { + + }; + Role.saveRolesByUserId = function (userId, roleIds, callback) { + $.ajax({ + url: '../api/Roles/' + userId, + data: { "": roleIds }, + type: 'PUT', + success: function (result) { + callback(result); + }, + error: function (XMLHttpRequest, textStatus, errorThrown) { + callback(); + } + }); + } + + Group = {}; + Group.getGroupsByUserId = function (userId) { + + }; + Group.getGroupsByRoleId = function (roleId) { + }; })(jQuery); \ No newline at end of file diff --git a/Bootstrap.Admin/Controllers/RolesController.cs b/Bootstrap.Admin/Controllers/RolesController.cs index 6a352e6b..cea6f2f4 100644 --- a/Bootstrap.Admin/Controllers/RolesController.cs +++ b/Bootstrap.Admin/Controllers/RolesController.cs @@ -1,5 +1,6 @@ using Bootstrap.Admin.Models; using Bootstrap.DataAccess; +using System.Collections.Generic; using System.Linq; using System.Web.Http; @@ -23,9 +24,19 @@ namespace Bootstrap.Admin.Controllers /// /// [HttpGet] - public Role Get(int id) + public IEnumerable Get(int id) { - return RoleHelper.RetrieveRole().FirstOrDefault(t => t.ID == id); + return RoleHelper.RetrieveRolesByUserId(); + } + /// + /// + /// + /// + /// + [HttpPut] + public bool Put(int id, [FromBody]string value) + { + return RoleHelper.SaveRolesByUserId(id, value); } /// /// diff --git a/Bootstrap.Admin/Models/QueryRoleOption.cs b/Bootstrap.Admin/Models/QueryRoleOption.cs index b7d1606a..dea041db 100644 --- a/Bootstrap.Admin/Models/QueryRoleOption.cs +++ b/Bootstrap.Admin/Models/QueryRoleOption.cs @@ -21,7 +21,7 @@ namespace Bootstrap.Admin.Models public QueryData RetrieveData() { // int limit, int offset, string name, string price, string sort, string order - var data = RoleHelper.RetrieveRole(string.Empty); + var data = RoleHelper.RetrieveRoles(string.Empty); if (!string.IsNullOrEmpty(RoleName)) { data = data.Where(t => t.RoleName.Contains(RoleName)); diff --git a/Bootstrap.Admin/Scripts/Users.js b/Bootstrap.Admin/Scripts/Users.js index b10a221f..60b6d32f 100644 --- a/Bootstrap.Admin/Scripts/Users.js +++ b/Bootstrap.Admin/Scripts/Users.js @@ -9,6 +9,35 @@ DisplayName: "displayName" } }), + click: { + assign: [{ + id: 'btn_assignRole', + click: function (row) { + Role.getRolesByUserId(1, function (roles) { + $("#dialogRole .modal-title").text($.format('{0}-角色授权窗口', row.DisplayName)); + var data = $.map(roles, function (element, index) { + return $.format('
', element.ID, element.RoleName); + }).join(''); + $('#dialogRole form').html(data); + $('#dialogRole').modal('show'); + }); + } + }, { + id: 'btn_assignGroup', + click: function (row) { + var userId = row.ID; + } + }, { + id: 'btnSubmitUserRole', + click: function (row) { + var userId = row.ID; + var roleIds = $('#dialogRole :checked').map(function (index, element) { + return $(element).val(); + }).toArray().join(','); + Role.saveRolesByUserId(userId, roleIds, function () { }); + } + }] + }, success: function (src, data) { if (src === 'save' && data.ID === $('#userId').val()) { $('.username').text(data.DisplayName); diff --git a/Bootstrap.Admin/Views/Admin/Users.cshtml b/Bootstrap.Admin/Views/Admin/Users.cshtml index b1c73f39..e861cd9e 100644 --- a/Bootstrap.Admin/Views/Admin/Users.cshtml +++ b/Bootstrap.Admin/Views/Admin/Users.cshtml @@ -27,6 +27,14 @@ } +@section toolbar { + + +} @section modal { -} \ No newline at end of file +} +@section roleModal { + @Html.Partial("RoleConfig") +} diff --git a/Bootstrap.Admin/Views/Shared/RoleConfig.cshtml b/Bootstrap.Admin/Views/Shared/RoleConfig.cshtml new file mode 100644 index 00000000..475ce5d8 --- /dev/null +++ b/Bootstrap.Admin/Views/Shared/RoleConfig.cshtml @@ -0,0 +1,18 @@ + \ No newline at end of file diff --git a/Bootstrap.Admin/Views/Shared/_Admin.cshtml b/Bootstrap.Admin/Views/Shared/_Admin.cshtml index b068f15a..2b4b99e0 100644 --- a/Bootstrap.Admin/Views/Shared/_Admin.cshtml +++ b/Bootstrap.Admin/Views/Shared/_Admin.cshtml @@ -42,3 +42,4 @@ @RenderSection("modal", false) +@RenderSection("roleModal", false) diff --git a/Bootstrap.Admin/Views/Shared/_Default.cshtml b/Bootstrap.Admin/Views/Shared/_Default.cshtml index a99a53d8..cef2bfbf 100644 --- a/Bootstrap.Admin/Views/Shared/_Default.cshtml +++ b/Bootstrap.Admin/Views/Shared/_Default.cshtml @@ -35,6 +35,9 @@ } +@section roleModal { + @RenderSection("roleModal", false) +}
查询条件
@@ -51,6 +54,7 @@ + @RenderSection("toolbar", false)
diff --git a/Bootstrap.DataAccess/RoleHelper.cs b/Bootstrap.DataAccess/RoleHelper.cs index a8983df0..31931267 100644 --- a/Bootstrap.DataAccess/RoleHelper.cs +++ b/Bootstrap.DataAccess/RoleHelper.cs @@ -1,113 +1,137 @@ -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 class RoleHelper - { - private const string RoleDataKey = "RoleData-CodeRoleHelper"; - /// - /// 查询所有角色 - /// - /// - /// - public static IEnumerable RetrieveRole(string tId = null) - { - string sql = "select * from Roles"; - var ret = CacheManager.GetOrAdd(RoleDataKey, CacheSection.RetrieveIntervalByKey(RoleDataKey), key => - { - List roles = new List(); - DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); - try - { - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) - { - while (reader.Read()) - { - roles.Add(new Role() - { - ID = (int)reader[0], - RoleName = LgbConvert.ReadValue(reader[1], string.Empty), - Description = LgbConvert.ReadValue(reader[2], string.Empty) - }); - } - } - } - catch (Exception ex) { ExceptionManager.Publish(ex); } - return roles; - }, CacheSection.RetrieveDescByKey(RoleDataKey)); - return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase)); - } - /// - /// 删除角色表 - /// - /// - public static bool DeleteRole(string IDs) - { - bool ret = false; - if (string.IsNullOrEmpty(IDs) || IDs.Contains("'")) return ret; - try - { - string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Roles 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; - } - - /// - /// 保存新建/更新的角色信息 - /// - /// - /// - public static bool SaveRole(Role p) - { - if (p == null) throw new ArgumentNullException("p"); - bool ret = false; - if (!string.IsNullOrEmpty(p.RoleName) && p.RoleName.Length > 50) p.RoleName = p.RoleName.Substring(0, 50); - if (!string.IsNullOrEmpty(p.Description) && p.Description.Length > 50) p.Description = p.Description.Substring(0, 500); - string sql = p.ID == 0 ? - "Insert Into Roles (RoleName, Description) Values (@RoleName, @Description)" : - "Update Roles set RoleName = @RoleName, Description = @Description 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("@RoleName", p.RoleName, ParameterDirection.Input)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, 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("RoleData-")); - } - } -} +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 class RoleHelper + { + private const string RoleDataKey = "RoleData-CodeRoleHelper"; + /// + /// 查询所有角色 + /// + /// + /// + public static IEnumerable RetrieveRoles(string tId = null) + { + string sql = "select * from Roles"; + var ret = CacheManager.GetOrAdd(RoleDataKey, CacheSection.RetrieveIntervalByKey(RoleDataKey), key => + { + List roles = new List(); + DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); + try + { + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + { + while (reader.Read()) + { + roles.Add(new Role() + { + ID = (int)reader[0], + RoleName = LgbConvert.ReadValue(reader[1], string.Empty), + Description = LgbConvert.ReadValue(reader[2], string.Empty) + }); + } + } + } + catch (Exception ex) { ExceptionManager.Publish(ex); } + return roles; + }, CacheSection.RetrieveDescByKey(RoleDataKey)); + return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase)); + } + /// + /// + /// + /// + /// + /// + public static bool SaveRolesByUserId(int id, string value) + { + //UNDONE: 编写通过用户ID保存当前授权角色的方法 + return true; + } + + /// + /// + /// + /// + public static IEnumerable RetrieveRolesByUserId() + { + //UNDONE: 编写通过用户ID获取所有角色的方法 + return new List() { + new Role() { ID = 1, RoleName = "TestRole1", Description = "测试角色1" }, + new Role() { ID = 2, RoleName = "TestRole2", Description = "测试角色2" } + }; + } + /// + /// 删除角色表 + /// + /// + public static bool DeleteRole(string IDs) + { + bool ret = false; + if (string.IsNullOrEmpty(IDs) || IDs.Contains("'")) return ret; + try + { + string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Roles 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; + } + + /// + /// 保存新建/更新的角色信息 + /// + /// + /// + public static bool SaveRole(Role p) + { + if (p == null) throw new ArgumentNullException("p"); + bool ret = false; + if (!string.IsNullOrEmpty(p.RoleName) && p.RoleName.Length > 50) p.RoleName = p.RoleName.Substring(0, 50); + if (!string.IsNullOrEmpty(p.Description) && p.Description.Length > 50) p.Description = p.Description.Substring(0, 500); + string sql = p.ID == 0 ? + "Insert Into Roles (RoleName, Description) Values (@RoleName, @Description)" : + "Update Roles set RoleName = @RoleName, Description = @Description 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("@RoleName", p.RoleName, ParameterDirection.Input)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, 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("RoleData-")); + } + } +} \ No newline at end of file diff --git a/Bootstrap.DataAccessTests/RoleHelperTests.cs b/Bootstrap.DataAccessTests/RoleHelperTests.cs index 7d114c54..50230fdf 100644 --- a/Bootstrap.DataAccessTests/RoleHelperTests.cs +++ b/Bootstrap.DataAccessTests/RoleHelperTests.cs @@ -1,49 +1,49 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Linq; - -namespace Bootstrap.DataAccess.Tests -{ - [TestClass()] - public class RoleTests - { - [TestMethod()] - public void SaveRoleTest() - { - Role role1 = new Role() - { - RoleName = "管理员", - Description = "可以读写所有内容" - }; - var result1 = RoleHelper.SaveRole(role1); - Assert.IsTrue(result1 == true, "带有参数的RoleHelper.SaveRole方法添加用户失败,请检查数据库连接或者数据库SQL语句"); - Role role2 = new Role() - { - ID = 1, - RoleName = "管理员", - Description = "读写所有内容" - }; - var result2 = RoleHelper.SaveRole(role2); - Assert.IsTrue(result2 == true, "带有参数的RoleHelper.SaveRole方法编辑用户信息失败,请检查数据库连接或者数据库SQL语句"); - } - [TestMethod()] - public void RetrieveRoleTest() - { - var result = RoleHelper.RetrieveRole("1"); - Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句"); - result = RoleHelper.RetrieveRole(); - Assert.IsTrue(result.Count() >= 0, "不带参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句"); - } - [TestMethod()] - public void DeleteRoleTest() - { - RoleHelper.SaveRole(new Role() - { - ID = 0, - RoleName = "RoleUnitTest", - Description = string.Empty - }); - var role = RoleHelper.RetrieveRole().FirstOrDefault(r => r.RoleName == "RoleUnitTest"); - Assert.IsTrue(RoleHelper.DeleteRole(role.ID.ToString()), "删除用户失败"); - } - } -} +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; + +namespace Bootstrap.DataAccess.Tests +{ + [TestClass()] + public class RoleTests + { + [TestMethod()] + public void SaveRoleTest() + { + Role role1 = new Role() + { + RoleName = "管理员", + Description = "可以读写所有内容" + }; + var result1 = RoleHelper.SaveRole(role1); + Assert.IsTrue(result1 == true, "带有参数的RoleHelper.SaveRole方法添加用户失败,请检查数据库连接或者数据库SQL语句"); + Role role2 = new Role() + { + ID = 1, + RoleName = "管理员", + Description = "读写所有内容" + }; + var result2 = RoleHelper.SaveRole(role2); + Assert.IsTrue(result2 == true, "带有参数的RoleHelper.SaveRole方法编辑用户信息失败,请检查数据库连接或者数据库SQL语句"); + } + [TestMethod()] + public void RetrieveRoleTest() + { + var result = RoleHelper.RetrieveRoles("1"); + Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句"); + result = RoleHelper.RetrieveRoles(); + Assert.IsTrue(result.Count() >= 0, "不带参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句"); + } + [TestMethod()] + public void DeleteRoleTest() + { + RoleHelper.SaveRole(new Role() + { + ID = 0, + RoleName = "RoleUnitTest", + Description = string.Empty + }); + var role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == "RoleUnitTest"); + Assert.IsTrue(RoleHelper.DeleteRole(role.ID.ToString()), "删除用户失败"); + } + } +}