⚡ 优化代码生成删除操作
This commit is contained in:
parent
925440716b
commit
bfe8b68a94
@ -1,51 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ZR.Infrastructure.ModelBinder
|
||||
{
|
||||
public class CommaSeparatedArrayModelBinder<T> : IModelBinder
|
||||
{
|
||||
public Task BindModelAsync(ModelBindingContext bindingContext)
|
||||
{
|
||||
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
|
||||
if (valueProviderResult == ValueProviderResult.None)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
var value = valueProviderResult.FirstValue;
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var array = value.Split(',').Select(x => (T)Convert.ChangeType(x, typeof(T))).ToArray();
|
||||
bindingContext.Result = ModelBindingResult.Success(array);
|
||||
}
|
||||
catch
|
||||
{
|
||||
bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, "Invalid value format");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public class CommaSeparatedArrayModelBinderProvider<T> : IModelBinderProvider
|
||||
{
|
||||
public IModelBinder? GetBinder(ModelBinderProviderContext context)
|
||||
{
|
||||
if (context.Metadata.ModelType == typeof(T[]))
|
||||
{
|
||||
return new BinderTypeModelBinder(typeof(CommaSeparatedArrayModelBinder<T>));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -8,5 +8,4 @@ global using Mapster;
|
||||
global using Infrastructure.Extensions;
|
||||
global using Infrastructure.Controllers;
|
||||
global using ZR.ServiceCore.Middleware;
|
||||
global using ZR.ServiceCore.Services;
|
||||
global using ZR.Infrastructure.ModelBinder;
|
||||
global using ZR.ServiceCore.Services;
|
||||
@ -16,12 +16,7 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Host.UseNLog();
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllers(options =>
|
||||
{
|
||||
options.ModelBinderProviders.Insert(0, new CommaSeparatedArrayModelBinderProvider<string>());
|
||||
options.ModelBinderProviders.Insert(0, new CommaSeparatedArrayModelBinderProvider<int>());
|
||||
options.ModelBinderProviders.Insert(0, new CommaSeparatedArrayModelBinderProvider<long>());
|
||||
});
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
@ -86,8 +86,8 @@ $if(replaceDto.ShowBtnAdd)
|
||||
|
||||
return SUCCESS(response);
|
||||
}
|
||||
$end
|
||||
|
||||
$end
|
||||
$if(replaceDto.ShowBtnEdit)
|
||||
/// <summary>
|
||||
/// 更新${genTable.FunctionName}
|
||||
@ -103,24 +103,24 @@ $if(replaceDto.ShowBtnEdit)
|
||||
|
||||
return ToResponse(response);
|
||||
}
|
||||
$end
|
||||
|
||||
$end
|
||||
$if(replaceDto.ShowBtnDelete || replaceDto.ShowBtnMultiDel)
|
||||
/// <summary>
|
||||
/// 删除${genTable.FunctionName}
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpDelete("{ids}")]
|
||||
[HttpDelete("delete/{ids}")]
|
||||
[ActionPermissionFilter(Permission = "${replaceDto.PermissionPrefix}:delete")]
|
||||
[Log(Title = "${genTable.FunctionName}", BusinessType = BusinessType.DELETE)]
|
||||
public IActionResult Delete${replaceDto.ModelTypeName}([ModelBinder(typeof(CommaSeparatedArrayModelBinder<${replaceDto.PKType}>))] ${replaceDto.PKType}[] ids)
|
||||
public IActionResult Delete${replaceDto.ModelTypeName}([FromRoute]string ids)
|
||||
{
|
||||
if (ids.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }
|
||||
var idArr = Tools.SplitAndConvert<${replaceDto.PKType}>(ids);
|
||||
|
||||
return ToResponse(_${replaceDto.ModelTypeName}Service.Delete(ids$if(replaceDto.enableLog), "删除${genTable.FunctionName}"$end));
|
||||
return ToResponse(_${replaceDto.ModelTypeName}Service.Delete(idArr$if(replaceDto.enableLog), "删除${genTable.FunctionName}"$end));
|
||||
}
|
||||
$end
|
||||
|
||||
$end
|
||||
$if(replaceDto.ShowBtnExport)
|
||||
/// <summary>
|
||||
/// 导出${genTable.FunctionName}
|
||||
@ -141,8 +141,8 @@ $if(replaceDto.ShowBtnExport)
|
||||
var result = ExportExcelMini(list, "${genTable.FunctionName}", "${genTable.FunctionName}");
|
||||
return ExportExcel(result.Item2, result.Item1);
|
||||
}
|
||||
$end
|
||||
|
||||
$end
|
||||
$if(replaceDto.ShowBtnTruncate)
|
||||
/// <summary>
|
||||
/// 清空${genTable.FunctionName}
|
||||
@ -159,8 +159,8 @@ $if(replaceDto.ShowBtnTruncate)
|
||||
}
|
||||
return SUCCESS(_${replaceDto.ModelTypeName}Service.Truncate${replaceDto.ModelTypeName}());
|
||||
}
|
||||
$end
|
||||
|
||||
$end
|
||||
$if(replaceDto.ShowBtnImport)
|
||||
/// <summary>
|
||||
/// 导入
|
||||
@ -193,8 +193,8 @@ $if(replaceDto.ShowBtnImport)
|
||||
var result = DownloadImportTemplate(new List<${replaceDto.ModelTypeName}Dto>() { }, "${replaceDto.ModelTypeName}");
|
||||
return ExportExcel(result.Item2, result.Item1);
|
||||
}
|
||||
$end
|
||||
|
||||
$end
|
||||
$if(showCustomInput)
|
||||
/// <summary>
|
||||
/// 保存排序
|
||||
|
||||
@ -80,7 +80,7 @@ $if(replaceDto.ShowBtnDelete || replaceDto.ShowBtnMultiDel)
|
||||
*/
|
||||
export function del${genTable.BusinessName}(pid) {
|
||||
return request({
|
||||
url: '${genTable.ModuleName}/${genTable.BusinessName}/' + pid,
|
||||
url: '${genTable.ModuleName}/${genTable.BusinessName}/delete/' + pid,
|
||||
method: 'delete'
|
||||
})
|
||||
}
|
||||
|
||||
@ -29,6 +29,18 @@ namespace ZR.Common
|
||||
int[] infoIdss = Array.ConvertAll(strIds, s => int.Parse(s));
|
||||
return infoIdss;
|
||||
}
|
||||
public static T[] SplitAndConvert<T>(string input, char split = ',')
|
||||
{
|
||||
if (string.IsNullOrEmpty(input)) { return Array.Empty<T>(); }
|
||||
string[] parts = input.Split(split, (char)StringSplitOptions.RemoveEmptyEntries);
|
||||
T[] result = Array.ConvertAll(parts, s => (T)Convert.ChangeType(s, typeof(T)));
|
||||
//for (int i = 0; i < parts.Length; i++)
|
||||
//{
|
||||
// result[i] = (T)Convert.ChangeType(parts[i], typeof(T));
|
||||
//}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据日期获取星期几
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user