laatzen/LaaProductionWeb/LaaProductionWeb/API/SendController.cs
2024-03-22 11:43:43 +01:00

184 lines
5.6 KiB
C#

namespace LaaProductionWeb.API
{
using LaaProductionDI;
using LaaProductionSMTP;
using LaaProduction.SQL.Interfaces;
using System;
using System.ComponentModel.DataAnnotations;
using System.Web.Http;
[RoutePrefix(ROUTE_PREFIX)]
public class SendController : ControllerBase
{
const string ROUTE_PREFIX = "api/Send";
[HttpPost]
[Route(nameof(Email))]
public IHttpActionResult Email(SendModel model)
{
var response = default(IHttpActionResult);
if (!this.ModelState.IsValid)
{
response = this.BadRequest();
}
else if (!this.TrySendEmail(model, out var state))
{
response = this.BadRequest(state);
}
else
{
response = this.Ok();
}
return response;
}
private bool TrySendEmail(SendModel model, out string state)
{
state = "Sent";
var sent = false;
var inDate = DateTime.Now;
var outDate = default(DateTime?);
var smtpClient = LaaServiceProvider.GetService<SMTPClient>();
void OnSMTPClientError(string error)
{
throw new Exception(error);
}
smtpClient.OnError += OnSMTPClientError;
try
{
var message = smtpClient.CreateMessage();
message
.From(model.Sender)
.To(model.Recipients);
if (model.CC != null && model.CC.Length > 0)
{
message.CC(model.CC);
}
message.Subject(model.Subject);
if (model.IsHtml)
{
message.HtmlBody(model.Body);
}
else
{
message.Body(model.Body);
}
message.Send();
sent = true;
outDate = DateTime.Now;
}
catch (Exception e)
{
state = e.ToString();
}
smtpClient.OnError -= OnSMTPClientError;
var recordId = this.AddRecord($"{ROUTE_PREFIX}/{nameof(Email)}", inDate, outDate, state, model);
return sent;
}
private int AddRecord(string source, DateTime inDate, DateTime? outDate, string state, SendModel model)
{
var connection = LaaServiceProvider.GetService<ISQLConnection>();
var command = connection?.CreateCommand($@"
INSERT
INTO [Messages]
( [Source]
, [InDate]
, [StationNr]
, [PCName]
, [IPAddress]
, [Trace]
, [Sender]
, [Subject]
, [Body]
, [IsHtml]
, [Recipients]
, [CC]
, [OutDate]
, [State])
OUTPUT [Inserted].[Id]
VALUES (@{nameof(source)}
, @{nameof(inDate)}
, @{nameof(model.StationNr)}
, @{nameof(model.PCName)}
, @{nameof(model.IPAddress)}
, @{nameof(model.Trace)}
, @{nameof(model.Sender)}
, @{nameof(model.Subject)}
, @{nameof(model.Body)}
, @{nameof(model.IsHtml)}
, @{nameof(model.Recipients)}
, @{nameof(model.CC)}
, @{nameof(outDate)}
, @{nameof(state)})");
var recordId = command
.SetParameter(nameof(source), source)
.SetParameter(nameof(inDate), inDate)
.SetParameter(nameof(model.StationNr), model.StationNr)
.SetParameter(nameof(model.PCName), model.PCName)
.SetParameter(nameof(model.IPAddress), model.IPAddress)
.SetParameter(nameof(model.Trace), model.Trace)
.SetParameter(nameof(model.Sender), model.Sender)
.SetParameter(nameof(model.Subject), model.Subject)
.SetParameter(nameof(model.Body), model.Body)
.SetParameter(nameof(model.IsHtml), model.IsHtml)
.SetParameter(nameof(model.Recipients), model.Recipients != null || model.Recipients.Length > 0 ? string.Join(", ", model.Recipients) : null)
.SetParameter(nameof(model.CC), model.CC != null && model.CC.Length > 0 ? string.Join(", ", model.CC) : null)
.SetParameter(nameof(outDate), outDate)
.SetParameter(nameof(state), state)
.ExecuteScalar(x => x?.GetInt() ?? 0);
return recordId;
}
}
public class SendModel
{
[Required]
public string StationNr { get; set; }
[Required]
public string PCName { get; set; }
[Required]
public string IPAddress { get; set; }
[Required]
public string Trace { get; set; }
[Required]
public string Sender { get; set; }
[Required]
public string Subject { get; set; }
[Required]
public string Body { get; set; }
public bool IsHtml { get; set; }
[Required]
public string[] Recipients { get; set; }
public string[] CC { get; set; }
}
}