2026-07-24 17:35:34 -03:00
|
|
|
using Data.SeaHavenIndustries;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-08-06 09:47:34 -03:00
|
|
|
using SeaHaven.DataServices.Helpers;
|
2026-07-24 17:35:34 -03:00
|
|
|
using SeaHaven.DataServices.Interfaces;
|
|
|
|
|
|
|
|
|
|
namespace SeaHaven.DataServices.Implementation
|
|
|
|
|
{
|
|
|
|
|
public class WorkOrderMediaDataService : IWorkOrderMediaDataService
|
|
|
|
|
{
|
|
|
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
|
|
|
|
|
|
public WorkOrderMediaDataService(ApplicationDbContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-06 09:47:34 -03:00
|
|
|
public Task<WorkOrder?> GetWorkOrderForMediaAuthAsync(
|
|
|
|
|
int workOrderId,
|
|
|
|
|
int? accountId,
|
|
|
|
|
CancellationToken cancellationToken)
|
2026-08-06 10:26:04 -03:00
|
|
|
{
|
|
|
|
|
var query = WorkOrderBoardQueryFilters.ApplyBaseScope(_context.workOrders.AsNoTracking());
|
|
|
|
|
if (accountId.HasValue)
|
|
|
|
|
query = WorkOrderBoardQueryFilters.ApplyAccountScope(query, accountId.Value);
|
|
|
|
|
|
|
|
|
|
return query.FirstOrDefaultAsync(w => w.Id == workOrderId, cancellationToken);
|
|
|
|
|
}
|
2026-08-06 09:47:34 -03:00
|
|
|
|
|
|
|
|
public Task<WorkOrder?> GetTrackedWorkOrderAsync(
|
|
|
|
|
int workOrderId,
|
|
|
|
|
int? accountId,
|
|
|
|
|
CancellationToken cancellationToken)
|
2026-08-06 10:26:04 -03:00
|
|
|
{
|
|
|
|
|
var query = WorkOrderBoardQueryFilters.ApplyBaseScope(_context.workOrders);
|
|
|
|
|
if (accountId.HasValue)
|
|
|
|
|
query = WorkOrderBoardQueryFilters.ApplyAccountScope(query, accountId.Value);
|
|
|
|
|
|
|
|
|
|
return query.FirstOrDefaultAsync(w => w.Id == workOrderId, cancellationToken);
|
|
|
|
|
}
|
2026-07-24 17:35:34 -03:00
|
|
|
|
2026-09-08 11:19:41 -03:00
|
|
|
public Task<WorkOrderAttachments?> GetAttachmentForReadAsync(
|
|
|
|
|
int mediaId,
|
|
|
|
|
int workOrderId,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
=> _context.workOrderAttachments
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.FirstOrDefaultAsync(
|
|
|
|
|
a => a.Id == mediaId && a.WorkorderId == workOrderId && a.IsDeleted != true,
|
|
|
|
|
cancellationToken);
|
|
|
|
|
|
2026-07-24 17:35:34 -03:00
|
|
|
public Task<WorkOrderAttachments?> GetTrackedAttachmentAsync(int mediaId, int workOrderId, CancellationToken cancellationToken)
|
|
|
|
|
=> _context.workOrderAttachments.FirstOrDefaultAsync(
|
|
|
|
|
a => a.Id == mediaId && a.WorkorderId == workOrderId && a.IsDeleted != true,
|
|
|
|
|
cancellationToken);
|
|
|
|
|
|
|
|
|
|
public void TrackAttachment(WorkOrderAttachments attachment)
|
|
|
|
|
=> _context.workOrderAttachments.Add(attachment);
|
|
|
|
|
|
2026-08-04 09:30:09 -03:00
|
|
|
public void SetExpectedWorkOrderVersion(WorkOrder workOrder, byte[] version)
|
|
|
|
|
=> _context.Entry(workOrder).Property(w => w.RowVersion).OriginalValue = version;
|
|
|
|
|
|
|
|
|
|
public void MarkWorkOrderModified(WorkOrder workOrder)
|
|
|
|
|
{
|
|
|
|
|
// Force a WO update so the RowVersion concurrency token is enforced when only
|
|
|
|
|
// attachment rows change (category-only Extra mutations).
|
|
|
|
|
var entry = _context.Entry(workOrder);
|
|
|
|
|
if (entry.State == EntityState.Unchanged)
|
|
|
|
|
entry.Property(w => w.Attachments).IsModified = true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-24 17:35:34 -03:00
|
|
|
public Task SaveAsync(CancellationToken cancellationToken)
|
|
|
|
|
=> _context.SaveChangesAsync(cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|