65 lines
1.8 KiB
C#
65 lines
1.8 KiB
C#
using Asp.Versioning;
|
|
using GerstITS.Examples.Api.Versioning;
|
|
using GerstITS.Mail;
|
|
using GerstITS.Validation;
|
|
using GerstITS.Web.Api;
|
|
using GerstITS.Web.Api.FluentApi;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace GerstITS.Examples.Api.Controllers._1._1;
|
|
|
|
/// <summary>
|
|
/// Is responsible to send contact requests via the configured smtp mail provider.
|
|
/// </summary>
|
|
[ApiController,
|
|
ApiVersion(Versions._1_1),
|
|
Route(ApplicationEnvironment.WebApi.ControllerRouteTemplate)]
|
|
public class ContactController : FluentApiControllerBase
|
|
{
|
|
#region Fields
|
|
|
|
private const string _supportAddress = "support@example.com";
|
|
|
|
private readonly IMailProvider _mailProvider;
|
|
|
|
#endregion
|
|
|
|
#region Constructors
|
|
|
|
public ContactController(IMailProvider mailProvider,
|
|
IValidator validator)
|
|
: base(validator)
|
|
{
|
|
_mailProvider = mailProvider;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <summary>
|
|
/// Sends a contact request as mail to the support address.
|
|
/// </summary>
|
|
/// <param name="contactRequest">Contact request to send.</param>
|
|
/// <returns>Returns true if the mail was sent.</returns>
|
|
[HttpPost]
|
|
public IActionResult Send([FromBody] ContactRequest contactRequest)
|
|
{
|
|
return Api().Use(contactRequest)
|
|
.Create(x => _mailProvider.SendMail(ToMailNotification(x)));
|
|
}
|
|
|
|
private static MailNotification ToMailNotification(ContactRequest contactRequest)
|
|
{
|
|
return new MailNotification
|
|
{
|
|
Recipients = new[] { _supportAddress },
|
|
Subject = $"{contactRequest.Subject} ({contactRequest.SenderName}, {contactRequest.SenderEmail})",
|
|
Body = contactRequest.Body,
|
|
IsHtmlMail = false
|
|
};
|
|
}
|
|
|
|
#endregion
|
|
}
|