Romanov_E.V._CourseWork_Vet.../VeterinaryClinicBusinessLogic/BusinessLogics/MedicationLogic.cs

110 lines
4.2 KiB
C#
Raw Normal View History

2023-04-07 14:13:18 +04:00
using Microsoft.Extensions.Logging;
using VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.BusinessLogicsContracts;
using VeterinaryClinicContracts.SearchModels;
using VeterinaryClinicContracts.StorageContracts;
using VeterinaryClinicContracts.ViewModels;
namespace VeterinaryClinicBusinessLogic.BusinessLogics
{
public class MedicationLogic : IMedicationLogic
{
private readonly ILogger _logger;
private readonly IMedicationStorage _MedicationStorage;
public MedicationLogic(ILogger<MedicationLogic> logger, IMedicationStorage
MedicationStorage)
{
_logger = logger;
_MedicationStorage = MedicationStorage;
}
public List<MedicationViewModel>? ReadList(MedicationSearchModel? model)
{
_logger.LogInformation("ReadList. MedicationName:{MedicationName}.Id:{ Id}", model?.MedicationName, model?.Id);
var list = model == null ? _MedicationStorage.GetFullList() : _MedicationStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public MedicationViewModel? ReadElement(MedicationSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. MedicationName:{MedicationName}.Id:{ Id}", model.MedicationName, model.Id);
var element = _MedicationStorage.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(MedicationBindingModel model)
{
CheckModel(model);
if (_MedicationStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(MedicationBindingModel model)
{
CheckModel(model);
if (_MedicationStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(MedicationBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_MedicationStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(MedicationBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.MedicationName))
{
throw new ArgumentNullException("Нет названия компонента",
nameof(model.MedicationName));
}
if (model.Cost <= 0)
{
throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost));
}
_logger.LogInformation("Medication. MedicationName:{MedicationName}. Cost:{ Cost}. Id: { Id}", model.MedicationName, model.Cost, model.Id);
var element = _MedicationStorage.GetElement(new MedicationSearchModel
{
MedicationName = model.MedicationName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
}
}