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

158 lines
4.9 KiB
C#
Raw Permalink Normal View History

2023-06-19 09:12:18 +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;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenBusinessLogic.BusinessLogics
{
public class ManagerLogic : IManagerLogic
{
private readonly ILogger _logger;
private readonly IManagerStorage _managerStorage;
2023-06-19 09:12:18 +04:00
private readonly AbstractMailWorker _mailWorker;
2023-04-09 13:00:51 +04:00
2023-06-19 09:12:18 +04:00
public ManagerLogic(ILogger<ManagerLogic> logger, IManagerStorage managerStorage, AbstractMailWorker mailWorker)
2023-04-09 13:00:51 +04:00
{
_logger = logger;
_managerStorage = managerStorage;
2023-06-19 09:12:18 +04:00
_mailWorker = mailWorker;
2023-04-09 13:00:51 +04:00
}
public bool Create(ManagerBindingModel model)
{
CheckModel(model);
if (_managerStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ManagerBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_managerStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ManagerViewModel? ReadElement(ManagerSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Login: {Login}. Id: {Id}", model.Login, model.Id);
var element = _managerStorage.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<ManagerViewModel>? ReadList(ManagerSearchModel? model)
{
_logger.LogInformation("ReadList. Login: {Login}. Id: {Id}", model.Login, model.Id);
var list = model == null ? _managerStorage.GetFullList() : _managerStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
2023-06-19 09:12:18 +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
public bool Update(ManagerBindingModel model)
{
CheckModel(model);
if (_managerStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ManagerBindingModel 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("Manager. Login: {Login}. Password: {Password}. PhoneNumber: {PhoneNumber}. Id: {Id}", model.Login, model.Password, model.PhoneNumber, model.Id);
var element = _managerStorage.GetElement(new ManagerSearchModel { Login = model.Login, PhoneNumber = model.PhoneNumber });
2023-04-09 13:00:51 +04:00
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Такой менеджер уже есть");
}
}
2023-06-19 09:12:18 +04:00
2023-04-09 13:00:51 +04:00
}
}