using System.Collections.Generic; using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using TakeoutSaaS.Application.App.Stores.Commands; using TakeoutSaaS.Application.App.Stores.Dto; using TakeoutSaaS.Application.App.Stores.Queries; using TakeoutSaaS.Module.Authorization.Attributes; using TakeoutSaaS.Shared.Abstractions.Constants; using TakeoutSaaS.Shared.Abstractions.Results; using TakeoutSaaS.Shared.Web.Api; namespace TakeoutSaaS.AdminApi.Controllers; /// /// 门店桌台区域管理。 /// [ApiVersion("1.0")] [Authorize] [Route("api/admin/v{version:apiVersion}/stores/{storeId:long}/table-areas")] public sealed class StoreTableAreasController(IMediator mediator) : BaseApiController { /// /// 查询区域列表。 /// [HttpGet] [PermissionAuthorize("store-table-area:read")] [ProducesResponseType(typeof(ApiResponse>), StatusCodes.Status200OK)] public async Task>> List(long storeId, CancellationToken cancellationToken) { var result = await mediator.Send(new ListStoreTableAreasQuery { StoreId = storeId }, cancellationToken); return ApiResponse>.Ok(result); } /// /// 创建区域。 /// [HttpPost] [PermissionAuthorize("store-table-area:create")] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status200OK)] public async Task> Create(long storeId, [FromBody] CreateStoreTableAreaCommand command, CancellationToken cancellationToken) { if (command.StoreId == 0) { command = command with { StoreId = storeId }; } var result = await mediator.Send(command, cancellationToken); return ApiResponse.Ok(result); } /// /// 更新区域。 /// [HttpPut("{areaId:long}")] [PermissionAuthorize("store-table-area:update")] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)] public async Task> Update(long storeId, long areaId, [FromBody] UpdateStoreTableAreaCommand command, CancellationToken cancellationToken) { if (command.StoreId == 0 || command.AreaId == 0) { command = command with { StoreId = storeId, AreaId = areaId }; } var result = await mediator.Send(command, cancellationToken); return result == null ? ApiResponse.Error(ErrorCodes.NotFound, "桌台区域不存在") : ApiResponse.Ok(result); } /// /// 删除区域。 /// [HttpDelete("{areaId:long}")] [PermissionAuthorize("store-table-area:delete")] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)] public async Task> Delete(long storeId, long areaId, CancellationToken cancellationToken) { var success = await mediator.Send(new DeleteStoreTableAreaCommand { StoreId = storeId, AreaId = areaId }, cancellationToken); return success ? ApiResponse.Ok(null) : ApiResponse.Error(ErrorCodes.NotFound, "桌台区域不存在或不可删除"); } }