2024-04-29 22:50:21 +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 22:50:21 +04:00
|
|
|
|
using PolyclinicContracts.StoragesContracts;
|
2024-04-29 22:01:58 +04:00
|
|
|
|
using PolyclinicContracts.ViewModels;
|
|
|
|
|
|
|
|
|
|
namespace PolyclinicBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class ProcedureLogic : IProcedureLogic
|
|
|
|
|
{
|
2024-04-29 22:50:21 +04:00
|
|
|
|
|
|
|
|
|
private readonly ILogger logger;
|
|
|
|
|
private readonly IProcedureStorage procedureStorage;
|
|
|
|
|
|
|
|
|
|
public ProcedureLogic(ILogger<ProcedureLogic> logger, IProcedureStorage procedureStorage)
|
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.procedureStorage = procedureStorage;
|
|
|
|
|
}
|
2024-04-29 22:01:58 +04:00
|
|
|
|
public bool Create(ProcedureBindingModel model)
|
|
|
|
|
{
|
2024-04-29 22:50:21 +04:00
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (procedureStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2024-04-29 22:01:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(ProcedureBindingModel model)
|
|
|
|
|
{
|
2024-04-29 22:50:21 +04:00
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("Delete Id:{Id}", model.Id);
|
|
|
|
|
if(procedureStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2024-04-29 22:01:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ProcedureViewModel? ReadElement(ProcedureSearchModel model)
|
|
|
|
|
{
|
2024-04-29 22:50:21 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
logger.LogInformation("ReadElement. ProcedureName:{ProcedureName}.Id:{ Id}", model.Name, model.Id);
|
|
|
|
|
var element = procedureStorage.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<ProcedureViewModel>? ReadList(ProcedureSearchModel? model)
|
|
|
|
|
{
|
2024-04-29 22:50:21 +04:00
|
|
|
|
logger.LogInformation("ReadList. Name:{Name}. Id:{ Id}", model?.Name, model?.Id);
|
|
|
|
|
var list = model == null ? procedureStorage.GetFullList() : procedureStorage.GetFilteredList(model);
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
logger.LogWarning("ReadList return null list");
|
2024-05-29 12:35:30 +04:00
|
|
|
|
return new List<ProcedureViewModel>();
|
2024-04-29 22:50:21 +04:00
|
|
|
|
}
|
|
|
|
|
logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
return list;
|
2024-04-29 22:01:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Update(ProcedureBindingModel model)
|
|
|
|
|
{
|
2024-04-29 22:50:21 +04:00
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (procedureStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(ProcedureBindingModel 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));
|
|
|
|
|
}
|
|
|
|
|
logger.LogInformation("Procedure. Name:{Name}. Id: { Id}", model.Name, model.Id);
|
|
|
|
|
|
|
|
|
|
var element = procedureStorage.GetElement(new ProcedureSearchModel
|
|
|
|
|
{
|
|
|
|
|
Name = model.Name
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Процедура с таким названием уже есть");
|
|
|
|
|
}
|
2024-04-29 22:01:58 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|