SUBD/Sushi/SushiBusinessLogic/SushiLogic.cs
2024-04-18 11:16:56 +04:00

114 lines
3.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using SushiContracts.BindingModels;
using SushiContracts.BusinessLogicsContracts;
using SushiContracts.SearchModels;
using SushiContracts.StoragesContracts;
using SushiContracts.ViewModels;
namespace SushiBusinessLogic
{
public class SushiLogic : ISushiLogic
{
private readonly ILogger _logger;
private readonly ISushiStorage _DishStorage;
public SushiLogic(ILogger<SushiLogic> logger, ISushiStorage DishStorage)
{
_logger = logger;
_DishStorage = DishStorage;
}
public List<SushiViewModel>? ReadList(SushiSearchModel? model)
{
_logger.LogInformation("ReadList. DishName:{DishName}. Id:{ Id}", model?.SushiName, model?.Id);
var list = model == null ? _DishStorage.GetFullList() :
_DishStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public SushiViewModel? ReadElement(SushiSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. DishName:{DishName}.Id:{ Id}", model.SushiName, model.Id);
var element = _DishStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(SushiBindingModel model)
{
CheckModel(model);
if (_DishStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(SushiBindingModel model)
{
CheckModel(model);
if (_DishStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(SushiBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_DishStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(SushiBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.SushiName))
{
throw new ArgumentNullException("Нет названия блюдаs",
nameof(model.SushiName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена блюдa должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Dish. Dish:{Dish}. Price:{ Price }. Id: { Id}", model.SushiName, model.Price, model.Id);
var element = _DishStorage.GetElement(new SushiSearchModel
{
SushiName = model.SushiName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
}
}