96 lines
3.8 KiB
C#
Raw Normal View History

2024-02-24 21:32:11 +04:00
using DinerContracts.BindingModels;
using DinerContracts.BusinessLogicsContacts;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DineryBusinessLogic.BusinessLogic
{
public class SnackLogic : ISnackLogic
{
private readonly ILogger _logger;
private readonly ISnackStorage _productStorage;
public SnackLogic(ILogger<SnackLogic> logger, ISnackStorage productStorage) {
_logger = logger;
_productStorage = productStorage;
}
public bool Create(SnackBindingModel model)
{
CheckModel(model);
if (_productStorage.Insert(model) == null) {
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(SnackBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. ID:{ID}", model.ID);
if (_productStorage.Delete(model) == null) {
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public SnackViewModel? ReadElement(SnackSearchModel model)
{
if (model == null) throw new ArgumentNullException(nameof(model));
_logger.LogInformation("ReadElement. ProductName:{ProductName}. ID:{ID}", model.ProductName, model.ID);
var element = _productStorage.GetElement(model);
if (element == null) {
_logger.LogWarning("ReadElement. elementn not found");
return null;
}
_logger.LogInformation("ReadElement find. ID:{ID}", element.ID);
return element;
}
public List<SnackViewModel>? ReadList(SnackSearchModel? model)
{
_logger.LogInformation("ReadList. ProductName:{ProductName}. ID:{ID}", model?.ProductName, model?.ID);
var list = model == null ? _productStorage.GetFullList() : _productStorage.GetFilteredList(model);
if (list == null) {
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(SnackBindingModel model)
{
CheckModel(model);
if (_productStorage.Update(model) == null) {
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(SnackBindingModel model, bool withParams = true) {
if (model == null) throw new ArgumentNullException(nameof(model));
if (!withParams) return;
if (string.IsNullOrEmpty(model.ProductName))
throw new ArgumentNullException("Нет названия продукта", nameof(model.ProductName));
if (model.Price <= 0)
throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price));
_logger.LogInformation("Product. ProductName:{ProductName}. Price:{Price}. ID:{ID}",
2024-03-16 18:07:46 +04:00
model.ProductName, model.Price, model.ID);
2024-02-24 21:32:11 +04:00
var element = _productStorage.GetElement(new SnackSearchModel { ProductName = model.ProductName });
if (element != null && element.ID != model.ID)
throw new InvalidOperationException("Продукт с таким названием уже есть");
}
}
}