using Application.Abstractions.Data; using Application.Abstractions.Messaging; using Microsoft.EntityFrameworkCore; using SharedKernel.Results; namespace Application.Features.Api.Developers.Apps.GetApp; internal sealed class Handler(IAppDbContext db) : IQueryHandler> { public async Task> Handle(Query request, CancellationToken ct) { var app = await db.ApiApplication .Where(c => c.ID == request.AppID && c.OwnerMemberID == request.OwnerMemberID) .FirstOrDefaultAsync(ct); if (app is null) { return Result.Failure(Error.NotFound("App.NotFound", "App not found.")); } var scopes = await db.ApiApplicationScope .Where(c => c.ApplicationID == app.ID) .Select(c => c.Scope) .ToListAsync(ct); var credentials = await db.ApiCredential .Where(c => c.ApplicationID == app.ID) .OrderByDescending(c => c.CreatedAt) .Select(c => new CredentialInfo(c.ClientID, c.SecretHint, c.Status, c.CreatedAt, c.LastUsedAt)) .ToListAsync(ct); return Result.Success(new Response( app.ID, app.Name, app.Description, app.HomepageUrl, app.LogoPath, app.Status, scopes, credentials, app.CreatedAt )); } }