PIbd-21_BatylkinaAO_MusoevD.../Canteen/CanteenBusinessLogic/BusinessLogics/VisitorLogic.cs

151 lines
4.8 KiB
C#
Raw Normal View History

2023-06-21 19:25:26 +04:00
using CanteenBusinessLogic.MailWorker;
using CanteenContracts.BindingModels;
2023-04-09 13:00:51 +04:00
using CanteenContracts.BusinessLogicsContracts;
using CanteenContracts.SearchModel;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
using Microsoft.Extensions.Logging;
namespace CanteenBusinessLogic.BusinessLogics
{
public class VisitorLogic : IVisitorLogic
{
private readonly ILogger _logger;
private readonly IVisitorStorage _visitorStorage;
2023-06-21 19:25:26 +04:00
private readonly AbstractMailWorker _mailWorker;
2023-04-09 13:00:51 +04:00
2023-06-21 19:25:26 +04:00
public VisitorLogic(ILogger<VisitorLogic> logger, IVisitorStorage visitorStorage, AbstractMailWorker mailWorker)
2023-04-09 13:00:51 +04:00
{
_logger = logger;
_visitorStorage = visitorStorage;
2023-06-21 19:25:26 +04:00
_mailWorker = mailWorker;
2023-04-09 13:00:51 +04:00
}
public bool Create(VisitorBindingModel model)
{
CheckModel(model);
if (_visitorStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(VisitorBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_visitorStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public VisitorViewModel? ReadElement(VisitorSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Login: {Login}. Id: {Id}", model.Login, model.Id);
var element = _visitorStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
return element;
}
public List<VisitorViewModel>? ReadList(VisitorSearchModel? model)
{
_logger.LogInformation("ReadList. Login: {Login}. Id: {Id}", model.Login, model.Id);
var list = model == null ? _visitorStorage.GetFullList() : _visitorStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(VisitorBindingModel model)
{
CheckModel(model);
if (_visitorStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
2023-06-21 19:25:26 +04:00
public bool SendMail(MailSendInfoBindingModel emailInfo)
{
_mailWorker.MailSendAsync(new()
{
MailAddress = emailInfo.MailAddress,
Subject = emailInfo.Subject,
Path = emailInfo.Path,
});
return true;
}
2023-04-09 13:00:51 +04:00
private void CheckModel(VisitorBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.FIO))
{
throw new ArgumentNullException("Нет ФИО у посетителя", nameof(model.FIO));
}
if (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Нет логина у посетителя", nameof(model.Login));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля у посетителя", nameof(model.Password));
}
if (string.IsNullOrEmpty(model.PhoneNumber))
{
throw new ArgumentNullException("У номер телефона у посетителя", nameof(model.PhoneNumber));
}
_logger.LogInformation("Visitor. Login: {Login}. Password: {Password}. PhoneNumber: {PhoneNumber}. Id: {Id}", model.Login, model.Password, model.PhoneNumber, model.Id);
var element = _visitorStorage.GetElement(new VisitorSearchModel { Login = model.Login, Password = model.Password });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Такой посетитель уже есть");
}
}
}
}