2024-04-29 23:05:12 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using PolyclinicContracts.BindingModels;
|
2024-04-29 22:01:58 +04:00
|
|
|
|
using PolyclinicContracts.BusinessLogicsContracts;
|
|
|
|
|
using PolyclinicContracts.SearchModels;
|
2024-04-29 23:05:12 +04:00
|
|
|
|
using PolyclinicContracts.StoragesContracts;
|
2024-04-29 22:01:58 +04:00
|
|
|
|
using PolyclinicContracts.ViewModels;
|
|
|
|
|
|
|
|
|
|
namespace PolyclinicBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class MedicamentLogic : IMedicamentLogic
|
|
|
|
|
{
|
2024-04-29 23:05:12 +04:00
|
|
|
|
private readonly ILogger logger;
|
|
|
|
|
private readonly IMedicamentStorage medicamentStorage;
|
|
|
|
|
|
|
|
|
|
public MedicamentLogic(ILogger<MedicamentLogic> logger, IMedicamentStorage medicamentStorage)
|
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.medicamentStorage = medicamentStorage;
|
|
|
|
|
}
|
2024-04-29 22:01:58 +04:00
|
|
|
|
public bool Create(MedicamentBindingModel model)
|
|
|
|
|
{
|
2024-04-29 23:05:12 +04:00
|
|
|
|
CheckModel(model);
|
|
|
|
|
if(medicamentStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
logger.LogWarning("Create operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2024-04-29 22:01:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(MedicamentBindingModel model)
|
|
|
|
|
{
|
2024-04-29 23:05:12 +04:00
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
if (medicamentStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
|
return true;
|
2024-04-29 22:01:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MedicamentViewModel? ReadElement(MedicamentSearchModel model)
|
|
|
|
|
{
|
2024-04-29 23:05:12 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
logger.LogInformation("ReadElement. MedicamentName:{Name}.Id:{ Id}", model.Name, model.Id);
|
|
|
|
|
var element = medicamentStorage.GetElement(model);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
|
|
|
|
return element;
|
2024-04-29 22:01:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<MedicamentViewModel>? ReadList(MedicamentSearchModel? model)
|
|
|
|
|
{
|
2024-04-29 23:05:12 +04:00
|
|
|
|
logger.LogInformation("ReadList. Name:{Name}. Id:{ Id}", model?.Name, model?.Id);
|
|
|
|
|
var list = model == null ? medicamentStorage.GetFullList() : medicamentStorage.GetFilteredList(model);
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
return list;
|
2024-04-29 22:01:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Update(MedicamentBindingModel model)
|
|
|
|
|
{
|
2024-04-29 23:05:12 +04:00
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (medicamentStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(MedicamentBindingModel 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 (string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет названия у препарата", nameof(model.Comment));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var element = medicamentStorage.GetElement(new MedicamentSearchModel
|
|
|
|
|
{
|
|
|
|
|
Name = model.Name
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Препарат с таким названием уже есть");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("Medicament. Comment:{Comment}. Id: { Id}", model.Comment, model.Id);
|
2024-04-29 22:01:58 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|