CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ProductLogic.cs

104 lines
3.8 KiB
C#
Raw Permalink Normal View History

2024-04-29 01:28:41 +04:00
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Enums;
2024-04-29 01:28:41 +04:00
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)
{
2024-04-30 21:31:36 +04:00
if (!withParams) {
return;
}
if (string.IsNullOrEmpty(model.ProductName)) {
2024-04-29 01:28:41 +04:00
throw new ArgumentNullException("Нет названия продукта", nameof(model.ProductName));
2024-04-30 21:31:36 +04:00
}
if (model.Price <= 0) {
2024-04-29 01:28:41 +04:00
throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price));
2024-04-30 21:31:36 +04:00
}
_logger.LogInformation($"Product. ID:{model.ID}.ProductName:{model.ProductName}.Price:{model.Price}.CostItemID:{model.CostItemID}");
2024-04-29 01:28:41 +04:00
var element = _storage.GetElement(new ProductSearchModel { ProductName = model.ProductName });
2024-04-30 22:48:51 +04:00
if (element != null && element.ProductName != model.ProductName) {
2024-04-29 01:28:41 +04:00
throw new InvalidOperationException("Продукт с таким названием уже есть");
2024-04-30 21:31:36 +04:00
}
2024-04-29 01:28:41 +04:00
}
}
}