CourseWork_Borschevskaya_A..../Hospital/HospitalBusinessLogic/MedicineLogic.cs

114 lines
3.8 KiB
C#

using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StorageContracts;
using HospitalContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace HospitalBusinessLogic
{
public class MedicineLogic : IMedicineLogic
{
private readonly ILogger _logger;
private readonly IMedicineStorage _medicineStorage;
public MedicineLogic(ILogger<MedicineLogic> logger, IMedicineStorage medicineStorage)
{
_logger = logger;
_medicineStorage = medicineStorage;
}
public MedicineViewModel? ReadElement(MedicineSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name: {Name}. Id:{ Id}", model.Name, model.Id);
var element = _medicineStorage.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<MedicineViewModel>? ReadList(MedicineSearchModel? model)
{
_logger.LogInformation("ReadList. Name: {Name}. Id:{ Id}", model?.Name, model?.Id);
var list = model == null ? _medicineStorage.GetFullList() : _medicineStorage.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(MedicineBindingModel model)
{
CheckModel(model);
if (_medicineStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(MedicineBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_medicineStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool Update(MedicineBindingModel model)
{
CheckModel(model);
if (_medicineStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(MedicineBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия лекарства",
nameof(model.Name));
}
if (model.Cost <= 0)
{
throw new ArgumentNullException("Стоимость лекарства должна быть больше 0",
nameof(model.Cost));
}
if (string.IsNullOrEmpty(model.Dose))
{
throw new ArgumentNullException("Нет дозировки лекарства",
nameof(model.Dose));
}
_logger.LogInformation("Medicine. Name:{ Name}. Cost: {Cost}. Dose: {Dose}. Id: { Id}", model.Name, model.Cost, model.Dose, model.Id);
}
}
}