Files
hrynco-notification-service/HrynCo.NotificationService.DAL.EF/Repositories/EmailChannelRepository.cs
T
Anatolii Grynchuk 215285d3c0 fix: channel save tracking conflict and test modal rendering
- Use AsNoTracking() on all EmailChannelRepository read methods to prevent
  EF identity conflict when Update() attaches a new entity with same key
- Move test modal to @section Scripts rendered at end of <body> so
  bootstrap.Modal is available and modal is not nested inside card DOM
- Add @RenderSection('Scripts') forwarding in _EditorLayout to bubble
  child scripts sections up to _Layout
- Switch Test button to programmatic bootstrap.Modal() open instead of
  data-bs-toggle (more reliable across layout section boundaries)

Ref: IT-628

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-02 03:29:54 +03:00

112 lines
3.8 KiB
C#

using System.Text.Json;
using HrynCo.NotificationService.DAL.Abstract.Providers;
using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.DAL.EF.Core;
using HrynCo.NotificationService.DAL.EF.Entities;
using Microsoft.EntityFrameworkCore;
namespace HrynCo.NotificationService.DAL.EF.Repositories;
internal sealed class EmailChannelRepository : EfRepository<EmailChannelEntity>, IEmailChannelRepository
{
public EmailChannelRepository(NotificationDbContext dbContext) : base(dbContext)
{
}
public async Task<IReadOnlyList<EmailChannel>> GetAllAsync(CancellationToken ct = default)
{
var entities = await DbSet
.AsNoTracking()
.OrderBy(x => x.ServiceName)
.ThenBy(x => x.Priority)
.ToListAsync(ct);
return entities.Select(MapToDomain).ToList();
}
public async Task<IReadOnlyList<EmailChannel>> GetByServiceAsync(string serviceName, CancellationToken ct = default)
{
var entities = await DbSet
.AsNoTracking()
.Where(x => x.ServiceName == serviceName)
.OrderBy(x => x.Priority)
.ToListAsync(ct);
return entities.Select(MapToDomain).ToList();
}
public async Task<EmailChannel?> GetByIdAsync(Guid id, CancellationToken ct = default)
{
EmailChannelEntity? entity = await DbSet.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id, ct);
return entity is null ? null : MapToDomain(entity);
}
public Task AddAsync(EmailChannel channel, CancellationToken ct = default)
{
return base.AddAsync(MapToEntity(channel), ct);
}
public Task UpdateAsync(EmailChannel channel, CancellationToken ct = default)
{
EmailChannelEntity entity = MapToEntity(channel);
entity.Updated = DateTimeOffset.UtcNow;
Update(entity);
return Task.CompletedTask;
}
public async Task DeleteAsync(EmailChannel channel, CancellationToken ct = default)
{
EmailChannelEntity? entity = await DbSet.FindAsync([channel.Id], ct);
if (entity is not null)
{
Delete(entity);
}
}
private static EmailChannel MapToDomain(EmailChannelEntity e)
{
return new EmailChannel
{
Id = e.Id,
ServiceName = e.ServiceName,
Priority = e.Priority,
EmailChannelType = e.EmailChannelType,
Settings = DeserializeSettings(e.EmailChannelType, e.SettingsJson),
DailyLimit = e.DailyLimit,
MonthlyLimit = e.MonthlyLimit,
WarnThresholdPercent = e.WarnThresholdPercent,
IsActive = e.IsActive,
Created = e.Created,
Updated = e.Updated
};
}
private static EmailChannelEntity MapToEntity(EmailChannel p)
{
return new EmailChannelEntity
{
Id = p.Id,
ServiceName = p.ServiceName,
Priority = p.Priority,
EmailChannelType = p.EmailChannelType,
SettingsJson = JsonSerializer.Serialize(p.Settings),
DailyLimit = p.DailyLimit,
MonthlyLimit = p.MonthlyLimit,
WarnThresholdPercent = p.WarnThresholdPercent,
IsActive = p.IsActive,
Created = p.Created,
Updated = p.Updated
};
}
private static EmailChannelSettings DeserializeSettings(EmailChannelType type, string json)
{
return type switch
{
EmailChannelType.Smtp => JsonSerializer.Deserialize<SmtpChannelSettings>(json)
?? throw new InvalidOperationException(
"Failed to deserialize SMTP EmailChannel settings."),
_ => throw new InvalidOperationException($"Unknown or undefined email channel type: {type}")
};
}
}