Files
hrynco-notification-service/HrynCo.NotificationService.Web/Controllers/Api/EmailChannels/EmailChannelsController.cs
T
agrynco 8e81572386 feat(notification): add admin rest management and deletion
Add revision-safe service and template APIs, channel mapping, and validated admin controls.

Ref: IT-1601
2026-10-17 22:51:01 +03:00

99 lines
4.3 KiB
C#

using HrynCo.NotificationService.DAL.Abstract.Providers;
using HrynCo.NotificationService.Services.Core;
using HrynCo.NotificationService.Web.Controllers.Api.Services;
using HrynCo.NotificationService.Web.Infrastructure;
using HrynCo.NotificationService.Services.EmailChannels.Create;
using HrynCo.NotificationService.Services.EmailChannels.Delete;
using HrynCo.NotificationService.Services.EmailChannels.Get;
using HrynCo.NotificationService.Services.EmailChannels.GetByService;
using HrynCo.NotificationService.Services.EmailChannels.GetAll;
using HrynCo.NotificationService.Services.EmailChannels.Update;
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace HrynCo.NotificationService.Web.Controllers.Api.EmailChannels;
[Route("api/v1/email-channels")]
[TypeFilter(typeof(ApiRequestValidationFilter), Order = -3000)]
public sealed class EmailChannelsController : ApiControllerBase
{
public EmailChannelsController(IMediator mediator) : base(mediator) { }
[HttpGet]
[ProducesResponseType<ApiResponse<IReadOnlyList<EmailChannelDto>>>(StatusCodes.Status200OK)]
public async Task<IActionResult> GetAll([FromQuery] string serviceName, CancellationToken cancellationToken)
{
var result = await Mediator.Send(new GetEmailChannelsQuery(serviceName), cancellationToken);
return FromChannels(result);
}
[HttpGet("all")]
[ProducesResponseType<ApiResponse<IReadOnlyList<EmailChannelDto>>>(StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllServices(CancellationToken cancellationToken) =>
FromChannels(await Mediator.Send(new GetAllEmailChannelsQuery(), cancellationToken));
[HttpGet("{id:guid}")]
[ProducesResponseType<ApiResponse<EmailChannelDto>>(StatusCodes.Status200OK)]
public async Task<IActionResult> Get([FromRoute] Guid id, CancellationToken cancellationToken)
{
var result = await Mediator.Send(new GetEmailChannelQuery(id), cancellationToken);
return result.IsSuccess
? Ok(new ApiResponse<EmailChannelDto> { Success = true, Data = EmailChannelMapping.Map(result.Result!) })
: MapServiceError(result.Error!);
}
[HttpPost]
public async Task<IActionResult> Create([FromBody] CreateEmailChannelRequest request, CancellationToken cancellationToken)
{
if (request.ChannelType != EmailChannelType.Smtp)
return MapServiceError(new ServiceError("Only SMTP channels are supported.", ServiceErrorCode.InvalidRequest));
var command = new CreateEmailChannelCommand(
request.ServiceName,
request.Priority,
request.ChannelType,
EmailChannelMapping.ToSettings(request.Settings),
request.DailyLimit,
request.MonthlyLimit,
request.WarnThresholdPercent,
request.IsActive
);
var result = await Mediator.Send(command, cancellationToken);
if (!result.IsSuccess)
return MapServiceError(result.Error!);
return CreatedAtAction(nameof(Get), new { id = result.Result },
new ApiResponse<Guid> { Success = true, Data = result.Result });
}
[HttpPut("{id:guid}")]
public async Task<IActionResult> Update([FromRoute] Guid id, [FromBody] UpdateEmailChannelRequest request, CancellationToken cancellationToken)
{
var command = new UpdateEmailChannelCommand(
id,
request.Priority,
EmailChannelMapping.ToSettings(request.Settings),
request.DailyLimit,
request.MonthlyLimit,
request.WarnThresholdPercent,
request.IsActive
);
var result = await Mediator.Send(command, cancellationToken);
return FromServiceResult(result);
}
[HttpDelete("{id:guid}")]
public async Task<IActionResult> Delete([FromRoute] Guid id, CancellationToken cancellationToken)
{
var result = await Mediator.Send(new DeleteEmailChannelCommand(id), cancellationToken);
return FromServiceResult(result);
}
private IActionResult FromChannels(ServiceResult<IReadOnlyList<EmailChannel>> result) =>
result.IsSuccess
? Ok(new ApiResponse<IReadOnlyList<EmailChannelDto>> { Success = true, Data = result.Result!.Select(EmailChannelMapping.Map).ToList() })
: MapServiceError(result.Error!);
}