Skip to content

Commit

Permalink
优化要创建空仓储的问题,可使用AdminRepositoryBase<T>创建仓储
Browse files Browse the repository at this point in the history
升级Freesql至最新版本
  • Loading branch information
zhontai committed Feb 21, 2024
1 parent 56b57a2 commit 129dc2b
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 97 deletions.
2 changes: 1 addition & 1 deletion src/hosts/ZhonTai.Host/Configs/appconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
{
"name": "中台Admin",
"code": "admin",
"version": "v5.2.0",
"version": "v8.0.0",
"description": ""
}
]
Expand Down
14 changes: 7 additions & 7 deletions src/hosts/ZhonTai.Host/ZhonTai.Host.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
</ItemGroup>

<ItemGroup Condition="'$(Configuration)'=='Debug'">
<PackageReference Include="FreeSql.Provider.MySql" Version="3.2.811" />
<PackageReference Include="FreeSql.Provider.SqlServer" Version="3.2.811" />
<PackageReference Include="FreeSql.Provider.PostgreSQL" Version="3.2.811" />
<PackageReference Include="FreeSql.Provider.Oracle" Version="3.2.811" />
<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.811" />
<PackageReference Include="FreeSql.Provider.MySqlConnector" Version="3.2.811" />
<PackageReference Include="FreeSql.Provider.MySql" Version="3.2.812" />
<PackageReference Include="FreeSql.Provider.SqlServer" Version="3.2.812" />
<PackageReference Include="FreeSql.Provider.PostgreSQL" Version="3.2.812" />
<PackageReference Include="FreeSql.Provider.Oracle" Version="3.2.812" />
<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.812" />
<PackageReference Include="FreeSql.Provider.MySqlConnector" Version="3.2.812" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Cronos" Version="0.8.2" />
<PackageReference Include="Cronos" Version="0.8.3" />
<PackageReference Include="DotNetCore.CAP.Dashboard" Version="8.0.1" />
<PackageReference Include="DotNetCore.CAP.InMemoryStorage" Version="8.0.1" />
<PackageReference Include="Savorboard.CAP.InMemoryMessageQueue" Version="8.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>5.2.0</Version>
<Version>8.0.0</Version>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using ZhonTai.Admin.Core.Repositories;
using Microsoft.AspNetCore.Identity;
using ZhonTai.Common.Helpers;
using ZhonTai.Admin.Repositories;

namespace ZhonTai.Admin.Core.RegisterModules;

Expand Down Expand Up @@ -70,6 +71,7 @@ static bool Predicate(Type a) => !a.IsDefined(typeof(NonRegisterIOCAttribute), t
//仓储泛型注入
builder.RegisterGeneric(typeof(RepositoryBase<>)).As(typeof(IRepositoryBase<>)).InstancePerLifetimeScope().PropertiesAutowired();
builder.RegisterGeneric(typeof(RepositoryBase<,>)).As(typeof(IRepositoryBase<,>)).InstancePerLifetimeScope().PropertiesAutowired();
builder.RegisterGeneric(typeof(AdminRepositoryBase<>)).InstancePerLifetimeScope().PropertiesAutowired();
}
}
}
45 changes: 23 additions & 22 deletions src/platform/ZhonTai.Admin/Services/Api/ApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using ZhonTai.DynamicApi;
using ZhonTai.DynamicApi.Attributes;
using ZhonTai.Admin.Core.Consts;
using ZhonTai.Admin.Repositories;

namespace ZhonTai.Admin.Services.Api;

Expand All @@ -20,11 +21,11 @@ namespace ZhonTai.Admin.Services.Api;
[DynamicApi(Area = AdminConsts.AreaName)]
public class ApiService : BaseService, IApiService, IDynamicApi
{
private readonly IApiRepository _apiRepository;
private readonly AdminRepositoryBase<ApiEntity> _apiRep;

public ApiService(IApiRepository moduleRepository)
public ApiService(AdminRepositoryBase<ApiEntity> apiRep)
{
_apiRepository = moduleRepository;
_apiRep = apiRep;
}

/// <summary>
Expand All @@ -34,7 +35,7 @@ public ApiService(IApiRepository moduleRepository)
/// <returns></returns>
public async Task<ApiGetOutput> GetAsync(long id)
{
var result = await _apiRepository.GetAsync<ApiGetOutput>(id);
var result = await _apiRep.GetAsync<ApiGetOutput>(id);
return result;
}

Expand All @@ -45,7 +46,7 @@ public async Task<ApiGetOutput> GetAsync(long id)
/// <returns></returns>
public async Task<List<ApiListOutput>> GetListAsync(string key)
{
var data = await _apiRepository
var data = await _apiRep
.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
.OrderBy(a => a.ParentId)
.OrderBy(a => a.Sort)
Expand All @@ -64,7 +65,7 @@ public async Task<PageOutput<ApiEntity>> GetPageAsync(PageInput<ApiGetPageDto> i
{
var key = input.Filter?.Label;

var list = await _apiRepository.Select
var list = await _apiRep.Select
.WhereDynamicFilter(input.DynamicFilter)
.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
.Count(out var total)
Expand All @@ -91,27 +92,27 @@ public async Task<long> AddAsync(ApiAddInput input)
{
var path = input.Path;

var entity = await _apiRepository.Select.DisableGlobalFilter(FilterNames.Delete)
var entity = await _apiRep.Select.DisableGlobalFilter(FilterNames.Delete)
.Where(w => w.Path.Equals(path) && w.IsDeleted).FirstAsync();

if (entity?.Id > 0)
{
Mapper.Map(input, entity);
entity.IsDeleted = false;
entity.Enabled = true;
await _apiRepository.UpdateDiy.DisableGlobalFilter(FilterNames.Delete).SetSource(entity).ExecuteAffrowsAsync();
await _apiRep.UpdateDiy.DisableGlobalFilter(FilterNames.Delete).SetSource(entity).ExecuteAffrowsAsync();

return entity.Id;
}
entity = Mapper.Map<ApiEntity>(input);

if (entity.Sort == 0)
{
var sort = await _apiRepository.Select.DisableGlobalFilter(FilterNames.Delete).Where(a => a.ParentId == input.ParentId).MaxAsync(a => a.Sort);
var sort = await _apiRep.Select.DisableGlobalFilter(FilterNames.Delete).Where(a => a.ParentId == input.ParentId).MaxAsync(a => a.Sort);
entity.Sort = sort + 1;
}

await _apiRepository.InsertAsync(entity);
await _apiRep.InsertAsync(entity);

return entity.Id;
}
Expand All @@ -123,14 +124,14 @@ public async Task<long> AddAsync(ApiAddInput input)
/// <returns></returns>
public async Task UpdateAsync(ApiUpdateInput input)
{
var entity = await _apiRepository.GetAsync(input.Id);
var entity = await _apiRep.GetAsync(input.Id);
if (!(entity?.Id > 0))
{
throw ResultOutput.Exception("接口不存在!");
}

Mapper.Map(input, entity);
await _apiRepository.UpdateAsync(entity);
await _apiRep.UpdateAsync(entity);
}

/// <summary>
Expand All @@ -140,7 +141,7 @@ public async Task UpdateAsync(ApiUpdateInput input)
/// <returns></returns>
public async Task DeleteAsync(long id)
{
await _apiRepository.DeleteAsync(a => a.Id == id);
await _apiRep.DeleteAsync(a => a.Id == id);
}

/// <summary>
Expand All @@ -150,7 +151,7 @@ public async Task DeleteAsync(long id)
/// <returns></returns>
public async Task BatchDeleteAsync(long[] ids)
{
await _apiRepository.DeleteAsync(a => ids.Contains(a.Id));
await _apiRep.DeleteAsync(a => ids.Contains(a.Id));
}

/// <summary>
Expand All @@ -161,7 +162,7 @@ public async Task BatchDeleteAsync(long[] ids)
[HttpDelete]
public async Task SoftDeleteAsync(long id)
{
await _apiRepository.SoftDeleteAsync(id);
await _apiRep.SoftDeleteAsync(id);
}

/// <summary>
Expand All @@ -171,7 +172,7 @@ public async Task SoftDeleteAsync(long id)
/// <returns></returns>
public async Task BatchSoftDeleteAsync(long[] ids)
{
await _apiRepository.SoftDeleteAsync(ids);
await _apiRep.SoftDeleteAsync(ids);
}

/// <summary>
Expand All @@ -186,13 +187,13 @@ public virtual async Task SyncAsync(ApiSyncInput input)

//查询分组下所有模块的api
var groupPaths = input.Apis.FindAll(a => a.ParentPath.IsNull()).Select(a => a.Path);
var groups = await _apiRepository.Select.DisableGlobalFilter(FilterNames.Delete)
var groups = await _apiRep.Select.DisableGlobalFilter(FilterNames.Delete)
.Where(a => a.ParentId == 0 && groupPaths.Contains(a.Path)).ToListAsync();
var groupIds = groups.Select(a => a.Id);
var modules = await _apiRepository.Select.DisableGlobalFilter(FilterNames.Delete)
var modules = await _apiRep.Select.DisableGlobalFilter(FilterNames.Delete)
.Where(a => groupIds.Contains(a.ParentId)).ToListAsync();
var moduleIds = modules.Select(a => a.Id);
var apis = await _apiRepository.Select.DisableGlobalFilter(FilterNames.Delete)
var apis = await _apiRep.Select.DisableGlobalFilter(FilterNames.Delete)
.Where(a=> moduleIds.Contains(a.ParentId)).ToListAsync();

apis = groups.Concat(modules).Concat(apis).ToList();
Expand All @@ -213,7 +214,7 @@ public virtual async Task SyncAsync(ApiSyncInput input)
if (pApis.Count > 0)
{
var insertPApis = Mapper.Map<List<ApiEntity>>(pApis);
insertPApis = await _apiRepository.InsertAsync(insertPApis);
insertPApis = await _apiRep.InsertAsync(insertPApis);
apis.AddRange(insertPApis);
}

Expand All @@ -223,7 +224,7 @@ public virtual async Task SyncAsync(ApiSyncInput input)
if (cApis.Count > 0)
{
var insertCApis = Mapper.Map<List<ApiEntity>>(cApis);
insertCApis = await _apiRepository.InsertAsync(insertCApis);
insertCApis = await _apiRep.InsertAsync(insertCApis);
apis.AddRange(insertCApis);
}

Expand Down Expand Up @@ -301,7 +302,7 @@ public virtual async Task SyncAsync(ApiSyncInput input)
#endregion 修改和禁用

//批量更新
await _apiRepository.UpdateDiy.DisableGlobalFilter(FilterNames.Delete).SetSource(apis)
await _apiRep.UpdateDiy.DisableGlobalFilter(FilterNames.Delete).SetSource(apis)
.UpdateColumns(a => new { a.ParentId, a.Label, a.HttpMethods, a.Description, a.Sort, a.Enabled, a.IsDeleted, a.ModifiedTime })
.ExecuteAffrowsAsync();
}
Expand Down
Loading

0 comments on commit 129dc2b

Please sign in to comment.