2024-04-29 22:50:40 +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:40 +04:00
|
|
|
|
using PolyclinicContracts.StoragesContracts;
|
2024-04-29 22:01:58 +04:00
|
|
|
|
using PolyclinicContracts.ViewModels;
|
|
|
|
|
|
|
|
|
|
namespace PolyclinicBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class RecipeLogic : IRecipeLogic
|
|
|
|
|
{
|
2024-04-29 22:50:40 +04:00
|
|
|
|
|
|
|
|
|
private readonly ILogger logger;
|
|
|
|
|
private readonly IRecipeStorage recipeStorage;
|
|
|
|
|
|
|
|
|
|
public RecipeLogic(ILogger<RecipeLogic> logger, IRecipeStorage recipeStorage)
|
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.recipeStorage = recipeStorage;
|
|
|
|
|
}
|
2024-04-29 22:01:58 +04:00
|
|
|
|
public bool Create(RecipeBindingModel model)
|
|
|
|
|
{
|
2024-04-29 22:50:40 +04:00
|
|
|
|
CheckModel(model);
|
|
|
|
|
if(recipeStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
logger.LogWarning("Create operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2024-04-29 22:01:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(RecipeBindingModel model)
|
|
|
|
|
{
|
2024-04-29 22:50:40 +04:00
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
if(recipeStorage.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 RecipeViewModel? ReadElement(RecipeSearchModel model)
|
|
|
|
|
{
|
2024-04-29 22:50:40 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
logger.LogInformation("ReadElement. Comment:{Comment}.Id:{ Id}", model.Comment, model.Id);
|
|
|
|
|
var element = recipeStorage.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<RecipeViewModel>? ReadList(RecipeSearchModel? model)
|
|
|
|
|
{
|
2024-04-29 22:50:40 +04:00
|
|
|
|
logger.LogInformation("ReadList. Comment:{Comment}. Id:{ Id}", model?.Comment, model?.Id);
|
|
|
|
|
var list = model == null ? recipeStorage.GetFullList() : recipeStorage.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(RecipeBindingModel model)
|
|
|
|
|
{
|
2024-04-29 22:50:40 +04:00
|
|
|
|
CheckModel(model);
|
|
|
|
|
if(recipeStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(RecipeBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(model.ProceduresCount <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Количество процедур не может быть равно нулю или быть меньше нуля", nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.Comment))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет комментария", nameof(model.Comment));
|
|
|
|
|
}
|
|
|
|
|
logger.LogInformation("Recipe. Comment:{Comment}. Id: { Id}", model.Comment, model.Id);
|
2024-04-29 22:01:58 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|