CourseWork_Borschevskaya_A..../Hospital/HospitalBusinessLogic/RecipeLogic.cs

104 lines
3.3 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 RecipeLogic : IRecipeLogic
{
private readonly ILogger _logger;
private readonly IRecipeStorage _recipeStorage;
public RecipeLogic(ILogger<RecipeLogic> logger, IRecipeStorage recipeStorage)
{
_logger = logger;
_recipeStorage = recipeStorage;
}
public RecipeViewModel? ReadElement(RecipeSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{ Id}", 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;
}
public List<RecipeViewModel>? ReadList(RecipeSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", 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;
}
public bool Create(RecipeBindingModel model)
{
CheckModel(model);
if (_recipeStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(RecipeBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_recipeStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool Update(RecipeBindingModel model)
{
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 (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия рецепта",
nameof(model.Name));
}
_logger.LogInformation("Recipe. Date:{ Date}. Name: {Name}. Id: { Id}", model.Date, model.Name, model.Id);
}
}
}