PIbd-23_Ivanov_V.N._Pizzeria/Pizzeria/PizzeriaView/FormLetter.cs

143 lines
3.5 KiB
C#
Raw Normal View History

2024-04-03 09:19:16 +04:00
using Microsoft.Extensions.Logging;
using Pizzeria;
using PizzeriaBusinessLogic.MailWorker;
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.BusinessLogicsContracts;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.ViewModels;
namespace PizzeriaView
{
2024-04-03 10:26:39 +04:00
public partial class FormLetter : Form
{
private readonly ILogger _logger;
private readonly IMessageInfoLogic _logic;
private readonly AbstractMailWorker _worker;
2024-04-03 09:19:16 +04:00
2024-04-03 10:26:39 +04:00
public MessageInfoViewModel? model;
public string? messageId;
2024-04-03 09:19:16 +04:00
2024-04-03 10:26:39 +04:00
public FormLetter(ILogger<FormLetter> logger, IMessageInfoLogic logic, AbstractMailWorker worker)
{
InitializeComponent();
_logger = logger;
_logic = logic;
_worker = worker;
}
2024-04-03 09:19:16 +04:00
2024-04-03 10:26:39 +04:00
private void FormLetter_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(messageId))
{
ReloadLetter();
return;
}
else if (model != null)
{
ConfigurateToCreateAnsver();
return;
}
_logger.LogError("Для формы не переданно сведений о письме, на которое отвечаем!");
DialogResult = DialogResult.Abort;
Close();
return;
}
2024-04-03 09:19:16 +04:00
2024-04-03 10:26:39 +04:00
private void ReloadLetter()
{
_logger.LogInformation("Загрузка существующего письма с id:{}", messageId);
model = _logic.ReadElement(new MessageInfoSearchModel
{
MessageId = messageId
});
if (model != null)
{
_logger.LogInformation("Письмо найдено");
textBoxEmail.Text = model.SenderName;
textBoxDate.Text = model.DateDelivery.ToString();
textBoxSubject.Text = model.Subject;
textBoxBody.Text = model.Body;
2024-04-03 09:19:16 +04:00
2024-04-03 10:26:39 +04:00
if (model.IsReply)
{
_logger.LogInformation("Письмо само и есть ответ");
buttonReply.Visible = false;
}
else
{
if (!string.IsNullOrEmpty(model.ReplyMessageId))
{
_logger.LogInformation("У письма есть ответ.");
buttonReply.Text = "Прочитать ответ";
}
}
return;
}
_logger.LogWarning("Письмо с таким id не удалось найти");
DialogResult = DialogResult.Abort;
Close();
return;
}
2024-04-03 09:19:16 +04:00
2024-04-03 10:26:39 +04:00
private void ConfigurateToCreateAnsver()
{
textBoxEmail.Text = model.SenderName;
labelDate.Visible = false;
textBoxDate.Visible = false;
textBoxSubject.Text = $"re: {model.Subject}";
textBoxBody.ReadOnly = false;
buttonReply.Visible = false;
buttonSend.Visible = true;
_logger.LogInformation("Запущена форма создания нового письма - ответа");
}
2024-04-03 09:19:16 +04:00
2024-04-03 10:26:39 +04:00
private void buttonClose_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
2024-04-03 09:19:16 +04:00
2024-04-03 10:26:39 +04:00
private void buttonReply_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormLetter));
if (service is FormLetter form)
{
if (!string.IsNullOrEmpty(model.ReplyMessageId))
{
form.messageId = model.ReplyMessageId;
}
else
{
form.model = model;
}
2024-04-03 09:19:16 +04:00
2024-04-03 10:26:39 +04:00
if (form.ShowDialog() != DialogResult.Cancel)
{
buttonReply.Visible = false;
}
}
}
2024-04-03 09:19:16 +04:00
2024-04-03 10:26:39 +04:00
private void buttonSend_Click(object sender, EventArgs e)
{
if (model == null)
{
return;
}
string subject = textBoxSubject.Text;
string text = textBoxBody.Text;
2024-04-03 09:19:16 +04:00
2024-04-03 10:26:39 +04:00
Task.Run(() => _worker.MailSendReplyAsync(new MailReplySendInfoBindingModel
{
MailAddress = model.SenderName,
Subject = subject,
Text = text,
ParentMessageId = model.MessageId,
}));
DialogResult = DialogResult.OK;
Close();
}
}
2024-04-03 09:19:16 +04:00
}