using Extrudex.Domain.Entities; using Extrudex.Domain.Interfaces; using Extrudex.Infrastructure.Data; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace Extrudex.Infrastructure.Services; /// /// EF Core–backed implementation of the filament usage service. /// Persists usage records to the database and provides query methods /// for retrieving usage by print job or spool. /// public class FilamentUsageService : IFilamentUsageService { private readonly ExtrudexDbContext _dbContext; private readonly ILogger _logger; public FilamentUsageService( ExtrudexDbContext dbContext, ILogger logger) { _dbContext = dbContext; _logger = logger; } /// public async Task RecordUsageAsync( Guid printJobId, Guid spoolId, Guid printerId, decimal gramsUsed, decimal mmExtruded, string? notes = null, CancellationToken cancellationToken = default) { var usage = new FilamentUsage { PrintJobId = printJobId, SpoolId = spoolId, PrinterId = printerId, GramsUsed = gramsUsed, MmExtruded = mmExtruded, RecordedAt = DateTime.UtcNow, Notes = notes }; _dbContext.FilamentUsages.Add(usage); await _dbContext.SaveChangesAsync(cancellationToken); _logger.LogInformation( "Recorded filament usage: {Grams}g / {Mm}mm for print job {JobId} on spool {SpoolId}", gramsUsed, mmExtruded, printJobId, spoolId); return usage; } /// public async Task> GetByPrintJobAsync( Guid printJobId, CancellationToken cancellationToken = default) { return await _dbContext.FilamentUsages .Where(u => u.PrintJobId == printJobId) .OrderByDescending(u => u.RecordedAt) .ToListAsync(cancellationToken); } /// public async Task> GetBySpoolAsync( Guid spoolId, CancellationToken cancellationToken = default) { return await _dbContext.FilamentUsages .Where(u => u.SpoolId == spoolId) .OrderByDescending(u => u.RecordedAt) .ToListAsync(cancellationToken); } }