125 lines
4.5 KiB
C#
125 lines
4.5 KiB
C#
using HospitalContracts.BindingModels;
|
||
using HospitalContracts.BusinessLogicContracts;
|
||
using HospitalContracts.SearchModels;
|
||
using HospitalContracts.StorageContracts;
|
||
using HospitalContracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
using System.Text.RegularExpressions;
|
||
|
||
namespace HospitalBusinessLogic
|
||
{
|
||
public class ApothecaryLogic : IApothecaryLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IApothecaryStorage _apothecaryStorage;
|
||
|
||
public ApothecaryLogic(ILogger<ApothecaryLogic> logger, IApothecaryStorage apothecaryStorage)
|
||
{
|
||
_logger = logger;
|
||
_apothecaryStorage = apothecaryStorage;
|
||
}
|
||
|
||
public ApothecaryViewModel? ReadElement(ApothecarySearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation("ReadElement. Login: {Login} Id:{ Id}", model.Login, model.Id);
|
||
var element = _apothecaryStorage.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<ApothecaryViewModel>? ReadList(ApothecarySearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. Login: {Login}. Id:{ Id}", model?.Login, model?.Id);
|
||
var list = model == null ? _apothecaryStorage.GetFullList() : _apothecaryStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||
return list;
|
||
}
|
||
|
||
public bool Create(ApothecaryBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_apothecaryStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(ApothecaryBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||
if (_apothecaryStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Update(ApothecaryBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_apothecaryStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private void CheckModel(ApothecaryBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(model.Login))
|
||
{
|
||
throw new ArgumentNullException("Нет логина пользователя",
|
||
nameof(model.Login));
|
||
}
|
||
if (string.IsNullOrEmpty(model.Password))
|
||
{
|
||
throw new ArgumentNullException("Нет пароля пользователя",
|
||
nameof(model.Password));
|
||
}
|
||
if (!Regex.IsMatch(model.Login, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,})+)$"))
|
||
{
|
||
throw new ArgumentException("Некорректно введена почта клиента", nameof(model.Login));
|
||
}
|
||
if (model.Password.Length < 5 || model.Password.Length > 15)
|
||
{
|
||
throw new ArgumentNullException("Пароль должен иметь длину от 5 до 15 символов",
|
||
nameof(model.Password));
|
||
}
|
||
_logger.LogInformation("Apothecary. Login:{ Login}. Id: { Id}", model.Login, model.Id);
|
||
var element = _apothecaryStorage.GetElement(new ApothecarySearchModel { Login = model.Login });
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Пользователь с таким логином уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|