PIbd22_Kuznetsov_A.V._Sewin.../SewingDresses/SewingDressesBusinessLogic/BusinessLogic/DressLogic.cs

112 lines
3.9 KiB
C#
Raw 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 SewingDressesContracts.BindingModels;
using SewingDressesContracts.BusinessLogicsContracts;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.ViewModels;
namespace SewingDressesBusinessLogic.BusinessLogic
{
public class DressLogic : IDressLogic
{
private readonly ILogger _logger;
private readonly IDressStorage _dressStorage;
public DressLogic(ILogger<DressLogic> logger, IDressStorage dressStorage)
{
_logger = logger;
_dressStorage = dressStorage;
}
public List<DressViewModel>? ReadList(DressSearchModel? model)
{
_logger.LogInformation("ReadList. DressName:{DressName}. Id:{Id}", model?.DressName, model?.Id);
var list = model == null ? _dressStorage.GetFullList() : _dressStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public DressViewModel? ReadElement(DressSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. DressName:{DressName}. Id: {Id}", model.DressName, model.Id);
var element = _dressStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
private void CheckModel(DressBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.DressName))
{
throw new ArgumentNullException("Нет названия платья", nameof(model.DressComponents));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена платья должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Dress. DressName:{DressName}. Price:{Price}. Id:{Id}", model.DressName, model.Price, model.Id);
var element = _dressStorage.GetElement(new DressSearchModel
{
DressName = model.DressName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Платье с таким названием уже есть");
}
}
public bool Create(DressBindingModel model)
{
CheckModel(model);
if (_dressStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(DressBindingModel model)
{
CheckModel(model);
if (_dressStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(DressBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_dressStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
}
}