125 lines
4.2 KiB
C#
125 lines
4.2 KiB
C#
|
using Microsoft.Extensions.Logging;
|
|||
|
using HospitalContracts.BindingModels;
|
|||
|
using HospitalContracts.BusinessLogicsContracts;
|
|||
|
using HospitalContracts.SearchModels;
|
|||
|
using HospitalContracts.StoragesContracts;
|
|||
|
using HospitalContracts.ViewModels;
|
|||
|
|
|||
|
namespace HospitalBusinessLogic.BusinessLogics
|
|||
|
{
|
|||
|
public class DrugLogic : IDrugLogic
|
|||
|
{
|
|||
|
private readonly ILogger logger;
|
|||
|
private readonly IDrugStorage drugStorage;
|
|||
|
|
|||
|
public DrugLogic(ILogger<DrugLogic> logger, IDrugStorage drugStorage)
|
|||
|
{
|
|||
|
this.logger = logger;
|
|||
|
this.drugStorage = drugStorage;
|
|||
|
}
|
|||
|
public bool Create(DrugBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if(drugStorage.Insert(model) == null)
|
|||
|
{
|
|||
|
logger.LogWarning("Create operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public bool Delete(DrugBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model, false);
|
|||
|
if (drugStorage.Delete(model) == null)
|
|||
|
{
|
|||
|
logger.LogWarning("Delete operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public DrugViewModel? ReadElement(DrugSearchModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
logger.LogInformation("ReadElement. DrugName:{Name}.Id:{ Id}", model.Name, model.Id);
|
|||
|
var element = drugStorage.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<DrugViewModel>? ReadList(DrugSearchModel? model)
|
|||
|
{
|
|||
|
logger.LogInformation("ReadList. Name:{Name}. Id:{ Id}", model?.Name, model?.Id);
|
|||
|
var list = model == null ? drugStorage.GetFullList() : drugStorage.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(DrugBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (drugStorage.Update(model) == null)
|
|||
|
{
|
|||
|
logger.LogWarning("Update operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private void CheckModel(DrugBindingModel model, bool withParams = true)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
if (!withParams)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
if (model.SymptomId <= 0)
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Неверный идентификатор симптома", nameof(model));
|
|||
|
}
|
|||
|
if (model.ProcedureId <= 0)
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Неверный идентификатор процедуры", nameof(model));
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(model.Comment))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Нет комментария", nameof(model.Comment));
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(model.Name))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Нет названия препарата", nameof(model.Comment));
|
|||
|
}
|
|||
|
|
|||
|
var element = drugStorage.GetElement(new DrugSearchModel
|
|||
|
{
|
|||
|
Name = model.Name
|
|||
|
});
|
|||
|
|
|||
|
if (element != null && element.Id != model.Id)
|
|||
|
{
|
|||
|
throw new InvalidOperationException("Препарат с таким названием уже есть");
|
|||
|
}
|
|||
|
|
|||
|
logger.LogInformation("Drug. Comment:{Comment}. Id: { Id}", model.Comment, model.Id);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|