50 lines
1.4 KiB
C#
50 lines
1.4 KiB
C#
using Hutopy.Infrastructure.Security;
|
|
using Hutopy.Modules.Identity.Contracts;
|
|
using Hutopy.Modules.Tipping.Data;
|
|
using Hutopy.Modules.Tipping.Models;
|
|
|
|
namespace Hutopy.Modules.Tipping.Handlers;
|
|
|
|
[PublicAPI]
|
|
public record struct GetReceivedTipsResponse(
|
|
IEnumerable<TipReceivedModel> Tips);
|
|
|
|
[PublicAPI]
|
|
public class GetReceivedTipsHandler(
|
|
IUserLookup userLookup,
|
|
TippingDbContext dbContext)
|
|
: EndpointWithoutRequest<GetReceivedTipsResponse>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Get("/api/tips");
|
|
Options(o => o.WithTags("Tips"));
|
|
}
|
|
|
|
public override async Task HandleAsync(
|
|
CancellationToken ct)
|
|
{
|
|
List<Tip> tips = await dbContext
|
|
.Tips
|
|
.Where(tip => tip.CreatorId == User.GetUserId())
|
|
.ToListAsync(ct);
|
|
|
|
TipReceivedModel[] result = await Task.WhenAll(
|
|
tips.Select(async tip =>
|
|
{
|
|
UserReference? tipper = await userLookup.GetUserAsync(tip.CreatorId, ct);
|
|
|
|
return new TipReceivedModel(
|
|
tip.Id,
|
|
tip.CreatedAt,
|
|
tip.CreatedBy,
|
|
tipper?.Fullname ?? "Unknown User",
|
|
tip.Amount,
|
|
tip.Currency,
|
|
tip.Message);
|
|
}));
|
|
|
|
await SendOkAsync(new GetReceivedTipsResponse(result), ct);
|
|
}
|
|
}
|