using Hcs.WebApp.Data.Hcs; using Microsoft.EntityFrameworkCore; namespace Hcs.WebApp.Services { public class RegistryService(IDbContextFactory factory) : HcsServiceBase(factory) { public async Task GetRegistryCountAsync(HcsDbContext context, bool isCommon) { return await context.Registries.CountAsync(x => x.IsCommon == isCommon); } public async Task> GetAllRegistriesAsync(bool isCommon) { using var context = GetNewContext(); return await (from registry in context.Registries where registry.IsCommon == isCommon select registry).ToListAsync(); } public async Task GetRegistryByOperationIdAsync(int operationId) { using var context = GetNewContext(); return await GetRegistryByOperationIdAsync(context, operationId); } public async Task GetRegistryByOperationIdAsync(HcsDbContext context, int operationId, bool includeElements = false) { if (includeElements) { return await context.Registries .Include(x => x.Elements) .SingleAsync(x => x.LastSyncOperationId == operationId); } return await context.Registries.SingleAsync(x => x.LastSyncOperationId == operationId); } public async Task> GetRegistriesByOperationIdAsync(int operationId) { using var context = GetNewContext(); return await (from registry in context.Registries where registry.LastSyncOperationId == operationId select registry).ToListAsync(); } public async Task SetOperationsToRegistriesAsync(HcsDbContext context, bool isCommon, IEnumerable operations) { var queue = new Queue(operations); await context.Registries.ForEachAsync(x => { if (x.IsCommon == isCommon) { x.LastSyncOperationId = queue.Dequeue().Id; } }); await context.SaveChangesAsync(); } public async Task> GetRegistryElementsAsync(Guid registryId) { using var context = GetNewContext(); return await context.Elements.Where(x => x.RegistryId == registryId).ToListAsync(); } } }