PIbd-21_Danilov_V.V._Course.../VeterinaryClinic/VeterinaryClinicBusinessLogics/BusinessLogics/VisitLogic.cs

117 lines
3.8 KiB
C#
Raw Normal View History

2024-05-01 01:24:55 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.BusinessLogicsContracts;
using VeterinaryClinicContracts.SearchModels;
using VeterinaryClinicContracts.StoragesContracts;
using VeterinaryClinicContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace VeterinaryClinicBusinessLogics.BusinessLogics
{
public class VisitLogic : IVisitLogic
{
private readonly ILogger _logger;
private readonly IVisitStorage _visitStorage;
public VisitLogic(ILogger<VisitLogic> logger, IVisitStorage visitStorage)
{
_logger = logger;
_visitStorage = visitStorage;
}
public bool Create(VisitBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Visit.Id: {Id}", model.Id);
if (_visitStorage.Insert(model) == null)
{
_logger.LogWarning("Insert 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;
}
public VisitViewModel? ReadElement(VisitSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. DateFrom:{DateFrom}. DateTo:{DateTo}. VisitId:{Id}", model.DateFrom, model.DateTo, model.Id);
var element = _visitStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. VisitId:{Id}", element.Id);
return element;
}
public List<VisitViewModel>? ReadList(VisitSearchModel? model)
{
_logger.LogInformation("ReadList. VisitId:{Id}", 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 Update(VisitBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Visit.Id: {Id}", model.Id);
if (_visitStorage.Update(model) == null)
{
_logger.LogWarning("Update 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 (model.Date == DateTime.MinValue)
{
throw new ArgumentNullException("Не указана дата визита", nameof(model.Date));
}
if (model.UserId <= 0)
{
throw new ArgumentNullException("Не указан идентификатор пользователя", nameof(model.UserId));
}
_logger.LogInformation("CheckModel. Visit.Id: {Id}", model.Id);
}
}
}