优化代码
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 23s
Docker Build & Deploy / Deploy to Production (push) Successful in 6s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s

This commit is contained in:
孙诚
2026-01-04 16:43:32 +08:00
parent ab22325ca7
commit 557d44aed8
11 changed files with 200 additions and 460 deletions

View File

@@ -53,19 +53,15 @@ public class TransactionPeriodicController(
var periodic = await periodicRepository.GetByIdAsync(id);
if (periodic == null)
{
return BaseResponse<TransactionPeriodic>.Fail("周期性账单不存在");
return "周期性账单不存在".Fail<TransactionPeriodic>();
}
return new BaseResponse<TransactionPeriodic>
{
Success = true,
Data = periodic
};
return periodic.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "获取周期性账单详情失败ID: {Id}", id);
return BaseResponse<TransactionPeriodic>.Fail($"获取详情失败: {ex.Message}");
return $"获取详情失败: {ex.Message}".Fail<TransactionPeriodic>();
}
}
@@ -94,20 +90,15 @@ public class TransactionPeriodicController(
var success = await periodicRepository.AddAsync(periodic);
if (!success)
{
return BaseResponse<TransactionPeriodic>.Fail("创建周期性账单失败");
return "创建周期性账单失败".Fail<TransactionPeriodic>();
}
return new BaseResponse<TransactionPeriodic>
{
Success = true,
Data = periodic,
Message = "创建成功"
};
return periodic.Ok("创建成功");
}
catch (Exception ex)
{
logger.LogError(ex, "创建周期性账单失败");
return BaseResponse<TransactionPeriodic>.Fail($"创建失败: {ex.Message}");
return $"创建失败: {ex.Message}".Fail<TransactionPeriodic>();
}
}
@@ -115,14 +106,14 @@ public class TransactionPeriodicController(
/// 更新周期性账单
/// </summary>
[HttpPost]
public async Task<BaseResponse<object>> UpdateAsync([FromBody] UpdatePeriodicRequest request)
public async Task<BaseResponse> UpdateAsync([FromBody] UpdatePeriodicRequest request)
{
try
{
var periodic = await periodicRepository.GetByIdAsync(request.Id);
if (periodic == null)
{
return BaseResponse<object>.Fail("周期性账单不存在");
return "周期性账单不存在".Fail();
}
periodic.PeriodicType = request.PeriodicType;
@@ -140,19 +131,15 @@ public class TransactionPeriodicController(
var success = await periodicRepository.UpdateAsync(periodic);
if (!success)
{
return BaseResponse<object>.Fail("更新周期性账单失败");
return "更新周期性账单失败".Fail();
}
return new BaseResponse<object>
{
Success = true,
Message = "更新成功"
};
return "更新成功".Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "更新周期性账单失败ID: {Id}", request.Id);
return BaseResponse<object>.Fail($"更新失败: {ex.Message}");
return $"更新失败: {ex.Message}".Fail();
}
}
@@ -160,26 +147,22 @@ public class TransactionPeriodicController(
/// 删除周期性账单
/// </summary>
[HttpPost]
public async Task<BaseResponse<object>> DeleteByIdAsync([FromQuery] long id)
public async Task<BaseResponse> DeleteByIdAsync([FromQuery] long id)
{
try
{
var success = await periodicRepository.DeleteAsync(id);
if (!success)
{
return BaseResponse<object>.Fail("删除周期性账单失败");
return "删除周期性账单失败".Fail();
}
return new BaseResponse<object>
{
Success = true,
Message = "删除成功"
};
return "删除成功".Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "删除周期性账单失败ID: {Id}", id);
return BaseResponse<object>.Fail($"删除失败: {ex.Message}");
return $"删除失败: {ex.Message}".Fail();
}
}
@@ -187,14 +170,14 @@ public class TransactionPeriodicController(
/// 启用/禁用周期性账单
/// </summary>
[HttpPost]
public async Task<BaseResponse<object>> ToggleEnabledAsync([FromQuery] long id, [FromQuery] bool enabled)
public async Task<BaseResponse> ToggleEnabledAsync([FromQuery] long id, [FromQuery] bool enabled)
{
try
{
var periodic = await periodicRepository.GetByIdAsync(id);
if (periodic == null)
{
return BaseResponse<object>.Fail("周期性账单不存在");
return "周期性账单不存在".Fail();
}
periodic.IsEnabled = enabled;
@@ -203,19 +186,15 @@ public class TransactionPeriodicController(
var success = await periodicRepository.UpdateAsync(periodic);
if (!success)
{
return BaseResponse<object>.Fail("操作失败");
return "操作失败".Fail();
}
return new BaseResponse<object>
{
Success = true,
Message = enabled ? "已启用" : "已禁用"
};
return (enabled ? "已启用" : "已禁用").Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "启用/禁用周期性账单失败ID: {Id}", id);
return BaseResponse<object>.Fail($"操作失败: {ex.Message}");
return $"操作失败: {ex.Message}".Fail();
}
}
}