forked from gongxuegit/tiku-backend.net
feat: add student points endpoints
This commit is contained in:
48
Tiku.Api/Contracts/PointDtos.cs
Normal file
48
Tiku.Api/Contracts/PointDtos.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Tiku.Application.Points;
|
||||
|
||||
namespace Tiku.Api.Contracts;
|
||||
|
||||
public sealed class PointQueryDto
|
||||
{
|
||||
[Range(1, 200)]
|
||||
public int? Limit { get; set; }
|
||||
|
||||
[StringLength(32)]
|
||||
public string? Status { get; set; }
|
||||
|
||||
public Guid? RegionId { get; set; }
|
||||
|
||||
public PointLimitQuery ToQuery()
|
||||
{
|
||||
return new PointLimitQuery(Limit, Status, RegionId);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ClaimPointTaskDto
|
||||
{
|
||||
[Required]
|
||||
[StringLength(100)]
|
||||
public string TaskKey { get; set; } = string.Empty;
|
||||
|
||||
[StringLength(100)]
|
||||
public string? SourceType { get; set; }
|
||||
|
||||
public Guid? SourceId { get; set; }
|
||||
|
||||
public ClaimPointTaskCommand ToCommand()
|
||||
{
|
||||
return new ClaimPointTaskCommand(TaskKey, SourceType, SourceId);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class CreatePointExchangeOrderDto
|
||||
{
|
||||
[Required]
|
||||
public Guid ItemId { get; set; }
|
||||
|
||||
public CreatePointExchangeOrderCommand ToCommand()
|
||||
{
|
||||
return new CreatePointExchangeOrderCommand(ItemId);
|
||||
}
|
||||
}
|
||||
100
Tiku.Api/Controllers/PointsController.cs
Normal file
100
Tiku.Api/Controllers/PointsController.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Tiku.Api.Contracts;
|
||||
using Tiku.Application.Points;
|
||||
using Tiku.Application.Security;
|
||||
|
||||
namespace Tiku.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Authorize(Policy = TikuPolicies.CurrentTenantMember)]
|
||||
[Produces("application/json")]
|
||||
[Route("api/points")]
|
||||
public sealed class PointsController(
|
||||
IPointService pointService,
|
||||
ICurrentUser currentUser,
|
||||
ICurrentTenant currentTenant) : ControllerBase
|
||||
{
|
||||
[HttpGet("summary")]
|
||||
[EndpointSummary("查询当前用户积分摘要")]
|
||||
[ProducesResponseType<PointSummaryItem>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<PointSummaryItem>> Summary(CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await pointService.GetSummaryAsync(ResolveActor(), cancellationToken));
|
||||
}
|
||||
|
||||
[HttpGet("tasks")]
|
||||
[EndpointSummary("查询当前可领取积分任务")]
|
||||
[ProducesResponseType<PointList<PointTaskItem>>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<PointList<PointTaskItem>>> Tasks(
|
||||
[FromQuery] PointQueryDto query,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await pointService.GetTasksAsync(
|
||||
ResolveActor(),
|
||||
query.ToQuery(),
|
||||
cancellationToken));
|
||||
}
|
||||
|
||||
[HttpPost("tasks/claim")]
|
||||
[EndpointSummary("领取积分任务奖励")]
|
||||
[ProducesResponseType<PointClaimItem>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<PointClaimItem>> ClaimTask(
|
||||
ClaimPointTaskDto request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await pointService.ClaimTaskAsync(
|
||||
ResolveActor(),
|
||||
request.ToCommand(),
|
||||
cancellationToken));
|
||||
}
|
||||
|
||||
[HttpGet("exchange-items")]
|
||||
[EndpointSummary("查询积分兑换项")]
|
||||
[ProducesResponseType<PointList<PointExchangeItemDto>>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<PointList<PointExchangeItemDto>>> ExchangeItems(
|
||||
[FromQuery] PointQueryDto query,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await pointService.GetExchangeItemsAsync(
|
||||
ResolveActor(),
|
||||
query.ToQuery(),
|
||||
cancellationToken));
|
||||
}
|
||||
|
||||
[HttpPost("exchange-orders")]
|
||||
[EndpointSummary("创建积分兑换订单")]
|
||||
[ProducesResponseType<PointExchangeOrderItem>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<PointExchangeOrderItem>> CreateExchangeOrder(
|
||||
CreatePointExchangeOrderDto request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await pointService.CreateExchangeOrderAsync(
|
||||
ResolveActor(),
|
||||
request.ToCommand(),
|
||||
cancellationToken));
|
||||
}
|
||||
|
||||
[HttpGet("exchange-orders")]
|
||||
[EndpointSummary("查询当前用户积分兑换订单")]
|
||||
[ProducesResponseType<PointList<PointExchangeOrderItem>>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<PointList<PointExchangeOrderItem>>> ExchangeOrders(
|
||||
[FromQuery] PointQueryDto query,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await pointService.GetExchangeOrdersAsync(
|
||||
ResolveActor(),
|
||||
query.ToQuery(),
|
||||
cancellationToken));
|
||||
}
|
||||
|
||||
private PointActor ResolveActor()
|
||||
{
|
||||
if (currentTenant.TenantId is null || currentUser.UserId is null)
|
||||
{
|
||||
throw new PointException("Current point actor was not resolved.", "point_access_denied");
|
||||
}
|
||||
|
||||
return new PointActor(currentTenant.TenantId.Value, currentUser.UserId.Value);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using Tiku.Application.Assets;
|
||||
using Tiku.Application.Auth;
|
||||
using Tiku.Application.Commerce;
|
||||
using Tiku.Application.Content;
|
||||
using Tiku.Application.Points;
|
||||
using Tiku.Application.Storage;
|
||||
using Tiku.Infrastructure.Content;
|
||||
using Tiku.Infrastructure.Learning;
|
||||
@@ -183,6 +184,16 @@ public sealed class ExceptionHandlingMiddleware(
|
||||
return;
|
||||
}
|
||||
|
||||
if (exception is PointException pointException)
|
||||
{
|
||||
await WriteProblemAsync(
|
||||
context,
|
||||
pointException.Message,
|
||||
PointStatusCode(pointException.Code),
|
||||
pointException.Code);
|
||||
return;
|
||||
}
|
||||
|
||||
if (exception is PaymentProviderException paymentProviderException)
|
||||
{
|
||||
await WriteProblemAsync(
|
||||
@@ -342,4 +353,16 @@ public sealed class ExceptionHandlingMiddleware(
|
||||
_ => StatusCodes.Status400BadRequest
|
||||
};
|
||||
}
|
||||
|
||||
private static int PointStatusCode(string code)
|
||||
{
|
||||
return code switch
|
||||
{
|
||||
"point_access_denied" or "tenant_access_denied" => StatusCodes.Status403Forbidden,
|
||||
"point_task_not_found" or "point_exchange_item_not_found" => StatusCodes.Status404NotFound,
|
||||
"point_task_claim_limit_reached" or "insufficient_points" or "point_exchange_item_sold_out" =>
|
||||
StatusCodes.Status409Conflict,
|
||||
_ => StatusCodes.Status400BadRequest
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user