91 lines
3.1 KiB
C#
91 lines
3.1 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Tiku.Api.Contracts;
|
|
using Tiku.Application.Commerce;
|
|
using Tiku.Application.Security;
|
|
|
|
namespace Tiku.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Authorize(Policy = TikuPolicies.CurrentTenantMember)]
|
|
[Produces("application/json")]
|
|
[Route("api/commerce")]
|
|
public sealed class CommerceController(
|
|
ICommerceService commerceService,
|
|
ICurrentUser currentUser,
|
|
ICurrentTenant currentTenant) : ControllerBase
|
|
{
|
|
[HttpPost("orders")]
|
|
[EndpointSummary("创建学生端订单")]
|
|
[ProducesResponseType<CommerceOrderItem>(StatusCodes.Status200OK)]
|
|
public async Task<ActionResult<CommerceOrderItem>> CreateOrder(
|
|
CreateCommerceOrderDto request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return Ok(await commerceService.CreateOrderAsync(
|
|
ResolveActor(),
|
|
request.ToCommand(),
|
|
cancellationToken));
|
|
}
|
|
|
|
[HttpGet("orders")]
|
|
[EndpointSummary("查询当前用户订单")]
|
|
[ProducesResponseType<CommerceOrderList>(StatusCodes.Status200OK)]
|
|
public async Task<ActionResult<CommerceOrderList>> Orders(
|
|
[FromQuery] CommerceOrderQueryDto query,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return Ok(await commerceService.GetOrdersAsync(
|
|
ResolveActor(),
|
|
new CommerceOrderQuery(query.Limit, query.Status),
|
|
cancellationToken));
|
|
}
|
|
|
|
[HttpGet("orders/{orderNo}")]
|
|
[EndpointSummary("查询当前用户订单详情")]
|
|
[ProducesResponseType<CommerceOrderItem>(StatusCodes.Status200OK)]
|
|
public async Task<ActionResult<CommerceOrderItem>> Order(
|
|
string orderNo,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return Ok(await commerceService.GetOrderAsync(
|
|
ResolveActor(),
|
|
orderNo,
|
|
cancellationToken));
|
|
}
|
|
|
|
[HttpPost("payments")]
|
|
[EndpointSummary("创建订单支付")]
|
|
[ProducesResponseType<CommercePaymentItem>(StatusCodes.Status200OK)]
|
|
public async Task<ActionResult<CommercePaymentItem>> CreatePayment(
|
|
CreateCommercePaymentDto request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return Ok(await commerceService.CreatePaymentAsync(
|
|
ResolveActor(),
|
|
request.ToCommand(),
|
|
cancellationToken));
|
|
}
|
|
|
|
[HttpGet("entitlements/current")]
|
|
[EndpointSummary("查询当前用户权益")]
|
|
[ProducesResponseType<CurrentEntitlementItem>(StatusCodes.Status200OK)]
|
|
public async Task<ActionResult<CurrentEntitlementItem>> CurrentEntitlement(
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return Ok(await commerceService.GetCurrentEntitlementAsync(
|
|
ResolveActor(),
|
|
cancellationToken));
|
|
}
|
|
|
|
private CommerceActor ResolveActor()
|
|
{
|
|
if (currentTenant.TenantId is null || currentUser.UserId is null)
|
|
{
|
|
throw new CommerceException("Current commerce actor was not resolved.", "commerce_access_denied");
|
|
}
|
|
|
|
return new CommerceActor(currentTenant.TenantId.Value, currentUser.UserId.Value);
|
|
}
|
|
}
|