128 lines
4.0 KiB
C#
128 lines
4.0 KiB
C#
using FlowerShopContracts.BindingModels;
|
||
using FlowerShopContracts.BusinessLogicsContracts;
|
||
using FlowerShopContracts.SearchModels;
|
||
using FlowerShopContracts.StoragesContracts;
|
||
using FlowerShopContracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace FlowerShopBusinessLogic.BusinessLogics
|
||
{
|
||
public class BouquetLogic : IBouquetLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IBouquetStorage _bouquetStorage;
|
||
|
||
public BouquetLogic(ILogger<BouquetLogic> logger, IBouquetStorage bouquetStorage)
|
||
{
|
||
_logger = logger;
|
||
_bouquetStorage = bouquetStorage;
|
||
}
|
||
|
||
public List<BouquetViewModel>? ReadList(BouquetSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. BouquetName: {BouquetName}. Id: {Id}", model?.BouquetName, model?.Id);
|
||
|
||
var list = model == null ? _bouquetStorage.GetFullList() : _bouquetStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
|
||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||
return list;
|
||
}
|
||
|
||
public BouquetViewModel? ReadElement(BouquetSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
|
||
_logger.LogInformation("ReadElement. BouquetName: {BouquetName}. Id: {Id}", model.BouquetName, model.Id);
|
||
|
||
var element = _bouquetStorage.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(BouquetBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
|
||
if (_bouquetStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public bool Update(BouquetBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
|
||
if (_bouquetStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(BouquetBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
|
||
_logger.LogInformation("Delete. Id: {Id}", model.Id);
|
||
if (_bouquetStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
private void CheckModel(BouquetBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(model.BouquetName))
|
||
{
|
||
throw new ArgumentNullException("Нет названия букета", nameof(model.BouquetName));
|
||
}
|
||
|
||
if (model.Price <= 0)
|
||
{
|
||
throw new ArgumentNullException("Цена букета должна быть больше 0", nameof(model.Price));
|
||
}
|
||
|
||
_logger.LogInformation("Bouquet. BouquetName: {BouquetName}. Price: {Price}. Id: {Id}", model.BouquetName, model.Price, model.Id);
|
||
|
||
var element = _bouquetStorage.GetElement(new BouquetSearchModel { BouquetName = model.BouquetName });
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Компонент с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|