Files
hrynco-notification-service/HrynCo.NotificationService.DAL.EF/Repositories/EmailChannelRepository.cs
T
Anatolii Grynchuk c90b07386d feat: polished admin UI styles + email channels admin CRUD
- Extract inline styles to wwwroot/css/admin.css
- Bootstrap Icons for nav and buttons
- Styled page headers, table, empty state, readonly fields
- Email Channels admin: list, create, edit, delete
- GetAllEmailChannelsQuery + handler
- AdminChannelsController with full CRUD
- form id + form= attribute pattern for EditorLayout footer buttons

Ref: IT-628

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

110 lines
3.7 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
.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
.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.FindAsync([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}")
};
}
}