using System.Text.Json; using FictionArchive.Service.ReportingService.Models.Database; using FictionArchive.Service.ReportingService.Models.DTOs; using FictionArchive.Service.ReportingService.Services; using HotChocolate; using HotChocolate.Data; using Microsoft.EntityFrameworkCore; namespace FictionArchive.Service.ReportingService.GraphQL; public class Query { [UseProjection] [UseFiltering] [UseSorting] [GraphQLName("reportingJobs")] public IQueryable GetReportingJobs(ReportingServiceDbContext dbContext) => dbContext.Jobs.Include(j => j.History); [GraphQLName("reportingJob")] public async Task GetReportingJob(Guid id, ReportingServiceDbContext dbContext) { var job = await dbContext.Jobs .Include(j => j.History.OrderBy(h => h.Timestamp)) .FirstOrDefaultAsync(j => j.Id == id); if (job == null) return null; return new JobDto { Id = job.Id, JobType = job.JobType, Status = job.Status, CurrentStep = job.CurrentStep, ErrorMessage = job.ErrorMessage, Metadata = job.Metadata != null ? JsonSerializer.Deserialize>(job.Metadata.RootElement.GetRawText()) : null, History = job.History.Select(h => new JobHistoryEntryDto { FromState = h.FromState, ToState = h.ToState, Message = h.Message, Error = h.Error, Timestamp = h.Timestamp }).ToList(), CreatedTime = job.CreatedTime, UpdatedTime = job.UpdatedTime, CompletedTime = job.CompletedTime }; } }