127 lines
3.7 KiB
C#
127 lines
3.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using VeterinaryContracts.BusinessLogicContracts;
|
||
using VeterinaryContracts.BindingModels;
|
||
using VeterinaryContracts.SearchModels;
|
||
using VeterinaryContracts.StorageContracts;
|
||
using VeterinaryContracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
using System.Text.RegularExpressions;
|
||
|
||
namespace VeterinaryBusinessLogic.BusinessLogic
|
||
{
|
||
public class DoctorLogic : IDoctorLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IDoctorStorage _doctorStorage;
|
||
public DoctorLogic(ILogger<DoctorLogic> logger, IDoctorStorage doctorStorage)
|
||
{
|
||
_logger = logger;
|
||
_doctorStorage = doctorStorage;
|
||
}
|
||
public List<DoctorViewModel>? ReadList(DoctorSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. DoctorFIO:{DoctorFIO}. Id:{ Id}", model?.DoctorFIO, model?.Id);
|
||
var list = model == null ? _doctorStorage.GetFullList() : _doctorStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||
return list;
|
||
}
|
||
public DoctorViewModel? ReadElement(DoctorSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation("ReadElement. DoctorFIO:{DoctorFIO}.Id:{ Id}", model.DoctorFIO, model.Id);
|
||
var element = _doctorStorage.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(DoctorBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_doctorStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Update(DoctorBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_doctorStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Delete(DoctorBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||
if (_doctorStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
private void CheckModel(DoctorBindingModel model, bool withParams =
|
||
true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.DoctorFIO))
|
||
{
|
||
throw new ArgumentNullException("Нет ФИО кладовщика",
|
||
nameof(model.DoctorFIO));
|
||
}
|
||
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("Doctor. DoctorFIO:{DoctorFIO}." +
|
||
"Login:{ Login}. Password:{ Password}. Id: { Id} ", model.DoctorFIO, model.Login, model.Password, model.Id);
|
||
var element = _doctorStorage.GetElement(new DoctorSearchModel
|
||
{
|
||
Login = model.Login,
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Кладовщик с таким же логином уже существует");
|
||
}
|
||
}
|
||
}
|
||
}
|