62 lines
2.2 KiB
C#

using System;
using System.Threading.Tasks;
using ComplianceNFs.Core.Entities;
using ComplianceNFs.Core.Ports;
using Npgsql;
using Newtonsoft.Json;
using System.Numerics;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
namespace ComplianceNFs.Infrastructure.Repositories
{
public class AttachmentRepository : IAttachmentRepository
{
private readonly ComplianceNFsDbContext _dbContext;
private readonly ILogger<AttachmentRepository> _logger;
public AttachmentRepository(ComplianceNFsDbContext dbContext, ILogger<AttachmentRepository> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public async Task SaveRawAsync(EnergyInvoice invoice)
{
try
{
await _dbContext.EnergyInvoices.AddAsync(invoice);
await _dbContext.SaveChangesAsync();
_logger.LogInformation("Saved raw invoice {InvoiceId} to EnergyInvoices table.", invoice.InvoiceId);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error saving raw invoice {InvoiceId} to EnergyInvoices table.", invoice.InvoiceId);
throw;
}
}
public async Task UpdateMatchAsync(int invoiceId, BigInteger matchedCodTE, InvoiceStatus status, string notes)
{
try
{
var invoice = await _dbContext.EnergyInvoices.FirstOrDefaultAsync(e => e.InvoiceId == invoiceId);
if (invoice == null)
{
throw new InvalidOperationException($"Invoice with ID {invoiceId} not found.");
}
invoice.MatchedCodTE = matchedCodTE;
invoice.Status = status;
invoice.DiscrepancyNotes = notes;
await _dbContext.SaveChangesAsync();
_logger.LogInformation("Updated match for invoice {InvoiceId}.", invoiceId);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating match for invoice {InvoiceId}.", invoiceId);
throw;
}
}
}
}