Messages BusinessLogic
This commit is contained in:
parent
505a708ddf
commit
14aeac885b
@ -4,10 +4,11 @@ using AutoWorkshopContracts.SearchModels;
|
||||
using AutoWorkshopContracts.StoragesContracts;
|
||||
using AutoWorkshopContracts.ViewModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace AutoWorkshopBusinessLogic.BusinessLogics
|
||||
{
|
||||
public class ClientLogic : IClientLogic
|
||||
public class ClientLogic : IClientLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IClientStorage _clientStorage;
|
||||
@ -100,11 +101,11 @@ namespace AutoWorkshopBusinessLogic.BusinessLogics
|
||||
if (string.IsNullOrEmpty(Model.ClientFIO))
|
||||
throw new ArgumentNullException("Нет ФИО клиента", nameof(Model.ClientFIO));
|
||||
|
||||
if (string.IsNullOrEmpty(Model.Email))
|
||||
throw new ArgumentNullException("Нет Email клиента", nameof(Model.ClientFIO));
|
||||
if (string.IsNullOrEmpty(Model.Email) || !Regex.IsMatch(Model.Email, @"^[a-z0-9._%+-]+\@([a-z0-9-]+\.)+[a-z]{2,4}$"))
|
||||
throw new ArgumentNullException("Почта не соответствует требованиям", nameof(Model.Email));
|
||||
|
||||
if (string.IsNullOrEmpty(Model.Password))
|
||||
throw new ArgumentNullException("Нет пароля клиента", nameof(Model.ClientFIO));
|
||||
if (string.IsNullOrEmpty(Model.Password) || !Regex.IsMatch(Model.Password, @"^(?=.*[A-Za-z])(?=.*\d)(?=.*[^A-Za-z0-9\n]).{10,50}$"))
|
||||
throw new ArgumentNullException("Пароль не соответствует требованиям", nameof(Model.Password));
|
||||
|
||||
_logger.LogInformation("Client. ClientFIO: {ClientFIO}." +
|
||||
"Email: {Email}. Password: {Password}. Id: {Id} ", Model.ClientFIO, Model.Email, Model.Password, Model.Id);
|
||||
|
@ -23,14 +23,63 @@ namespace AutoWorkshopBusinessLogic.BusinessLogics
|
||||
|
||||
public List<MessageInfoViewModel>? ReadList(MessageInfoSearchModel? Model)
|
||||
{
|
||||
// TODO
|
||||
return new();
|
||||
_logger.LogDebug("ReadList. MessageId: {MessageId}, ClientId: {ClientId}", Model?.MessageId, Model?.ClientId);
|
||||
|
||||
var Result = Model == null ? _messageInfoStorage.GetFullList() : _messageInfoStorage.GetFilteredList(Model);
|
||||
|
||||
_logger.LogDebug("ReadList result. Count: {Count}", Result.Count);
|
||||
return Result;
|
||||
}
|
||||
|
||||
public bool Create(MessageInfoBindingModel Model)
|
||||
{
|
||||
// TODO
|
||||
return false;
|
||||
CheckModel(Model);
|
||||
|
||||
if (_messageInfoStorage.Insert(Model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void CheckModel(MessageInfoBindingModel Model, bool WithParams = true)
|
||||
{
|
||||
if (Model == null)
|
||||
throw new ArgumentNullException(nameof(Model));
|
||||
|
||||
if (!WithParams)
|
||||
return;
|
||||
|
||||
if (string.IsNullOrEmpty(Model.MessageId))
|
||||
throw new ArgumentNullException("Не указан id сообщения", nameof(Model.MessageId));
|
||||
|
||||
if (string.IsNullOrEmpty(Model.SenderName))
|
||||
throw new ArgumentNullException("Не указао почта", nameof(Model.SenderName));
|
||||
|
||||
if (string.IsNullOrEmpty(Model.Subject))
|
||||
throw new ArgumentNullException("Не указана тема", nameof(Model.Subject));
|
||||
|
||||
if (string.IsNullOrEmpty(Model.Body))
|
||||
throw new ArgumentNullException("Не указан текст сообщения", nameof(Model.Subject));
|
||||
|
||||
_logger.LogInformation("MessageInfo. MessageId: {MessageId}. SenderName: {SenderName}. Subject: {Subject}. Body: {Body}",
|
||||
Model.MessageId, Model.SenderName, Model.Subject, Model.Body);
|
||||
|
||||
var Element = _clientStorage.GetElement(new ClientSearchModel
|
||||
{
|
||||
Email = Model.SenderName
|
||||
});
|
||||
|
||||
if (Element == null)
|
||||
{
|
||||
_logger.LogWarning("Не удалось найти клиента, отправившего письмо с адреса {Email}", Model.SenderName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Model.ClientId = Element.Id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using AutoWorkshopContracts.BindingModels;
|
||||
using AutoWorkshopBusinessLogic.MailWorker;
|
||||
using AutoWorkshopContracts.BindingModels;
|
||||
using AutoWorkshopContracts.BusinessLogicContracts;
|
||||
using AutoWorkshopContracts.SearchModels;
|
||||
using AutoWorkshopContracts.StoragesContracts;
|
||||
@ -8,18 +9,21 @@ using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace AutoWorkshopBusinessLogic.BusinessLogics
|
||||
{
|
||||
public class OrderLogic : IOrderLogic
|
||||
public class OrderLogic : IOrderLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
private readonly IOrderStorage _orderStorage;
|
||||
|
||||
private readonly AbstractMailWorker _mailLogic;
|
||||
|
||||
static readonly object _locker = new object();
|
||||
|
||||
public OrderLogic(ILogger<RepairLogic> Logger, IOrderStorage OrderStorage)
|
||||
public OrderLogic(ILogger<RepairLogic> Logger, IOrderStorage OrderStorage, AbstractMailWorker MailLogic)
|
||||
{
|
||||
_logger = Logger;
|
||||
_orderStorage = OrderStorage;
|
||||
_mailLogic = MailLogic;
|
||||
}
|
||||
|
||||
public List<OrderViewModel>? ReadList(OrderSearchModel? Model)
|
||||
@ -36,6 +40,7 @@ namespace AutoWorkshopBusinessLogic.BusinessLogics
|
||||
_logger.LogInformation("ReadList. Count: {Count}", List.Count);
|
||||
return List;
|
||||
}
|
||||
|
||||
public bool CreateOrder(OrderBindingModel Model)
|
||||
{
|
||||
CheckModel(Model);
|
||||
@ -48,23 +53,30 @@ namespace AutoWorkshopBusinessLogic.BusinessLogics
|
||||
|
||||
Model.Status = OrderStatus.Accepted;
|
||||
|
||||
if (_orderStorage.Insert(Model) is null)
|
||||
var CreatedOrder = _orderStorage.Insert(Model);
|
||||
|
||||
if (CreatedOrder == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Task.Run(() => _mailLogic.SendMailAsync(new MailSendInfoBindingModel
|
||||
{
|
||||
MailAddress = CreatedOrder.ClientEmail,
|
||||
Subject = $"Изменение статуса заказа номер {CreatedOrder.Id}",
|
||||
Text = $"Ваш заказ номер {CreatedOrder.Id} на ремонт {CreatedOrder.RepairName} от {CreatedOrder.DateCreate} на сумму {CreatedOrder.Sum} принят."
|
||||
}));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool ChangeOrderStatus(OrderBindingModel Model, OrderStatus NewStatus)
|
||||
{
|
||||
CheckModel(Model, false);
|
||||
|
||||
var Order = _orderStorage.GetElement(new OrderSearchModel { Id = Model.Id });
|
||||
|
||||
if (Order == null)
|
||||
{
|
||||
{
|
||||
CheckModel(Model, false);
|
||||
var Order = _orderStorage.GetElement(new OrderSearchModel { Id = Model.Id });
|
||||
if (Order == null)
|
||||
{
|
||||
_logger.LogWarning("Change status operation failed. Order not found");
|
||||
return false;
|
||||
}
|
||||
@ -92,10 +104,19 @@ namespace AutoWorkshopBusinessLogic.BusinessLogics
|
||||
if (_orderStorage.Update(Model) == null)
|
||||
{
|
||||
_logger.LogWarning("Change status operation failed");
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
// TODO: check order ClientEmail
|
||||
string DateInfo = Model.DateImplement.HasValue ? $"Дата выполнения {Model.DateImplement}" : "";
|
||||
Task.Run(() => _mailLogic.SendMailAsync(new MailSendInfoBindingModel
|
||||
{
|
||||
MailAddress = Order.ClientEmail,
|
||||
Subject = $"Изменение статуса заказа номер {Order.Id}",
|
||||
Text = $"Ваш заказ номер {Order.Id} на ремонт {Order.RepairName} от {Order.DateCreate} на сумму {Order.Sum} {Order.Status}. {DateInfo}"
|
||||
}));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TakeOrderInWork(OrderBindingModel Model)
|
||||
|
Loading…
Reference in New Issue
Block a user