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}/products")]
public sealed class ProductsController : BaseApiController
public sealed class ProductsController(IMediator mediator) : BaseApiController
{
private readonly IMediator _mediator;
/// <summary>
/// 初始化控制器。
/// </summary>
public ProductsController(IMediator mediator)
{
_mediator = mediator;
}
/// <summary>
/// 创建商品。
@@ -39,7 +34,7 @@ public sealed class ProductsController : BaseApiController
[ProducesResponseType(typeof(ApiResponse<ProductDto>), StatusCodes.Status200OK)]
public async Task<ApiResponse<ProductDto>> Create([FromBody] CreateProductCommand command, CancellationToken cancellationToken)
{
var result = await _mediator.Send(command, cancellationToken);
var result = await mediator.Send(command, cancellationToken);
return ApiResponse<ProductDto>.Ok(result);
}
@@ -59,7 +54,7 @@ public sealed class ProductsController : BaseApiController
[FromQuery] bool sortDesc = true,
CancellationToken cancellationToken = default)
{
var result = await _mediator.Send(new SearchProductsQuery
var result = await mediator.Send(new SearchProductsQuery
{
StoreId = storeId,
CategoryId = categoryId,
@@ -82,7 +77,7 @@ public sealed class ProductsController : BaseApiController
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
public async Task<ApiResponse<ProductDto>> Detail(long productId, CancellationToken cancellationToken)
{
var result = await _mediator.Send(new GetProductByIdQuery { ProductId = productId }, cancellationToken);
var result = await mediator.Send(new GetProductByIdQuery { ProductId = productId }, cancellationToken);
return result == null
? ApiResponse<ProductDto>.Error(ErrorCodes.NotFound, "商品不存在")
: ApiResponse<ProductDto>.Ok(result);
@@ -97,8 +92,11 @@ public sealed class ProductsController : BaseApiController
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
public async Task<ApiResponse<ProductDto>> Update(long productId, [FromBody] UpdateProductCommand command, CancellationToken cancellationToken)
{
command.ProductId = command.ProductId == 0 ? productId : command.ProductId;
var result = await _mediator.Send(command, cancellationToken);
if (command.ProductId == 0)
{
command = command with { ProductId = productId };
}
var result = await mediator.Send(command, cancellationToken);
return result == null
? ApiResponse<ProductDto>.Error(ErrorCodes.NotFound, "商品不存在")
: ApiResponse<ProductDto>.Ok(result);
@@ -113,7 +111,7 @@ public sealed class ProductsController : BaseApiController
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
public async Task<ApiResponse<object>> Delete(long productId, CancellationToken cancellationToken)
{
var success = await _mediator.Send(new DeleteProductCommand { ProductId = productId }, cancellationToken);
var success = await mediator.Send(new DeleteProductCommand { ProductId = productId }, cancellationToken);
return success
? ApiResponse<object>.Ok(null)
: ApiResponse<object>.Error(ErrorCodes.NotFound, "商品不存在");