132 lines
3.7 KiB
C#
132 lines
3.7 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using VeterinaryContracts.BindingModels;
|
||
using VeterinaryContracts.BusinessLogicContracts;
|
||
using VeterinaryContracts.SearchModels;
|
||
using VeterinaryContracts.StorageContracts;
|
||
using VeterinaryContracts.ViewModels;
|
||
|
||
namespace VeterinaryBusinessLogic.BusinessLogic
|
||
{
|
||
public class VisitLogic : IVisitLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IVisitStorage _visitStorage;
|
||
|
||
public VisitLogic(ILogger<VisitLogic> logger, IVisitStorage visitStorage)
|
||
{
|
||
_logger = logger;
|
||
_visitStorage = visitStorage;
|
||
}
|
||
|
||
public VisitViewModel? ReadElement(VisitSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation("ReadElement. VisitName:{VisitName} Id:{ Id}", model.VisitName, model.Id);
|
||
var element = _visitStorage.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<VisitViewModel>? ReadList(VisitSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. VisitName:{VisitName} Id:{ Id}", model?.VisitName, model?.Id);
|
||
var list = model == null ? _visitStorage.GetFullList() : _visitStorage.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(VisitBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_visitStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Update(VisitBindingModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (string.IsNullOrEmpty(model.VisitName))
|
||
{
|
||
throw new ArgumentNullException("Нет названия визита", nameof(model.VisitName));
|
||
}
|
||
var element = _visitStorage.GetElement(new VisitSearchModel
|
||
{
|
||
VisitName = model.VisitName
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Визит с таким названием уже есть");
|
||
}
|
||
if (_visitStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Delete(VisitBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||
if (_visitStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
private void CheckModel(VisitBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.VisitName))
|
||
{
|
||
throw new ArgumentNullException("Нет названия визита", nameof(model.VisitName));
|
||
}
|
||
if (model.DateVisit == null)
|
||
{
|
||
throw new ArgumentNullException("Нет времени визита",
|
||
nameof(model.DateVisit));
|
||
}
|
||
if (model.DateVisit <= DateTime.Now)
|
||
{
|
||
throw new ArgumentNullException("Дата визита не должна быть в прошлом", nameof(model.DateVisit));
|
||
}
|
||
_logger.LogInformation("Visit. Visit:{NameVisit}. DateVisit:{ DateVisit}. Id: { Id}", model.VisitName, model.DateVisit, model.Id);
|
||
var element = _visitStorage.GetElement(new VisitSearchModel
|
||
{
|
||
VisitName = model.VisitName
|
||
});
|
||
//if (element != null && element.Id != model.Id)
|
||
//{
|
||
// throw new InvalidOperationException("Визит с таким названием уже есть");
|
||
//}
|
||
}
|
||
}
|
||
}
|