style: 命令不可变化与规范补充

This commit is contained in:
2025-12-02 12:09:46 +08:00
parent 3e01943727
commit 541b75ecd8
13 changed files with 217 additions and 186 deletions

View File

@@ -16,20 +16,15 @@ namespace TakeoutSaaS.AdminApi.Controllers;
/// <summary>
/// 支付记录管理。
/// </summary>
/// <remarks>
/// 初始化控制器。
/// </remarks>
[ApiVersion("1.0")]
[Authorize]
[Route("api/admin/v{version:apiVersion}/payments")]
public sealed class PaymentsController : BaseApiController
public sealed class PaymentsController(IMediator mediator) : BaseApiController
{
private readonly IMediator _mediator;
/// <summary>
/// 初始化控制器。
/// </summary>
public PaymentsController(IMediator mediator)
{
_mediator = mediator;
}
/// <summary>
/// 创建支付记录。
@@ -39,7 +34,7 @@ public sealed class PaymentsController : BaseApiController
[ProducesResponseType(typeof(ApiResponse<PaymentDto>), StatusCodes.Status200OK)]
public async Task<ApiResponse<PaymentDto>> Create([FromBody] CreatePaymentCommand command, CancellationToken cancellationToken)
{
var result = await _mediator.Send(command, cancellationToken);
var result = await mediator.Send(command, cancellationToken);
return ApiResponse<PaymentDto>.Ok(result);
}
@@ -58,7 +53,7 @@ public sealed class PaymentsController : BaseApiController
[FromQuery] bool sortDesc = true,
CancellationToken cancellationToken = default)
{
var result = await _mediator.Send(new SearchPaymentsQuery
var result = await mediator.Send(new SearchPaymentsQuery
{
OrderId = orderId,
Status = status,
@@ -80,7 +75,7 @@ public sealed class PaymentsController : BaseApiController
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
public async Task<ApiResponse<PaymentDto>> Detail(long paymentId, CancellationToken cancellationToken)
{
var result = await _mediator.Send(new GetPaymentByIdQuery { PaymentId = paymentId }, cancellationToken);
var result = await mediator.Send(new GetPaymentByIdQuery { PaymentId = paymentId }, cancellationToken);
return result == null
? ApiResponse<PaymentDto>.Error(ErrorCodes.NotFound, "支付记录不存在")
: ApiResponse<PaymentDto>.Ok(result);
@@ -95,8 +90,11 @@ public sealed class PaymentsController : BaseApiController
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
public async Task<ApiResponse<PaymentDto>> Update(long paymentId, [FromBody] UpdatePaymentCommand command, CancellationToken cancellationToken)
{
command.PaymentId = command.PaymentId == 0 ? paymentId : command.PaymentId;
var result = await _mediator.Send(command, cancellationToken);
if (command.PaymentId == 0)
{
command = command with { PaymentId = paymentId };
}
var result = await mediator.Send(command, cancellationToken);
return result == null
? ApiResponse<PaymentDto>.Error(ErrorCodes.NotFound, "支付记录不存在")
: ApiResponse<PaymentDto>.Ok(result);
@@ -111,7 +109,7 @@ public sealed class PaymentsController : BaseApiController
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
public async Task<ApiResponse<object>> Delete(long paymentId, CancellationToken cancellationToken)
{
var success = await _mediator.Send(new DeletePaymentCommand { PaymentId = paymentId }, cancellationToken);
var success = await mediator.Send(new DeletePaymentCommand { PaymentId = paymentId }, cancellationToken);
return success
? ApiResponse<object>.Ok(null)
: ApiResponse<object>.Error(ErrorCodes.NotFound, "支付记录不存在");