CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ProductLogic.cs
Илья Федотов 134f7d6cef Business logic 0.2
2024-04-29 01:28:41 +04:00

100 lines
3.8 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 ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopBusinessLogic.BusinessLogic
{
public class ProductLogic : IProductLogic
{
private readonly ILogger _logger;
private readonly IProductStorage _storage;
public ProductLogic(ILogger<ProductLogic> logger, IProductStorage productStorage) {
_logger = logger;
_storage = productStorage;
}
public bool Create(ProductBindingModel model)
{
CheckModel(model);
if (_storage.Insert(model) == null) {
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ProductBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation($"Delete. ID:{model.ID}");
if (_storage.Delete(model) == null) {
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool Update(ProductBindingModel model)
{
CheckModel(model);
if (_storage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public ProductViewModel? ReadElement(ProductSearchModel? model)
{
if (model == null) throw new ArgumentNullException(nameof(model));
_logger.LogInformation($"ReadElement. ProductName:{model.ProductName}. ID:{model.ID}");
var element = _storage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement. elementn not found");
return null;
}
_logger.LogInformation($"ReadElement find. ID:{element.ID}");
return element;
}
public List<ProductViewModel>? ReadList(ProductSearchModel? model)
{
_logger.LogInformation($"ReadList. ProductName:{model?.ProductName}. ID:{model?.ID}");
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation($"ReadList. Count:{list.Count}");
return list;
}
private void CheckModel(ProductBindingModel 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. ID:{model.ID}.ProductName:{model.ProductName}.Price:{model.Price}.Count:{model.Count}" +
$".CategoryID:{model.CategoryID}");
var element = _storage.GetElement(new ProductSearchModel { ProductName = model.ProductName });
if (element != null && element.ID != model.ID)
throw new InvalidOperationException("Продукт с таким названием уже есть");
}
}
}