using StellaOps.Notify.Models; using System.Collections.Concurrent; using System.Linq; namespace StellaOps.Notify.WebService.Storage.Compat; public interface INotifyQuietHoursRepository { Task> ListAsync( string tenantId, string? channelId, bool? enabledOnly, CancellationToken cancellationToken = default); Task GetAsync( string tenantId, string scheduleId, CancellationToken cancellationToken = default); Task UpsertAsync( NotifyQuietHoursSchedule schedule, CancellationToken cancellationToken = default); Task DeleteAsync( string tenantId, string scheduleId, CancellationToken cancellationToken = default); } public sealed class InMemoryQuietHoursRepository : INotifyQuietHoursRepository { private readonly ConcurrentDictionary> _store = new(); public Task> ListAsync( string tenantId, string? channelId, bool? enabledOnly, CancellationToken cancellationToken = default) { var items = ForTenant(tenantId).Values.AsEnumerable(); if (!string.IsNullOrWhiteSpace(channelId)) { items = items.Where(s => string.Equals(s.ChannelId, channelId, StringComparison.OrdinalIgnoreCase)); } if (enabledOnly is true) { items = items.Where(s => s.Enabled); } var result = items .OrderBy(s => s.Name, StringComparer.OrdinalIgnoreCase) .ToList(); return Task.FromResult>(result); } public Task GetAsync( string tenantId, string scheduleId, CancellationToken cancellationToken = default) { var items = ForTenant(tenantId); items.TryGetValue(scheduleId, out var schedule); return Task.FromResult(schedule); } public Task UpsertAsync( NotifyQuietHoursSchedule schedule, CancellationToken cancellationToken = default) { var items = ForTenant(schedule.TenantId); items[schedule.ScheduleId] = schedule; return Task.FromResult(schedule); } public Task DeleteAsync( string tenantId, string scheduleId, CancellationToken cancellationToken = default) { var items = ForTenant(tenantId); return Task.FromResult(items.TryRemove(scheduleId, out _)); } private ConcurrentDictionary ForTenant(string tenantId) => _store.GetOrAdd(tenantId, _ => new ConcurrentDictionary()); }