using SurrealDb.Net; using TarninaHub.Application.Abstractions; using TarninaHub.Application.Dtos; namespace TarninaHub.Infrastructure.Persistence; public sealed class AdminService : IAdminService { private const int MaxSubmissions = 200; private readonly ISurrealDbClient _client; private readonly ISubmissionService _submissions; private readonly IKnowledgeItemService _knowledgeItems; public AdminService( ISurrealDbClient client, ISubmissionService submissions, IKnowledgeItemService knowledgeItems) { _client = client; _submissions = submissions; _knowledgeItems = knowledgeItems; } public async Task> ListSubmissionsAsync( string? status, string? challengeId, CancellationToken cancellationToken) { var page = await _submissions.ListAsync( viewerId: string.Empty, isStaff: true, status, page: 1, pageSize: MaxSubmissions, cancellationToken); return string.IsNullOrWhiteSpace(challengeId) ? page.Items : page.Items.Where(item => item.ChallengeId == challengeId).ToList(); } public async Task VerifyKnowledgeItemAsync( string knowledgeItemId, VerifyKnowledgeItemRequest request, CancellationToken cancellationToken) { var (table, id) = SurrealRecordId.Split(knowledgeItemId); var response = await _client.RawQuery( "UPDATE type::record($table, $id) SET published = $published, updated_at = time::now();", new Dictionary { ["table"] = table, ["id"] = id, ["published"] = request.Verified, }, cancellationToken); response.EnsureAllOks(); return await _knowledgeItems.GetAsync(knowledgeItemId, cancellationToken); } public async Task GetMetricsAsync(CancellationToken cancellationToken) { var response = await _client.RawQuery( "SELECT count() AS count FROM submission GROUP ALL; " + "SELECT count() AS count FROM problem GROUP ALL; " + "SELECT count() AS count FROM idea GROUP ALL; " + "SELECT count() AS count FROM innovation GROUP ALL; " + "SELECT count() AS count FROM knowledge_item GROUP ALL; " + "SELECT count() AS count FROM user GROUP ALL; " + "SELECT count() AS count FROM grant_call WHERE status = 'open' GROUP ALL; " + "SELECT count() AS count FROM application GROUP ALL;", new Dictionary(), cancellationToken); response.EnsureAllOks(); return new AdminMetricsResponse( Count(response, 0), Count(response, 1), Count(response, 2), Count(response, 3), Count(response, 4), Count(response, 5), Count(response, 6), Count(response, 7)); } private static int Count(SurrealDb.Net.Models.Response.SurrealDbResponse response, int index) { var rows = response.GetValue>(index) ?? []; return rows.Count > 0 ? rows[0].Count : 0; } }