Refactor TestEmailTemplateController to use MediatR
Replaced direct service dependency and base class inheritance with MediatR in TestEmailTemplateController. Updated the GetAll endpoint to use a MediatR query and removed obsolete code related to the previous service-based approach. This aligns the controller with CQRS/MediatR best practices.
This commit is contained in:
@@ -1,34 +1,22 @@
|
||||
using DigitalData.Core.Abstraction.Application.DTO;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using EnvelopeGenerator.Domain.Constants;
|
||||
using EnvelopeGenerator.Application.Common.Dto;
|
||||
using EnvelopeGenerator.Application.Common.Interfaces.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MediatR;
|
||||
using EnvelopeGenerator.Application.EmailTemplates.Queries;
|
||||
|
||||
namespace EnvelopeGenerator.Web.Controllers.Test;
|
||||
|
||||
[Obsolete("Use MediatR")]
|
||||
public class TestEmailTemplateController : TestControllerBase<IEmailTemplateService, EmailTemplateDto, EmailTemplate, int>
|
||||
public class TestEmailTemplateController : ControllerBase
|
||||
{
|
||||
public TestEmailTemplateController(ILogger<TestEmailTemplateController> logger, IEmailTemplateService service) : base(logger, service)
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public TestEmailTemplateController(IMediator mediator)
|
||||
{
|
||||
_mediator = mediator;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Obsolete("Use MediatR")]
|
||||
public virtual async Task<IActionResult> GetAll([FromQuery] string? tempType = null)
|
||||
public virtual async Task<IActionResult> GetAll([FromQuery] ReadEmailTemplateQuery query, CancellationToken cancel)
|
||||
{
|
||||
return tempType is null
|
||||
? await base.GetAll()
|
||||
: await _service.ReadByNameAsync((EmailTemplateType)Enum.Parse(typeof(EmailTemplateType), tempType)).ThenAsync(
|
||||
Success: Ok,
|
||||
Fail: IActionResult (messages, notices) =>
|
||||
{
|
||||
_logger.LogNotice(notices);
|
||||
return NotFound(messages);
|
||||
});
|
||||
var res = await _mediator.Send(query, cancel);
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
public override Task<IActionResult> GetAll() => base.GetAll();
|
||||
}
|
||||
Reference in New Issue
Block a user