121 lines
3.5 KiB
C#
121 lines
3.5 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using System.Text.RegularExpressions;
|
||
using VeterinaryContracts.BindingModels;
|
||
using VeterinaryContracts.BusinessLogicContracts;
|
||
using VeterinaryContracts.SearchModels;
|
||
using VeterinaryContracts.StorageContracts;
|
||
using VeterinaryContracts.ViewModels;
|
||
|
||
namespace VeterinaryBusinessLogic.BusinessLogic
|
||
{
|
||
public class OwnerLogic : IOwnerLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IOwnerStorage _ownerStorage;
|
||
public OwnerLogic(ILogger<OwnerLogic> logger, IOwnerStorage ownerStorage)
|
||
{
|
||
_logger = logger;
|
||
_ownerStorage = ownerStorage;
|
||
}
|
||
public List<OwnerViewModel>? ReadList(OwnerSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. OwnerFIO:{OwnerFIO}. Id:{ Id}", model?.OwnerFIO, model?.Id);
|
||
var list = model == null ? _ownerStorage.GetFullList() : _ownerStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||
return list;
|
||
}
|
||
public OwnerViewModel? ReadElement(OwnerSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation("ReadElement. OwnerFIO:{OwnerFIO}.Id:{ Id}", model.OwnerFIO, model.Id);
|
||
var element = _ownerStorage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
_logger.LogWarning("ReadElement element not found");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
||
return element;
|
||
}
|
||
public bool Create(OwnerBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_ownerStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Update(OwnerBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_ownerStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Delete(OwnerBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||
if (_ownerStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
private void CheckModel(OwnerBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.OwnerFIO))
|
||
{
|
||
throw new ArgumentNullException("Нет ФИО клиента",
|
||
nameof(model.OwnerFIO));
|
||
}
|
||
if (string.IsNullOrEmpty(model.Login))
|
||
{
|
||
throw new ArgumentNullException("Нет Login клиента",
|
||
nameof(model.Login));
|
||
}
|
||
if (!Regex.IsMatch(model.Login, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"))
|
||
{
|
||
throw new ArgumentException("Некорретно введен email клиента", nameof(model.Login));
|
||
}
|
||
if (string.IsNullOrEmpty(model.Password))
|
||
{
|
||
throw new ArgumentNullException("Нет пароля клиента",
|
||
nameof(model.Password));
|
||
}
|
||
_logger.LogInformation("Owner. OwnerFIO:{OwnerFIO}." +
|
||
"Login:{ Login}. Password:{ Password}. Id: { Id} ", model.OwnerFIO, model.Login, model.Password, model.Id);
|
||
var element = _ownerStorage.GetElement(new OwnerSearchModel
|
||
{
|
||
Login = model.Login,
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Клиент с таким логином уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|