feat: 添加 Group 服务

This commit is contained in:
zhangpeihang 2022-04-29 15:09:14 +08:00
parent 203b3fd37f
commit 32eb9734cd
3 changed files with 115 additions and 1 deletions

View File

@ -35,7 +35,7 @@ public static class ServiceCollectionExtensions
services.AddSingleton<IApp, AppService>();
services.AddSingleton<IDict, DictService>();
//services.AddSingleton<IException, ExceptionService>();
//services.AddSingleton<IGroup, GroupService>();
services.AddSingleton<IGroup, GroupService>();
services.AddSingleton<ILogin, LoginService>();
services.AddSingleton<INavigation, NavigationService>();
//services.AddSingleton<IRole, RoleService>();

View File

@ -0,0 +1,107 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone
using BootStarpAdmin.DataAccess.SqlSugar.Models;
using BootstrapAdmin.Caching;
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
using SqlSugar;
namespace BootstrapAdmin.DataAccess.SqlSugar.Services;
class GroupService : IGroup
{
private const string GroupServiceGetAllCacheKey = "GroupService-GetAll";
private const string GroupServiceGetGroupsByUserIdCacheKey = "GroupService-GetGroupsByUserId";
private const string GroupServiceGetGroupsByRoleIdCacheKey = "GroupService-GetGroupsByRoleId";
private ISqlSugarClient Client { get; }
/// <summary>
///
/// </summary>
/// <param name="client"></param>
public GroupService(ISqlSugarClient client) => Client = client;
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<Group> GetAll() => CacheManager.GetOrAdd(GroupServiceGetAllCacheKey, entry => Client.Queryable<Group>().AS("Groups").ToList());
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public List<string> GetGroupsByUserId(string? userId) => CacheManager.GetOrAdd($"{GroupServiceGetGroupsByUserIdCacheKey}-{userId}", entry => Client.Ado.SqlQuery<string>("select GroupID from UserGroup where UserID = @UserID", new { UserID = userId }));
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <param name="groupIds"></param>
/// <returns></returns>
public bool SaveGroupsByUserId(string? userId, IEnumerable<string> groupIds)
{
var ret = false;
try
{
Client.Ado.BeginTran();
Client.Ado.ExecuteCommand("delete from UserGroup where UserID = @UserID", new { UserID = userId });
Client.Insertable<UserGroup>(groupIds.Select(g => new { GroupID = g, UserID = userId })).ExecuteCommand();
Client.Ado.CommitTran();
ret = true;
}
catch (Exception)
{
Client.Ado.RollbackTran();
throw;
}
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public List<string> GetGroupsByRoleId(string? roleId) => CacheManager.GetOrAdd($"{GroupServiceGetGroupsByRoleIdCacheKey}-{roleId}", entry => Client.Ado.SqlQuery<string>("select GroupID from RoleGroup where RoleID = @RoleID", new { RoleID = roleId }));
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <param name="groupIds"></param>
/// <returns></returns>
public bool SaveGroupsByRoleId(string? roleId, IEnumerable<string> groupIds)
{
var ret = false;
try
{
Client.Ado.BeginTran();
Client.Ado.ExecuteCommand("delete from RoleGroup where RoleID = @RoleID", new { RoleID = roleId });
Client.Insertable<RoleGroup>(groupIds.Select(g => new { GroupID = g, RoleID = roleId }));
Client.Ado.CommitTran();
ret = true;
}
catch (Exception)
{
Client.Ado.RollbackTran();
throw;
}
if (ret)
{
CacheManager.Clear();
}
return ret;
}
}

View File

@ -89,6 +89,13 @@ namespace Microsoft.Extensions.DependencyInjection
config.DbType = SqlSugar.DbType.Sqlite;
config.ConnectionString = connString;
config.InitKeyType = SqlSugar.InitKeyType.SystemTable;
config.ConfigureExternalServices = new SqlSugar.ConfigureExternalServices()
{
EntityNameService = (type, entity) =>
{
entity.DbTableName = entity.DbTableName + "s";
}
};
});
return services;