CourseWork_Borschevskaya_A..../Hospital/HospitalBusinessLogic/PrescriptionLogic.cs

104 lines
3.5 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 PrescriptionLogic : IPrescriptionLogic
{
private readonly ILogger _logger;
private readonly IPrescriptionStorage _prescriptionStorage;
public PrescriptionLogic(ILogger<PrescriptionLogic> logger, IPrescriptionStorage prescriptionStorage)
{
_logger = logger;
_prescriptionStorage = prescriptionStorage;
}
public PrescriptionViewModel? ReadElement(PrescriptionSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
var element = _prescriptionStorage.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<PrescriptionViewModel>? ReadList(PrescriptionSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _prescriptionStorage.GetFullList() : _prescriptionStorage.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(PrescriptionBindingModel model)
{
CheckModel(model);
if (_prescriptionStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(PrescriptionBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_prescriptionStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool Update(PrescriptionBindingModel model)
{
CheckModel(model);
if (_prescriptionStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(PrescriptionBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.Number <= 0)
{
throw new ArgumentNullException("Количество лекарств в поставке должно быть больше 0", nameof(model.Number));
}
_logger.LogInformation("Prescription. Date:{ Date}. Number: {Number}. MedicineId: {MedicineId}. Id: { Id}", model.Date, model.Number, model.MedicineId, model.Id);
}
}
}