Handler.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.EntityFrameworkCore;
  4. using SharedKernel.Results;
  5. namespace Application.Features.Api.Developers.Apps.GetApp;
  6. internal sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Result<Response>>
  7. {
  8. public async Task<Result<Response>> Handle(Query request, CancellationToken ct)
  9. {
  10. var app = await db.ApiApplication
  11. .Where(c => c.ID == request.AppID && c.OwnerMemberID == request.OwnerMemberID)
  12. .FirstOrDefaultAsync(ct);
  13. if (app is null)
  14. {
  15. return Result.Failure<Response>(Error.NotFound("App.NotFound", "App not found."));
  16. }
  17. var scopes = await db.ApiApplicationScope
  18. .Where(c => c.ApplicationID == app.ID)
  19. .Select(c => c.Scope)
  20. .ToListAsync(ct);
  21. var credentials = await db.ApiCredential
  22. .Where(c => c.ApplicationID == app.ID)
  23. .OrderByDescending(c => c.CreatedAt)
  24. .Select(c => new CredentialInfo(c.ClientID, c.SecretHint, c.Status, c.CreatedAt, c.LastUsedAt))
  25. .ToListAsync(ct);
  26. return Result.Success(new Response(
  27. app.ID, app.Name, app.Description, app.HomepageUrl, app.LogoPath, app.Status,
  28. scopes, credentials, app.CreatedAt
  29. ));
  30. }
  31. }