From a016c48960de5fdcee139fbf28235fc9348d1a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E8=BF=81?= Date: Tue, 26 Dec 2023 19:23:22 +0800 Subject: [PATCH] =?UTF-8?q?Service=20=E5=AE=9E=E7=8E=B0=E5=B1=82=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=20FreeSql=20=E7=9A=84=E4=BE=9D=E8=B5=96=EF=BC=8C?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E9=80=82=E9=85=8D=E6=96=B0=E7=9A=84=20Reposi?= =?UTF-8?q?tory=20=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IRepository.cs | 7 ++ .../FreesqlRepository.cs | 44 ++++++++++ .../MongodbRepository.cs | 45 ++++++++++ .../IUserService.cs | 1 - .../AgileConfig.Server.Service.csproj | 7 +- .../ConfigService.cs | 2 +- .../ConfigStatusUpdateRegister.cs | 25 ------ .../ServiceInfoStatusUpdateRegister.cs | 28 +++--- .../EventRegisterService/SysLogRegister.cs | 57 ++++++------ src/AgileConfig.Server.Service/JwtService.cs | 1 - .../PremissionService.cs | 25 ++++-- .../RegisterCenterService.cs | 31 +++---- .../RemoteServerNodeProxy.cs | 46 ++++++---- .../ServerNodeService.cs | 88 +++++++------------ .../ServiceCollectionExt.cs | 5 ++ .../ServiceHealthCheckService.cs | 11 ++- .../ServiceInfoService.cs | 45 ++++------ .../SettingService.cs | 1 - .../SysLogService.cs | 69 ++++++++++----- src/AgileConfig.Server.Service/UserService.cs | 66 ++++++-------- .../AgileConfig.Server.ServiceTests.csproj | 1 + .../PostgreSQL/ServerNodeServiceTests.cs | 14 ++- .../PostgreSQL/SysLogServiceTests.cs | 14 ++- .../mysql/ServerNodeServiceTests.cs | 14 ++- .../mysql/SysLogServiceTests.cs | 14 ++- .../oracle/ServerNodeServiceTests.cs | 14 ++- .../oracle/SysLogServiceTests.cs | 14 ++- .../sqlite/ServerNodeServiceTests.cs | 14 ++- .../sqlite/SysLogServiceTests.cs | 14 ++- .../sqlserver/ServerNodeServiceTests.cs | 16 +++- .../sqlserver/SysLogServiceTests.cs | 14 ++- 31 files changed, 461 insertions(+), 286 deletions(-) diff --git a/src/AgileConfig.Server.Data.Abstraction/IRepository.cs b/src/AgileConfig.Server.Data.Abstraction/IRepository.cs index 00ab8eac..062393d4 100644 --- a/src/AgileConfig.Server.Data.Abstraction/IRepository.cs +++ b/src/AgileConfig.Server.Data.Abstraction/IRepository.cs @@ -11,6 +11,8 @@ public interface IRepository : IDisposable where T : IEntity Task UpdateAsync(T entity); Task UpdateAsync(IList entities); + Task DeleteAsync(T1 id); + Task DeleteAsync(T entity); Task DeleteAsync(IList entities); @@ -20,5 +22,10 @@ public interface IRepository : IDisposable where T : IEntity Task InsertAsync(IList entities); Task> QueryAsync(Expression> exp); + + Task> QueryPageAsync(Expression> exp, int pageIndex, int pageSize, + string defaultSortField = "Id", string defaultSortType = "ASC"); + + Task CountAsync(Expression>? exp = null); } } diff --git a/src/AgileConfig.Server.Data.Freesql/FreesqlRepository.cs b/src/AgileConfig.Server.Data.Freesql/FreesqlRepository.cs index 5ddb651a..c6e93d84 100644 --- a/src/AgileConfig.Server.Data.Freesql/FreesqlRepository.cs +++ b/src/AgileConfig.Server.Data.Freesql/FreesqlRepository.cs @@ -22,6 +22,11 @@ public Task> AllAsync() return _repository.Select.ToListAsync(); } + public async Task DeleteAsync(T1 id) + { + await _repository.DeleteAsync(x => Equals(x.Id, id)); + } + public Task DeleteAsync(T entity) { return _repository.DeleteAsync(entity); @@ -52,6 +57,45 @@ public Task> QueryAsync(Expression> exp) return _repository.Where(exp).ToListAsync(); } + public async Task> QueryPageAsync(Expression> exp, int pageIndex, int pageSize, string defaultSortField = "Id", + string defaultSortType = "ASC") + { + var query = _repository.Where(exp); + var sort = Sort(defaultSortField); + if (string.Equals(defaultSortField, "DESC", StringComparison.OrdinalIgnoreCase)) + { + query.OrderByDescending(sort); + } + else + { + query.OrderBy(sort); + } + return await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync(); + } + + private Expression> Sort(string defaultSortField) + { + Expression> defaultSort = x => x.Id; + if (!string.IsNullOrEmpty(defaultSortField) && + !defaultSortField.Equals("Id", StringComparison.OrdinalIgnoreCase)) + { + var property = typeof(T).GetProperty(defaultSortField); + if (property == null) + { + return defaultSort; + } + var parameter = Expression.Parameter(typeof(T), "__q"); + var memberExpress = Expression.Property(parameter, property); + return Expression.Lambda>(memberExpress, parameter); + } + return defaultSort; + } + + public async Task CountAsync(Expression> exp = null) + { + return await (exp == null ? _repository.Select.CountAsync() : _repository.Select.Where(exp).CountAsync()); + } + public Task UpdateAsync(T entity) { return _repository.UpdateAsync(entity); diff --git a/src/AgileConfig.Server.Data.Mongodb/MongodbRepository.cs b/src/AgileConfig.Server.Data.Mongodb/MongodbRepository.cs index d05a1be3..39a796a3 100644 --- a/src/AgileConfig.Server.Data.Mongodb/MongodbRepository.cs +++ b/src/AgileConfig.Server.Data.Mongodb/MongodbRepository.cs @@ -60,6 +60,12 @@ public async Task UpdateAsync(IList entities) await _access.Collection.BulkWriteAsync(writes); } + public async Task DeleteAsync(TId id) + { + var filter = Builders.Filter.Eq(x => x.Id, id); + await _access.Collection.DeleteOneAsync(filter); + } + public async Task DeleteAsync(TEntity entity) { var filter = GetIdPropertyFilter(entity.Id); @@ -88,4 +94,43 @@ public async Task> QueryAsync(Expression> exp) { return await _access.MongoQueryable.Where(exp).ToListAsync(); } + + public async Task> QueryPageAsync(Expression> exp, int pageIndex, int pageSize, string defaultSortField = "Id", + string defaultSortType = "ASC") + { + var query = _access.MongoQueryable.Where(exp); + var sort = Sort(defaultSortField); + if (string.Equals(defaultSortField, "DESC", StringComparison.OrdinalIgnoreCase)) + { + query.OrderByDescending(sort); + } + else + { + query.OrderBy(sort); + } + return await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync(); + } + + private Expression> Sort(string defaultSortField) + { + Expression> defaultSort = x => x.Id; + if (!string.IsNullOrEmpty(defaultSortField) && + !defaultSortField.Equals("Id", StringComparison.OrdinalIgnoreCase)) + { + var property = typeof(TEntity).GetProperty(defaultSortField); + if (property == null) + { + return defaultSort; + } + var parameter = Expression.Parameter(typeof(TEntity), "__q"); + var memberExpress = Expression.Property(parameter, property); + return Expression.Lambda>(memberExpress, parameter); + } + return defaultSort; + } + + public async Task CountAsync(Expression>? exp = null) + { + return await (exp == null ? _access.MongoQueryable.CountAsync() : _access.MongoQueryable.Where(exp).CountAsync()); + } } \ No newline at end of file diff --git a/src/AgileConfig.Server.IService/IUserService.cs b/src/AgileConfig.Server.IService/IUserService.cs index 50d84710..9bc566fb 100644 --- a/src/AgileConfig.Server.IService/IUserService.cs +++ b/src/AgileConfig.Server.IService/IUserService.cs @@ -10,7 +10,6 @@ public interface IUserService: IDisposable { Task> GetAll(); Task GetUserAsync(string userId); - User GetUser(string userId); Task> GetUsersByNameAsync(string userName); Task> GetUserRolesAsync(string userId); diff --git a/src/AgileConfig.Server.Service/AgileConfig.Server.Service.csproj b/src/AgileConfig.Server.Service/AgileConfig.Server.Service.csproj index ea67d43a..98b830c9 100644 --- a/src/AgileConfig.Server.Service/AgileConfig.Server.Service.csproj +++ b/src/AgileConfig.Server.Service/AgileConfig.Server.Service.csproj @@ -4,9 +4,14 @@ net8.0 + + + + + - + diff --git a/src/AgileConfig.Server.Service/ConfigService.cs b/src/AgileConfig.Server.Service/ConfigService.cs index ce0740b8..1f6db9ec 100644 --- a/src/AgileConfig.Server.Service/ConfigService.cs +++ b/src/AgileConfig.Server.Service/ConfigService.cs @@ -468,7 +468,7 @@ public void Dispose() //这里默认admin console 实例只部署一个,如果部署多个同步操作,高并发的时候这个version会有问题 var versionMax = (await _publishTimelineRepository.QueryAsync(x => x.AppId == appId)).Max(x => x.Version); - var user = _userService.GetUser(operatorr); + var user = await _userService.GetUserAsync(operatorr); var publishTimelineNode = new PublishTimeline(); publishTimelineNode.AppId = appId; diff --git a/src/AgileConfig.Server.Service/EventRegisterService/ConfigStatusUpdateRegister.cs b/src/AgileConfig.Server.Service/EventRegisterService/ConfigStatusUpdateRegister.cs index 756767ff..0efe9122 100644 --- a/src/AgileConfig.Server.Service/EventRegisterService/ConfigStatusUpdateRegister.cs +++ b/src/AgileConfig.Server.Service/EventRegisterService/ConfigStatusUpdateRegister.cs @@ -4,7 +4,6 @@ using Agile.Config.Protocol; using AgileConfig.Server.Common; using AgileConfig.Server.Data.Entity; -using AgileConfig.Server.Data.Freesql; using AgileConfig.Server.IService; namespace AgileConfig.Server.Service.EventRegisterService; @@ -25,30 +24,6 @@ public ConfigStatusUpdateRegister( _appService = appService; } - // private IServerNodeService NewServerNodeService() - // { - // return new ServerNodeService(new FreeSqlContext(FreeSQL.Instance)); - // } - - private IAppService NewAppService() - { - // todo - - throw new NotImplementedException(); - - //return new AppService(new FreeSqlContext(FreeSQL.Instance)); - } - - private IConfigService NewConfigService() - { - // todo - - throw new NotImplementedException(); - - //return new ConfigService(null, NewAppService(), new SettingService(new FreeSqlContext(FreeSQL.Instance)), - // new UserService(new FreeSqlContext(FreeSQL.Instance))); - } - public void Register() { TinyEventBus.Instance.Register(EventKeys.PUBLISH_CONFIG_SUCCESS, (param) => diff --git a/src/AgileConfig.Server.Service/EventRegisterService/ServiceInfoStatusUpdateRegister.cs b/src/AgileConfig.Server.Service/EventRegisterService/ServiceInfoStatusUpdateRegister.cs index 9c8b63f3..4acba8e6 100644 --- a/src/AgileConfig.Server.Service/EventRegisterService/ServiceInfoStatusUpdateRegister.cs +++ b/src/AgileConfig.Server.Service/EventRegisterService/ServiceInfoStatusUpdateRegister.cs @@ -7,7 +7,6 @@ using AgileConfig.Server.Common; using AgileConfig.Server.Common.RestClient; using AgileConfig.Server.Data.Entity; -using AgileConfig.Server.Data.Freesql; using AgileConfig.Server.IService; using Microsoft.Extensions.Logging; @@ -17,28 +16,25 @@ internal class ServiceInfoStatusUpdateRegister : IEventRegister { private readonly IRemoteServerNodeProxy _remoteServerNodeProxy; private readonly IRestClient _restClient; + private readonly EventRegisterTransient _serverNodeService; + private readonly EventRegisterTransient _serviceInfoService; private ILogger _logger; public ServiceInfoStatusUpdateRegister( IRemoteServerNodeProxy remoteServerNodeProxy, ILoggerFactory loggerFactory, - IRestClient restClient + IRestClient restClient, + EventRegisterTransient serverNodeService, + EventRegisterTransient serviceInfoService ) { _remoteServerNodeProxy = remoteServerNodeProxy; _restClient = restClient; + _serverNodeService = serverNodeService; + _serviceInfoService = serviceInfoService; _logger = loggerFactory.CreateLogger(); } - - private IServerNodeService NewServerNodeService() - { - return new ServerNodeService(new FreeSqlContext(FreeSQL.Instance)); - } - - private IServiceInfoService NewServiceInfoService() - { - return new ServiceInfoService(null); - } + public void Register() { @@ -46,7 +42,7 @@ public void Register() { Task.Run(async () => { - using var serverNodeService = NewServerNodeService(); + using var serverNodeService = _serverNodeService(); var serverNodes = await serverNodeService.GetAllNodesAsync(); foreach (var serverNode in serverNodes.Where(x => x.Status == NodeStatus.Online)) { @@ -66,7 +62,7 @@ public void Register() { Task.Run(async () => { - using var serverNodeService = NewServerNodeService(); + using var serverNodeService = _serverNodeService(); var serverNodes = await serverNodeService.GetAllNodesAsync(); foreach (var serverNode in serverNodes.Where(x => x.Status == NodeStatus.Online)) { @@ -86,7 +82,7 @@ public void Register() { Task.Run(async () => { - using var serverNodeService = NewServerNodeService(); + using var serverNodeService = _serverNodeService(); var serverNodes = await serverNodeService.GetAllNodesAsync(); foreach (var serverNode in serverNodes.Where(x => x.Status == NodeStatus.Online)) { @@ -108,7 +104,7 @@ public void Register() { dynamic paramObj = param; string id = paramObj.UniqueId; - using var serviceInfoService = NewServiceInfoService(); + using var serviceInfoService = _serviceInfoService(); var service = await serviceInfoService.GetByUniqueIdAsync(id); if (service != null && !string.IsNullOrWhiteSpace(service.AlarmUrl) && service.Status == ServiceStatus.Unhealthy) diff --git a/src/AgileConfig.Server.Service/EventRegisterService/SysLogRegister.cs b/src/AgileConfig.Server.Service/EventRegisterService/SysLogRegister.cs index 3372090f..8ded506b 100644 --- a/src/AgileConfig.Server.Service/EventRegisterService/SysLogRegister.cs +++ b/src/AgileConfig.Server.Service/EventRegisterService/SysLogRegister.cs @@ -1,19 +1,18 @@ using System; -using System.Collections.Generic; using System.Threading.Tasks; -using Agile.Config.Protocol; using AgileConfig.Server.Common; using AgileConfig.Server.Data.Entity; -using AgileConfig.Server.Data.Freesql; using AgileConfig.Server.IService; namespace AgileConfig.Server.Service.EventRegisterService; internal class SysLogRegister : IEventRegister { - private ISysLogService NewSysLogService() + private readonly EventRegisterTransient _sysLogService; + + public SysLogRegister(EventRegisterTransient sysLogService) { - return new SysLogService(new FreeSqlContext(FreeSQL.Instance)); + _sysLogService = sysLogService; } public void Register() @@ -30,7 +29,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -47,7 +46,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -68,7 +67,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -88,7 +87,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -110,7 +109,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -134,7 +133,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -157,7 +156,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -180,7 +179,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -207,7 +206,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -232,7 +231,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -258,7 +257,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -282,7 +281,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -306,7 +305,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -331,7 +330,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -356,7 +355,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -381,7 +380,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -402,7 +401,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -423,7 +422,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -444,7 +443,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -465,7 +464,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -486,7 +485,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -507,7 +506,7 @@ public void Register() }; Task.Run(async () => { - using (var syslogService = NewSysLogService()) + using (var syslogService = _sysLogService()) { await syslogService.AddSysLogAsync(log); } @@ -528,7 +527,7 @@ public void Register() }; Task.Run(async () => { - using var syslogService = NewSysLogService(); + using var syslogService = _sysLogService(); await syslogService.AddSysLogAsync(log); }); }); @@ -545,7 +544,7 @@ public void Register() }; Task.Run(async () => { - using var syslogService = NewSysLogService(); + using var syslogService = _sysLogService(); await syslogService.AddSysLogAsync(log); }); }); diff --git a/src/AgileConfig.Server.Service/JwtService.cs b/src/AgileConfig.Server.Service/JwtService.cs index b7ef38bb..a1252529 100644 --- a/src/AgileConfig.Server.Service/JwtService.cs +++ b/src/AgileConfig.Server.Service/JwtService.cs @@ -3,7 +3,6 @@ using System.Security.Claims; using System.Text; using AgileConfig.Server.Common; -using AgileConfig.Server.Data.Freesql; using AgileConfig.Server.IService; using Microsoft.IdentityModel.Tokens; diff --git a/src/AgileConfig.Server.Service/PremissionService.cs b/src/AgileConfig.Server.Service/PremissionService.cs index c51525d3..f2c28ac9 100644 --- a/src/AgileConfig.Server.Service/PremissionService.cs +++ b/src/AgileConfig.Server.Service/PremissionService.cs @@ -1,18 +1,26 @@ using AgileConfig.Server.Data.Entity; -using AgileConfig.Server.Data.Freesql; using AgileConfig.Server.IService; using System.Collections.Generic; using System.Threading.Tasks; using System.Linq; +using AgileConfig.Server.Data.Abstraction; namespace AgileConfig.Server.Service { public class PermissionService : IPremissionService { - private FreeSqlContext _dbContext; - public PermissionService(FreeSqlContext freeSql) + private readonly IUserRoleRepository _userRoleRepository; + private readonly IUserAppAuthRepository _userAppAuthRepository; + private readonly IAppRepository _appRepository; + + public PermissionService( + IUserRoleRepository userRoleRepository, + IUserAppAuthRepository userAppAuthRepository, + IAppRepository appRepository) { - _dbContext = freeSql; + _userRoleRepository = userRoleRepository; + _userAppAuthRepository = userAppAuthRepository; + _appRepository = appRepository; } private static readonly List Template_SuperAdminPermissions = new List @@ -177,7 +185,7 @@ private async Task> GetNormalUserFunctions(string userId) /// public async Task> GetUserPermission(string userId) { - var userRoles = await _dbContext.UserRoles.Where(x => x.UserId == userId).ToListAsync(); + var userRoles = await _userRoleRepository.QueryAsync(x => x.UserId == userId); if (userRoles.Any(x=>x.Role == Role.SuperAdmin)) { return Template_SuperAdminPermissions; @@ -206,10 +214,11 @@ public async Task> GetUserPermission(string userId) private async Task> GetUserAuthApp(string userId, string authPermissionKey) { var apps = new List(); - var userAuths = await _dbContext.UserAppAuths.Where(x => x.UserId == userId && x.Permission == authPermissionKey).ToListAsync(); + var userAuths = + await _userAppAuthRepository.QueryAsync(x => x.UserId == userId && x.Permission == authPermissionKey); foreach (var appAuth in userAuths) { - var app = await _dbContext.Apps.Where(x=>x.Id == appAuth.AppId).FirstAsync(); + var app = await _appRepository.GetAsync(appAuth.AppId); if (app!= null) { apps.Add(app); @@ -226,7 +235,7 @@ private async Task> GetUserAuthApp(string userId, string authPermissio /// private async Task> GetUserAdminApps(string userId) { - return await _dbContext.Apps.Where(x => x.AppAdmin == userId).ToListAsync(); + return await _appRepository.QueryAsync(x => x.AppAdmin == userId); } public string EditConfigPermissionKey => "EDIT_CONFIG"; diff --git a/src/AgileConfig.Server.Service/RegisterCenterService.cs b/src/AgileConfig.Server.Service/RegisterCenterService.cs index 4a05ba08..bfcc383a 100644 --- a/src/AgileConfig.Server.Service/RegisterCenterService.cs +++ b/src/AgileConfig.Server.Service/RegisterCenterService.cs @@ -1,5 +1,4 @@ using AgileConfig.Server.Data.Entity; -using AgileConfig.Server.Data.Freesql; using AgileConfig.Server.IService; using System; using System.Collections.Generic; @@ -8,22 +7,23 @@ using System.Text; using System.Threading.Tasks; using AgileConfig.Server.Common; +using AgileConfig.Server.Data.Abstraction; using Microsoft.Extensions.Logging; namespace AgileConfig.Server.Service { public class RegisterCenterService : IRegisterCenterService { - private readonly FreeSqlContext _dbContext; + private readonly IServiceInfoRepository _serviceInfoRepository; private readonly ILogger _logger; private readonly IServiceInfoService _serviceInfoService; public RegisterCenterService( - FreeSqlContext freeSql, + IServiceInfoRepository serviceInfoRepository, IServiceInfoService serviceInfoService, ILogger logger) { - _dbContext = freeSql; + _serviceInfoRepository = serviceInfoRepository; _logger = logger; _serviceInfoService = serviceInfoService; } @@ -38,7 +38,7 @@ public async Task RegisterAsync(ServiceInfo serviceInfo) _logger.LogInformation("try to register service {0} {1}", serviceInfo.ServiceId, serviceInfo.ServiceName); //if exist - var oldEntity = await _dbContext.ServiceInfo.Where(x => x.ServiceId == serviceInfo.ServiceId).FirstAsync(); + var oldEntity = (await _serviceInfoRepository.QueryAsync(x => x.ServiceId == serviceInfo.ServiceId)).FirstOrDefault(); if (oldEntity != null) { oldEntity.RegisterTime = DateTime.Now; @@ -52,8 +52,7 @@ public async Task RegisterAsync(ServiceInfo serviceInfo) oldEntity.CheckUrl = serviceInfo.CheckUrl; oldEntity.AlarmUrl = serviceInfo.AlarmUrl; oldEntity.RegisterWay = serviceInfo.RegisterWay; - await _dbContext.ServiceInfo.UpdateAsync(oldEntity); - var rows = await _dbContext.SaveChangesAsync(); + await _serviceInfoRepository.UpdateAsync(oldEntity); _serviceInfoService.ClearCache(); @@ -68,8 +67,7 @@ public async Task RegisterAsync(ServiceInfo serviceInfo) serviceInfo.Status = ServiceStatus.Healthy; serviceInfo.Id = Guid.NewGuid().ToString("n"); - _dbContext.ServiceInfo.Add(serviceInfo); - await _dbContext.SaveChangesAsync(); + await _serviceInfoRepository.InsertAsync(serviceInfo); _serviceInfoService.ClearCache(); @@ -88,7 +86,7 @@ public async Task UnRegisterAsync(string serviceUniqueId) throw new ArgumentNullException(nameof(serviceUniqueId)); } - var oldEntity = await _dbContext.ServiceInfo.Where(x => x.Id == serviceUniqueId).FirstAsync(); + var oldEntity = await _serviceInfoRepository.GetAsync(serviceUniqueId); if (oldEntity == null) { //if not exist @@ -96,8 +94,7 @@ public async Task UnRegisterAsync(string serviceUniqueId) return false; } - _dbContext.ServiceInfo.Remove(oldEntity); - await _dbContext.SaveChangesAsync(); + await _serviceInfoRepository.DeleteAsync(oldEntity); _serviceInfoService.ClearCache(); @@ -116,7 +113,7 @@ public async Task UnRegisterByServiceIdAsync(string serviceId) throw new ArgumentNullException(nameof(serviceId)); } - var oldEntity = await _dbContext.ServiceInfo.Where(x => x.ServiceId == serviceId).FirstAsync(); + var oldEntity = (await _serviceInfoRepository.QueryAsync(x => x.ServiceId == serviceId)).FirstOrDefault(); if (oldEntity == null) { //if not exist @@ -124,8 +121,7 @@ public async Task UnRegisterByServiceIdAsync(string serviceId) return false; } - _dbContext.ServiceInfo.Remove(oldEntity); - await _dbContext.SaveChangesAsync(); + await _serviceInfoRepository.DeleteAsync(oldEntity); _serviceInfoService.ClearCache(); @@ -137,7 +133,7 @@ public async Task UnRegisterByServiceIdAsync(string serviceId) public async Task ReceiveHeartbeatAsync(string serviceUniqueId) { - var entity = await _dbContext.ServiceInfo.Where(x => x.Id == serviceUniqueId).FirstAsync(); + var entity = await _serviceInfoRepository.GetAsync(serviceUniqueId); if (entity == null) { return false; @@ -154,9 +150,8 @@ public async Task ReceiveHeartbeatAsync(string serviceUniqueId) var oldStatus = entity.Status; entity.Status = ServiceStatus.Healthy; entity.LastHeartBeat = DateTime.Now; - await _dbContext.UpdateAsync(entity); - await _dbContext.SaveChangesAsync(); + await _serviceInfoRepository.UpdateAsync(entity); if (oldStatus != ServiceStatus.Healthy) { diff --git a/src/AgileConfig.Server.Service/RemoteServerNodeProxy.cs b/src/AgileConfig.Server.Service/RemoteServerNodeProxy.cs index 2dea6f76..1d100119 100644 --- a/src/AgileConfig.Server.Service/RemoteServerNodeProxy.cs +++ b/src/AgileConfig.Server.Service/RemoteServerNodeProxy.cs @@ -2,7 +2,6 @@ using AgileConfig.Server.Common; using AgileConfig.Server.Common.RestClient; using AgileConfig.Server.Data.Entity; -using AgileConfig.Server.Data.Freesql; using AgileConfig.Server.IService; using Microsoft.Extensions.Logging; using System; @@ -15,26 +14,35 @@ namespace AgileConfig.Server.Service { public class RemoteServerNodeProxy : IRemoteServerNodeProxy { - private IServerNodeService GetServerNodeService() - { - return new ServerNodeService(new FreeSqlContext(FreeSQL.Instance)); - } - - private ILogger _logger; - - private ISysLogService GetSysLogService() - { - return new SysLogService(new FreeSqlContext(FreeSQL.Instance)); - } + // private IServerNodeService GetServerNodeService() + // { + // return new ServerNodeService(new FreeSqlContext(FreeSQL.Instance)); + // } + // + // private ISysLogService GetSysLogService() + // { + // return new SysLogService(new FreeSqlContext(FreeSQL.Instance)); + // } + + private readonly ILogger _logger; + private readonly IRestClient _restClient; + private readonly EventRegisterTransient _serverNodeService; + private readonly EventRegisterTransient _sysLogService; private static ConcurrentDictionary _serverNodeClientReports = new ConcurrentDictionary(); - private readonly IRestClient _restClient; + - public RemoteServerNodeProxy(ILoggerFactory loggerFactory, IRestClient restClient) + public RemoteServerNodeProxy( + ILoggerFactory loggerFactory, + IRestClient restClient, + EventRegisterTransient serverNodeService, + EventRegisterTransient sysLogService) { _logger = loggerFactory.CreateLogger(); _restClient = restClient; + _serverNodeService = serverNodeService; + _sysLogService = sysLogService; } public async Task AllClientsDoActionAsync(string address, WebsocketAction action) @@ -50,7 +58,7 @@ public async Task AllClientsDoActionAsync(string address, WebsocketAction return false; }, 5); - using (var service = GetSysLogService()) + using (var service = _sysLogService()) { var module = ""; if (action.Module == "r") @@ -86,7 +94,7 @@ public async Task AppClientsDoActionAsync(string address, string appId, st return false; }, 5); - using (var service = GetSysLogService()) + using (var service = _sysLogService()) { var module = ""; if (action.Module == "r") @@ -137,7 +145,7 @@ public async Task OneClientDoActionAsync(string address, string clientId, return false; }, 5); - using (var service = GetSysLogService()) + using (var service = _sysLogService()) { var module = ""; if (action.Module == "r") @@ -201,7 +209,7 @@ public async Task GetClientsReportAsync(string address) public async Task TestEchoAsync(string address) { - using var service = GetServerNodeService(); + using var service = _serverNodeService(); var node = await service.GetAsync(address); try { @@ -249,7 +257,7 @@ public Task TestEchoAsync() { while (true) { - using var service = GetServerNodeService(); + using var service = _serverNodeService(); var nodes = await service.GetAllNodesAsync(); foreach (var node in nodes) diff --git a/src/AgileConfig.Server.Service/ServerNodeService.cs b/src/AgileConfig.Server.Service/ServerNodeService.cs index 4f2084f7..db0a66a3 100644 --- a/src/AgileConfig.Server.Service/ServerNodeService.cs +++ b/src/AgileConfig.Server.Service/ServerNodeService.cs @@ -4,82 +4,73 @@ using System.Threading.Tasks; using AgileConfig.Server.Common; using System; -using AgileConfig.Server.Data.Freesql; +using System.Linq; +using AgileConfig.Server.Data.Abstraction; namespace AgileConfig.Server.Service { public class ServerNodeService : IServerNodeService { - private FreeSqlContext _dbContext; + private readonly IServerNodeRepository _serverNodeRepository; - public ServerNodeService(FreeSqlContext context) + + public ServerNodeService(IServerNodeRepository serverNodeRepository) { - _dbContext = context; + _serverNodeRepository = serverNodeRepository; } public async Task AddAsync(ServerNode node) { - await _dbContext.ServerNodes.AddAsync(node); - int x = await _dbContext.SaveChangesAsync(); - var result = x > 0; - - return result; + await _serverNodeRepository.InsertAsync(node); + return true; } public async Task DeleteAsync(ServerNode node) { - node = await _dbContext.ServerNodes.Where(n => n.Id == node.Id).ToOneAsync(); - if (node != null) + var node2 = await _serverNodeRepository.GetAsync(node.Id); + if (node2 != null) { - _dbContext.ServerNodes.Remove(node); + await _serverNodeRepository.DeleteAsync(node2); } - int x = await _dbContext.SaveChangesAsync(); - var result = x > 0; - - return result; + return true; } public async Task DeleteAsync(string address) { - var node = await _dbContext.ServerNodes.Where(n => n.Id == address).ToOneAsync(); - if (node != null) + var node2 = await _serverNodeRepository.GetAsync(address); + if (node2 != null) { - _dbContext.ServerNodes.Remove(node); + await _serverNodeRepository.DeleteAsync(node2); } - int x = await _dbContext.SaveChangesAsync(); - var result = x > 0; - return result; + return true; } public void Dispose() { - _dbContext.Dispose(); + _serverNodeRepository.Dispose(); } public async Task> GetAllNodesAsync() { - return await _dbContext.ServerNodes.Where(n => 1 == 1).ToListAsync(); + return await _serverNodeRepository.AllAsync(); } public async Task GetAsync(string address) { - return await _dbContext.ServerNodes.Where(n => n.Id == address).ToOneAsync(); + return await _serverNodeRepository.GetAsync(address); } public async Task UpdateAsync(ServerNode node) { - _dbContext.Update(node); - var x = await _dbContext.SaveChangesAsync(); - var result = x > 0; - - return result; + await _serverNodeRepository.UpdateAsync(node); + return true; } public async Task InitWatchNodeAsync() { - var count = await _dbContext.ServerNodes.Select.CountAsync(); + var count = await _serverNodeRepository.CountAsync(); if (count > 0) { return false; @@ -105,33 +96,23 @@ public async Task InitWatchNodeAsync() } } - foreach (var address in addresses) - { - var node = await _dbContext.ServerNodes.Where(n => n.Id == address).ToOneAsync(); - if (node == null) - { - node = new ServerNode() - { - Id = address, - CreateTime = DateTime.Now, - }; - await _dbContext.ServerNodes.AddAsync(node); - } - } + var existNodes = await _serverNodeRepository.QueryAsync(x => addresses.Contains(x.Id)); + var newNodes = addresses + .Where(x => existNodes.All(y => y.Id != x)) + .Select(x => new ServerNode { Id = x, CreateTime = DateTime.Now }) + .ToList(); - var result = 0; - if (addresses.Count > 0) - { - result = await _dbContext.SaveChangesAsync(); - } + if (newNodes.Count == 0) + return false; - return result > 0; + await _serverNodeRepository.InsertAsync(newNodes); + return true; } public async Task JoinAsync(string ip, int port, string desc) { var address = $"http://{ip}:{port}"; - var nodes = await _dbContext.ServerNodes.Where(x => x.Id == address).ToListAsync(); + var nodes = await _serverNodeRepository.QueryAsync(x => x.Id == address); if (nodes.Count > 0) { nodes.ForEach(n => { @@ -142,7 +123,7 @@ public async Task JoinAsync(string ip, int port, string desc) } else { - await _dbContext.ServerNodes.AddAsync(new ServerNode { + await _serverNodeRepository.InsertAsync(new ServerNode { Id = address, CreateTime = DateTime.Now, Remark = desc, @@ -151,8 +132,7 @@ await _dbContext.ServerNodes.AddAsync(new ServerNode { }); } - var effRows = await _dbContext.SaveChangesAsync(); - return effRows > 0; + return true; } } } diff --git a/src/AgileConfig.Server.Service/ServiceCollectionExt.cs b/src/AgileConfig.Server.Service/ServiceCollectionExt.cs index 216c6ee4..e3e5ee95 100644 --- a/src/AgileConfig.Server.Service/ServiceCollectionExt.cs +++ b/src/AgileConfig.Server.Service/ServiceCollectionExt.cs @@ -32,6 +32,9 @@ public static void AddBusinessServices(this IServiceCollection sc) sc.AddScoped(); sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); sc.AddScoped(x => key => { return key switch @@ -46,6 +49,8 @@ public static void AddBusinessServices(this IServiceCollection sc) sc.AddTransient,EventRegisterTransient>(); sc.AddTransient,EventRegisterTransient>(); sc.AddTransient,EventRegisterTransient>(); + sc.AddTransient,EventRegisterTransient>(); + sc.AddTransient,EventRegisterTransient>(); } } } diff --git a/src/AgileConfig.Server.Service/ServiceHealthCheckService.cs b/src/AgileConfig.Server.Service/ServiceHealthCheckService.cs index 0474d275..2c73b6f0 100644 --- a/src/AgileConfig.Server.Service/ServiceHealthCheckService.cs +++ b/src/AgileConfig.Server.Service/ServiceHealthCheckService.cs @@ -2,8 +2,8 @@ using System.Threading.Tasks; using AgileConfig.Server.Common; using AgileConfig.Server.Common.RestClient; +using AgileConfig.Server.Data.Abstraction; using AgileConfig.Server.Data.Entity; -using AgileConfig.Server.Data.Freesql; using AgileConfig.Server.IService; using Microsoft.Extensions.Logging; @@ -13,17 +13,20 @@ public class ServiceHealthCheckService : IServiceHealthCheckService { private readonly ILogger _logger; private readonly IRestClient _restClient; + private readonly IServiceInfoRepository _serviceInfoRepository; private readonly IServiceInfoService _serviceInfoService; public ServiceHealthCheckService( IServiceInfoService serviceInfoService, ILogger logger, - IRestClient restClient + IRestClient restClient, + IServiceInfoRepository serviceInfoRepository ) { _serviceInfoService = serviceInfoService; _logger = logger; _restClient = restClient; + _serviceInfoRepository = serviceInfoRepository; } private int _checkInterval; @@ -121,8 +124,8 @@ public Task StartCheckAsync() while (true) { //没有填写心跳模式,则不做检查 - var services = await FreeSQL.Instance.Select() - .Where(x => x.HeartBeatMode != null && x.HeartBeatMode != "").ToListAsync(); + var services = await _serviceInfoRepository + .QueryAsync(x => x.HeartBeatMode != null && x.HeartBeatMode != ""); foreach (var service in services) { if (service.HeartBeatMode == HeartBeatModes.none.ToString()) diff --git a/src/AgileConfig.Server.Service/ServiceInfoService.cs b/src/AgileConfig.Server.Service/ServiceInfoService.cs index e899223e..49fce178 100644 --- a/src/AgileConfig.Server.Service/ServiceInfoService.cs +++ b/src/AgileConfig.Server.Service/ServiceInfoService.cs @@ -1,5 +1,4 @@ using AgileConfig.Server.Data.Entity; -using AgileConfig.Server.Data.Freesql; using AgileConfig.Server.IService; using System; using System.Collections.Generic; @@ -8,6 +7,7 @@ using System.Text; using System.Threading.Tasks; using AgileConfig.Server.Common; +using AgileConfig.Server.Data.Abstraction; using Microsoft.Extensions.Caching.Memory; using Newtonsoft.Json; @@ -16,53 +16,51 @@ namespace AgileConfig.Server.Service public class ServiceInfoService : IServiceInfoService { private readonly IMemoryCache _memoryCache; + private readonly IServiceInfoRepository _serviceInfoRepository; - public ServiceInfoService(IMemoryCache memoryCache) + public ServiceInfoService(IMemoryCache memoryCache,IServiceInfoRepository serviceInfoRepository) { _memoryCache = memoryCache; + _serviceInfoRepository = serviceInfoRepository; } public async Task GetByUniqueIdAsync(string id) { - var entity = await FreeSQL.Instance.Select().Where(x => x.Id == id).FirstAsync(); + var entity = await _serviceInfoRepository.GetAsync(id); return entity; } public async Task GetByServiceIdAsync(string serviceId) { - var entity = await FreeSQL.Instance.Select().Where(x => x.ServiceId == serviceId).FirstAsync(); + var entity = (await _serviceInfoRepository.QueryAsync(x => x.ServiceId == serviceId)).FirstOrDefault(); return entity; } public async Task RemoveAsync(string id) { - var aff = await FreeSQL.Instance.Delete().Where(x => x.Id == id) - .ExecuteAffrowsAsync(); - - return aff > 0; + await _serviceInfoRepository.DeleteAsync(id); + return true; } public async Task> GetAllServiceInfoAsync() { - var services = await FreeSQL.Instance.Select().Where(x => 1 == 1).ToListAsync(); + var services = await _serviceInfoRepository.AllAsync(); return services; } public async Task> GetOnlineServiceInfoAsync() { - var services = await FreeSQL.Instance.Select().Where(x => x.Status == ServiceStatus.Healthy) - .ToListAsync(); + var services = await _serviceInfoRepository.QueryAsync(x => x.Status == ServiceStatus.Healthy); return services; } public async Task> GetOfflineServiceInfoAsync() { - var services = await FreeSQL.Instance.Select().Where(x => x.Status == ServiceStatus.Unhealthy) - .ToListAsync(); + var services = await _serviceInfoRepository.QueryAsync(x => x.Status == ServiceStatus.Unhealthy); return services; } @@ -127,21 +125,15 @@ public async Task UpdateServiceStatus(ServiceInfo service, ServiceStatus status) var id = service.Id; var oldStatus = service.Status; - if (status == ServiceStatus.Unhealthy) - { - await FreeSQL.Instance.Update() - .Set(x => x.Status, status) - .Where(x => x.Id == id) - .ExecuteAffrowsAsync(); - } - else + var service2 = await _serviceInfoRepository.GetAsync(id); + if (service2 == null) return; + + service2.Status = status; + if (status != ServiceStatus.Unhealthy) { - await FreeSQL.Instance.Update() - .Set(x => x.Status, status) - .Set(x => x.LastHeartBeat, DateTime.Now) - .Where(x => x.Id == id) - .ExecuteAffrowsAsync(); + service2.LastHeartBeat = DateTime.Now; } + await _serviceInfoRepository.UpdateAsync(service2); if (oldStatus != status) { @@ -156,6 +148,7 @@ await FreeSQL.Instance.Update() public void Dispose() { + _serviceInfoRepository.Dispose(); } } } \ No newline at end of file diff --git a/src/AgileConfig.Server.Service/SettingService.cs b/src/AgileConfig.Server.Service/SettingService.cs index 89812a60..e0bb9fe0 100644 --- a/src/AgileConfig.Server.Service/SettingService.cs +++ b/src/AgileConfig.Server.Service/SettingService.cs @@ -5,7 +5,6 @@ using System.Threading.Tasks; using AgileConfig.Server.Common; using AgileConfig.Server.Data.Abstraction; -using AgileConfig.Server.Data.Freesql; namespace AgileConfig.Server.Service { diff --git a/src/AgileConfig.Server.Service/SysLogService.cs b/src/AgileConfig.Server.Service/SysLogService.cs index d98620c3..3a49c5e5 100644 --- a/src/AgileConfig.Server.Service/SysLogService.cs +++ b/src/AgileConfig.Server.Service/SysLogService.cs @@ -2,85 +2,108 @@ using AgileConfig.Server.IService; using System; using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; using System.Threading.Tasks; -using AgileConfig.Server.Data.Freesql; +using AgileConfig.Server.Data.Abstraction; namespace AgileConfig.Server.Service { public class SysLogService : ISysLogService { - private FreeSqlContext _dbContext; + private readonly ISysLogRepository _sysLogRepository; - public SysLogService(FreeSqlContext context) + public SysLogService(ISysLogRepository sysLogRepository) { - _dbContext = context; + _sysLogRepository = sysLogRepository; } public async Task AddRangeAsync(IEnumerable logs) { - int x = await _dbContext.Freesql.Insert(logs).ExecuteAffrowsAsync(); - return x > 0; + await _sysLogRepository.InsertAsync(logs.ToList()); + return true; } public async Task AddSysLogAsync(SysLog log) { - int x = await _dbContext.Freesql.Insert(log).ExecuteAffrowsAsync();; - return x > 0; + await _sysLogRepository.InsertAsync(log); + return true; } public async Task Count(string appId, SysLogType? logType, DateTime? startTime, DateTime? endTime) { - var query = _dbContext.SysLogs.Where(s => 1 == 1); + var query = (Expression)Expression + .Constant(true); + if (!string.IsNullOrEmpty(appId)) { - query = query.Where(x => x.AppId == appId); + Expression> expr = x => x.AppId == appId; + query = Expression.AndAlso(query, expr); } + if (startTime.HasValue) { - query = query.Where(x => x.LogTime >= startTime); + Expression> expr = (x => x.LogTime >= startTime); + query = Expression.AndAlso(query, expr); } + if (endTime.HasValue) { - query = query.Where(x => x.LogTime < endTime); + Expression> expr = (x => x.LogTime < endTime); + query = Expression.AndAlso(query, expr); } + if (logType.HasValue) { - query = query.Where(x => x.LogType == logType); + Expression> expr = (x => x.LogType == logType); + query = Expression.AndAlso(query, expr); } - var count = await query.CountAsync(); + var exp = Expression.Lambda>(query); + + var count = await _sysLogRepository.CountAsync(exp); return count; } public void Dispose() { - _dbContext.Dispose(); + _sysLogRepository.Dispose(); } - public Task> SearchPage(string appId, SysLogType? logType, DateTime? startTime, DateTime? endTime, int pageSize, int pageIndex) + public async Task> SearchPage(string appId, SysLogType? logType, DateTime? startTime, DateTime? endTime, int pageSize, int pageIndex) { - var query = _dbContext.SysLogs.Where(s => 1 == 1); + var query = (Expression)Expression + .Constant(true); + if (!string.IsNullOrEmpty(appId)) { - query = query.Where(x => x.AppId == appId); + Expression> expr = x => x.AppId == appId; + query = Expression.AndAlso(query, expr); } + if (startTime.HasValue) { - query = query.Where(x => x.LogTime >= startTime); + Expression> expr = (x => x.LogTime >= startTime); + query = Expression.AndAlso(query, expr); } + if (endTime.HasValue) { - query = query.Where(x => x.LogTime < endTime); + Expression> expr = (x => x.LogTime < endTime); + query = Expression.AndAlso(query, expr); } + if (logType.HasValue) { - query = query.Where(x => x.LogType == logType); + Expression> expr = (x => x.LogType == logType); + query = Expression.AndAlso(query, expr); } - query = query.OrderByDescending(x => x.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize); + var exp = Expression.Lambda>(query); - return query.ToListAsync(); + var list = await _sysLogRepository.QueryPageAsync(exp, pageIndex, pageSize, defaultSortType: "DESC"); + return list; } } } diff --git a/src/AgileConfig.Server.Service/UserService.cs b/src/AgileConfig.Server.Service/UserService.cs index a709df93..7b87c74e 100644 --- a/src/AgileConfig.Server.Service/UserService.cs +++ b/src/AgileConfig.Server.Service/UserService.cs @@ -1,59 +1,59 @@ using AgileConfig.Server.Data.Entity; -using AgileConfig.Server.Data.Freesql; using AgileConfig.Server.IService; using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Linq; using AgileConfig.Server.Common; +using AgileConfig.Server.Data.Abstraction; namespace AgileConfig.Server.Service { public class UserService : IUserService { - private FreeSqlContext _dbContext; + private readonly IUserRepository _userRepository; + private readonly IUserRoleRepository _userRoleRepository; - public UserService(FreeSqlContext context) + + public UserService(IUserRepository userRepository,IUserRoleRepository userRoleRepository) { - _dbContext = context; + _userRepository = userRepository; + _userRoleRepository = userRoleRepository; } public async Task AddAsync(User user) { - var old = await _dbContext.Users.Where(u => u.UserName == user.UserName && u.Status == UserStatus.Normal).FirstAsync(); + var old = (await _userRepository.QueryAsync(u => u.UserName == user.UserName && u.Status == UserStatus.Normal)).FirstOrDefault(); if (old != null) { return false; } - await _dbContext.Users.AddAsync(user); - var result = await _dbContext.SaveChangesAsync(); + await _userRepository.InsertAsync(user); - return result > 0; + return true; } public async Task DeleteAsync(User user) { - _dbContext.Users.Remove(user); - var result = await _dbContext.SaveChangesAsync(); - - return result > 0; + await _userRepository.DeleteAsync(user); + return true; } public async Task> GetUsersByNameAsync(string userName) { - return await _dbContext.Users.Where(u => u.UserName == userName).ToListAsync(); + return await _userRepository.QueryAsync(u => u.UserName == userName); } public async Task GetUserAsync(string id) { - return await _dbContext.Users.Where(u => u.Id == id).ToOneAsync(); + return await _userRepository.GetAsync(id); } public async Task> GetUserRolesAsync(string userId) { - var userRoles = await _dbContext.UserRoles.Where(x => x.UserId == userId).ToListAsync(); + var userRoles = await _userRoleRepository.QueryAsync(x => x.UserId == userId); return userRoles.Select(x => x.Role).ToList(); } @@ -61,15 +61,14 @@ public async Task> GetUserRolesAsync(string userId) public async Task UpdateAsync(User user) { - await _dbContext.Users.UpdateAsync(user); - var result = await _dbContext.SaveChangesAsync(); - - return result > 0; + await _userRepository.UpdateAsync(user); + return true; } public async Task UpdateUserRolesAsync(string userId, List roles) { - await _dbContext.UserRoles.RemoveAsync(x => x.UserId == userId); + var dbUserRoles = await _userRoleRepository.QueryAsync(x => x.UserId == userId); + await _userRoleRepository.DeleteAsync(dbUserRoles); var userRoles = new List(); roles.ForEach(x => { userRoles.Add(new UserRole @@ -80,25 +79,24 @@ public async Task UpdateUserRolesAsync(string userId, List roles) }); }); - await _dbContext.UserRoles.AddRangeAsync(userRoles); - var result = await _dbContext.SaveChangesAsync(); - - return result > 0; + await _userRoleRepository.InsertAsync(userRoles); + return true; } public void Dispose() { - _dbContext?.Dispose(); + _userRepository.Dispose(); + _userRoleRepository.Dispose(); } public async Task> GetAll() { - return await _dbContext.Users.Where(x => 1 == 1).ToListAsync(); + return await _userRepository.AllAsync(); } public async Task ValidateUserPassword(string userName, string password) { - var user = await _dbContext.Users.Where(u => u.Status == UserStatus.Normal && u.UserName == userName).FirstAsync(); + var user = (await _userRepository.QueryAsync(u => u.Status == UserStatus.Normal && u.UserName == userName)).FirstOrDefault(); if (user == null) { return false; @@ -114,17 +112,9 @@ public async Task ValidateUserPassword(string userName, string password) public async Task> GetUsersByRoleAsync(Role role) { - var users = await FreeSQL.Instance.Select() - .InnerJoin((a, b) => a.Id == b.UserId) - .Where((a, b) => b.Role == role) - .ToListAsync((a, b) => a); - - return users; - } - - public User GetUser(string userId) - { - return _dbContext.Users.Where(u => u.Id == userId).ToOne(); + var userRoles = await _userRoleRepository.QueryAsync(x => x.Role == role); + var userIds = userRoles.Select(x => x.UserId).Distinct().ToList(); + return await _userRepository.QueryAsync(x => userIds.Contains(x.Id)); } } } diff --git a/test/AgileConfig.Server.ServiceTests/AgileConfig.Server.ServiceTests.csproj b/test/AgileConfig.Server.ServiceTests/AgileConfig.Server.ServiceTests.csproj index 581ad7c0..4de281c3 100644 --- a/test/AgileConfig.Server.ServiceTests/AgileConfig.Server.ServiceTests.csproj +++ b/test/AgileConfig.Server.ServiceTests/AgileConfig.Server.ServiceTests.csproj @@ -18,6 +18,7 @@ + diff --git a/test/AgileConfig.Server.ServiceTests/PostgreSQL/ServerNodeServiceTests.cs b/test/AgileConfig.Server.ServiceTests/PostgreSQL/ServerNodeServiceTests.cs index 429523c9..2afdf5e1 100644 --- a/test/AgileConfig.Server.ServiceTests/PostgreSQL/ServerNodeServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/PostgreSQL/ServerNodeServiceTests.cs @@ -7,6 +7,9 @@ using FreeSql; using AgileConfig.Server.Data.Entity; using System.Threading.Tasks; +using AgileConfig.Server.Data.Repository.Freesql; +using AgileConfig.Server.IService; +using Microsoft.Extensions.DependencyInjection; namespace AgileConfig.Server.Service.Tests.PostgreSQL { @@ -15,7 +18,7 @@ public class ServerNodeServiceTests { IFreeSql fsq = null; FreeSqlContext freeSqlContext; - ServerNodeService service = null; + IServerNodeService service = null; [TestInitialize] public void TestInitialize() @@ -28,7 +31,14 @@ public void TestInitialize() FluentApi.Config(fsq); freeSqlContext = new FreeSqlContext(fsq); - service = new ServerNodeService(freeSqlContext); + IServiceCollection services = new ServiceCollection(); + services.AddFreeSqlFactory(); + services.AddFreeSqlRepository(); + services.AddBusinessServices(); + + + var serviceProvider = services.BuildServiceProvider(); + service = serviceProvider.GetService(); fsq.Delete().Where("1=1"); Console.WriteLine("TestInitialize"); diff --git a/test/AgileConfig.Server.ServiceTests/PostgreSQL/SysLogServiceTests.cs b/test/AgileConfig.Server.ServiceTests/PostgreSQL/SysLogServiceTests.cs index 11a65701..097f199f 100644 --- a/test/AgileConfig.Server.ServiceTests/PostgreSQL/SysLogServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/PostgreSQL/SysLogServiceTests.cs @@ -7,6 +7,9 @@ using FreeSql; using AgileConfig.Server.Data.Entity; using System.Threading.Tasks; +using AgileConfig.Server.Data.Repository.Freesql; +using AgileConfig.Server.IService; +using Microsoft.Extensions.DependencyInjection; namespace AgileConfig.Server.Service.Tests.PostgreSQL { @@ -16,7 +19,7 @@ public class SysLogServiceTests IFreeSql fsq = null; FreeSqlContext freeSqlContext; - SysLogService service = null; + ISysLogService service = null; [TestInitialize] public void TestInitialize() @@ -29,7 +32,14 @@ public void TestInitialize() FluentApi.Config(fsq); freeSqlContext = new FreeSqlContext(fsq); - service = new SysLogService(freeSqlContext); + IServiceCollection services = new ServiceCollection(); + services.AddFreeSqlFactory(); + services.AddFreeSqlRepository(); + services.AddBusinessServices(); + + + var serviceProvider = services.BuildServiceProvider(); + service = serviceProvider.GetService(); fsq.Delete().Where("1=1"); Console.WriteLine("TestInitialize"); diff --git a/test/AgileConfig.Server.ServiceTests/mysql/ServerNodeServiceTests.cs b/test/AgileConfig.Server.ServiceTests/mysql/ServerNodeServiceTests.cs index e4ce7725..daa7565e 100644 --- a/test/AgileConfig.Server.ServiceTests/mysql/ServerNodeServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/mysql/ServerNodeServiceTests.cs @@ -7,6 +7,9 @@ using FreeSql; using AgileConfig.Server.Data.Entity; using System.Threading.Tasks; +using AgileConfig.Server.Data.Repository.Freesql; +using AgileConfig.Server.IService; +using Microsoft.Extensions.DependencyInjection; namespace AgileConfig.Server.Service.Tests.mysql { @@ -15,7 +18,7 @@ public class ServerNodeServiceTests { IFreeSql fsq = null; FreeSqlContext freeSqlContext; - ServerNodeService service = null; + IServerNodeService service = null; [TestInitialize] public void TestInitialize() @@ -27,7 +30,14 @@ public void TestInitialize() .Build(); freeSqlContext = new FreeSqlContext(fsq); - service = new ServerNodeService(freeSqlContext); + IServiceCollection services = new ServiceCollection(); + services.AddFreeSqlFactory(); + services.AddFreeSqlRepository(); + services.AddBusinessServices(); + + + var serviceProvider = services.BuildServiceProvider(); + service = serviceProvider.GetService(); fsq.Delete().Where("1=1"); Console.WriteLine("TestInitialize"); diff --git a/test/AgileConfig.Server.ServiceTests/mysql/SysLogServiceTests.cs b/test/AgileConfig.Server.ServiceTests/mysql/SysLogServiceTests.cs index 6500da51..b4021f00 100644 --- a/test/AgileConfig.Server.ServiceTests/mysql/SysLogServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/mysql/SysLogServiceTests.cs @@ -7,6 +7,9 @@ using FreeSql; using AgileConfig.Server.Data.Entity; using System.Threading.Tasks; +using AgileConfig.Server.Data.Repository.Freesql; +using AgileConfig.Server.IService; +using Microsoft.Extensions.DependencyInjection; namespace AgileConfig.Server.Service.Tests.mysql { @@ -16,7 +19,7 @@ public class SysLogServiceTests IFreeSql fsq = null; FreeSqlContext freeSqlContext; - SysLogService service = null; + ISysLogService service = null; [TestInitialize] public void TestInitialize() @@ -28,7 +31,14 @@ public void TestInitialize() .Build(); freeSqlContext = new FreeSqlContext(fsq); - service = new SysLogService(freeSqlContext); + IServiceCollection services = new ServiceCollection(); + services.AddFreeSqlFactory(); + services.AddFreeSqlRepository(); + services.AddBusinessServices(); + + + var serviceProvider = services.BuildServiceProvider(); + service = serviceProvider.GetService(); fsq.Delete().Where("1=1"); Console.WriteLine("TestInitialize"); diff --git a/test/AgileConfig.Server.ServiceTests/oracle/ServerNodeServiceTests.cs b/test/AgileConfig.Server.ServiceTests/oracle/ServerNodeServiceTests.cs index 93ea8ec8..59206aef 100644 --- a/test/AgileConfig.Server.ServiceTests/oracle/ServerNodeServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/oracle/ServerNodeServiceTests.cs @@ -7,6 +7,9 @@ using FreeSql; using AgileConfig.Server.Data.Entity; using System.Threading.Tasks; +using AgileConfig.Server.Data.Repository.Freesql; +using AgileConfig.Server.IService; +using Microsoft.Extensions.DependencyInjection; namespace AgileConfig.Server.Service.Tests.oracle { @@ -15,7 +18,7 @@ public class ServerNodeServiceTests { IFreeSql fsq = null; FreeSqlContext freeSqlContext; - ServerNodeService service = null; + IServerNodeService service = null; [TestInitialize] public void TestInitialize() @@ -28,7 +31,14 @@ public void TestInitialize() FluentApi.Config(fsq); freeSqlContext = new FreeSqlContext(fsq); - service = new ServerNodeService(freeSqlContext); + IServiceCollection services = new ServiceCollection(); + services.AddFreeSqlFactory(); + services.AddFreeSqlRepository(); + services.AddBusinessServices(); + + + var serviceProvider = services.BuildServiceProvider(); + service = serviceProvider.GetService(); fsq.Delete().Where("1=1"); Console.WriteLine("TestInitialize"); diff --git a/test/AgileConfig.Server.ServiceTests/oracle/SysLogServiceTests.cs b/test/AgileConfig.Server.ServiceTests/oracle/SysLogServiceTests.cs index 9d71a71d..b0a18c1e 100644 --- a/test/AgileConfig.Server.ServiceTests/oracle/SysLogServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/oracle/SysLogServiceTests.cs @@ -7,6 +7,9 @@ using FreeSql; using AgileConfig.Server.Data.Entity; using System.Threading.Tasks; +using AgileConfig.Server.Data.Repository.Freesql; +using AgileConfig.Server.IService; +using Microsoft.Extensions.DependencyInjection; namespace AgileConfig.Server.Service.Tests.oracle { @@ -16,7 +19,7 @@ public class SysLogServiceTests IFreeSql fsq = null; FreeSqlContext freeSqlContext; - SysLogService service = null; + ISysLogService service = null; [TestInitialize] public void TestInitialize() @@ -29,7 +32,14 @@ public void TestInitialize() FluentApi.Config(fsq); freeSqlContext = new FreeSqlContext(fsq); - service = new SysLogService(freeSqlContext); + IServiceCollection services = new ServiceCollection(); + services.AddFreeSqlFactory(); + services.AddFreeSqlRepository(); + services.AddBusinessServices(); + + + var serviceProvider = services.BuildServiceProvider(); + service = serviceProvider.GetService(); fsq.Delete().Where("1=1"); Console.WriteLine("TestInitialize"); diff --git a/test/AgileConfig.Server.ServiceTests/sqlite/ServerNodeServiceTests.cs b/test/AgileConfig.Server.ServiceTests/sqlite/ServerNodeServiceTests.cs index b7365651..3a7fb9f0 100644 --- a/test/AgileConfig.Server.ServiceTests/sqlite/ServerNodeServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/sqlite/ServerNodeServiceTests.cs @@ -7,6 +7,9 @@ using FreeSql; using AgileConfig.Server.Data.Entity; using System.Threading.Tasks; +using AgileConfig.Server.Data.Repository.Freesql; +using AgileConfig.Server.IService; +using Microsoft.Extensions.DependencyInjection; namespace AgileConfig.Server.Service.Tests { @@ -15,7 +18,7 @@ public class ServerNodeServiceTests { IFreeSql fsq = null; FreeSqlContext freeSqlContext; - ServerNodeService service = null; + IServerNodeService service = null; [TestInitialize] public void TestInitialize() @@ -27,7 +30,14 @@ public void TestInitialize() .Build(); freeSqlContext = new FreeSqlContext(fsq); - service = new ServerNodeService(freeSqlContext); + IServiceCollection services = new ServiceCollection(); + services.AddFreeSqlFactory(); + services.AddFreeSqlRepository(); + services.AddBusinessServices(); + + + var serviceProvider = services.BuildServiceProvider(); + service = serviceProvider.GetService(); fsq.Delete().Where("1=1"); Console.WriteLine("TestInitialize"); diff --git a/test/AgileConfig.Server.ServiceTests/sqlite/SysLogServiceTests.cs b/test/AgileConfig.Server.ServiceTests/sqlite/SysLogServiceTests.cs index 7ce386a5..f84493f9 100644 --- a/test/AgileConfig.Server.ServiceTests/sqlite/SysLogServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/sqlite/SysLogServiceTests.cs @@ -7,6 +7,9 @@ using FreeSql; using AgileConfig.Server.Data.Entity; using System.Threading.Tasks; +using AgileConfig.Server.Data.Repository.Freesql; +using AgileConfig.Server.IService; +using Microsoft.Extensions.DependencyInjection; namespace AgileConfig.Server.Service.Tests { @@ -16,7 +19,7 @@ public class SysLogServiceTests IFreeSql fsq = null; FreeSqlContext freeSqlContext; - SysLogService service = null; + ISysLogService service = null; [TestInitialize] public void TestInitialize() @@ -28,7 +31,14 @@ public void TestInitialize() .Build(); freeSqlContext = new FreeSqlContext(fsq); - service = new SysLogService(freeSqlContext); + IServiceCollection services = new ServiceCollection(); + services.AddFreeSqlFactory(); + services.AddFreeSqlRepository(); + services.AddBusinessServices(); + + + var serviceProvider = services.BuildServiceProvider(); + service = serviceProvider.GetService(); fsq.Delete().Where("1=1"); Console.WriteLine("TestInitialize"); diff --git a/test/AgileConfig.Server.ServiceTests/sqlserver/ServerNodeServiceTests.cs b/test/AgileConfig.Server.ServiceTests/sqlserver/ServerNodeServiceTests.cs index bf18df40..7b13b63f 100644 --- a/test/AgileConfig.Server.ServiceTests/sqlserver/ServerNodeServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/sqlserver/ServerNodeServiceTests.cs @@ -7,6 +7,9 @@ using FreeSql; using AgileConfig.Server.Data.Entity; using System.Threading.Tasks; +using AgileConfig.Server.Data.Repository.Freesql; +using AgileConfig.Server.IService; +using Microsoft.Extensions.DependencyInjection; namespace AgileConfig.Server.Service.Tests.sqlserver { @@ -15,7 +18,7 @@ public class ServerNodeServiceTests { IFreeSql fsq = null; FreeSqlContext freeSqlContext; - ServerNodeService service = null; + IServerNodeService service = null; [TestInitialize] public void TestInitialize() @@ -27,8 +30,15 @@ public void TestInitialize() .Build(); FluentApi.Config(fsq); freeSqlContext = new FreeSqlContext(fsq); - - service = new ServerNodeService(freeSqlContext); + + IServiceCollection services = new ServiceCollection(); + services.AddFreeSqlFactory(); + services.AddFreeSqlRepository(); + services.AddBusinessServices(); + + + var serviceProvider = services.BuildServiceProvider(); + service = serviceProvider.GetService(); fsq.Delete().Where("1=1"); Console.WriteLine("TestInitialize"); diff --git a/test/AgileConfig.Server.ServiceTests/sqlserver/SysLogServiceTests.cs b/test/AgileConfig.Server.ServiceTests/sqlserver/SysLogServiceTests.cs index 94325df4..88eea163 100644 --- a/test/AgileConfig.Server.ServiceTests/sqlserver/SysLogServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/sqlserver/SysLogServiceTests.cs @@ -7,6 +7,9 @@ using FreeSql; using AgileConfig.Server.Data.Entity; using System.Threading.Tasks; +using AgileConfig.Server.Data.Repository.Freesql; +using AgileConfig.Server.IService; +using Microsoft.Extensions.DependencyInjection; namespace AgileConfig.Server.Service.Tests.sqlserver { @@ -16,7 +19,7 @@ public class SysLogServiceTests IFreeSql fsq = null; FreeSqlContext freeSqlContext; - SysLogService service = null; + ISysLogService service = null; [TestInitialize] public void TestInitialize() @@ -29,7 +32,14 @@ public void TestInitialize() FluentApi.Config(fsq); freeSqlContext = new FreeSqlContext(fsq); - service = new SysLogService(freeSqlContext); + IServiceCollection services = new ServiceCollection(); + services.AddFreeSqlFactory(); + services.AddFreeSqlRepository(); + services.AddBusinessServices(); + + + var serviceProvider = services.BuildServiceProvider(); + service = serviceProvider.GetService(); fsq.Delete().Where("1=1"); Console.WriteLine("TestInitialize");