логика сборки + логика покупок (подкорректированы purchaseBinding и BuildSearch)

This commit is contained in:
dex_moth 2024-04-29 13:31:37 +04:00
parent 06ec3952d3
commit 10ac29df01
6 changed files with 322 additions and 5 deletions

View File

@ -0,0 +1,113 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.BusinessLogicsContracts;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace ComputerHardwareStoreBusinessLogic.BusinessLogic
{
public class BuildLogic : IBuildLogic
{
private readonly ILogger _logger;
private readonly IBuildStorage _buildStorage;
public BuildLogic(ILogger<BuildLogic> logger, IBuildStorage buildStorage)
{
_logger = logger;
_buildStorage = buildStorage;
}
public List<BuildViewModel>? ReadList(BuildSearchModel? model)
{
_logger.LogInformation("ReadList Build. Name:{Name}. Id:{ Id}", model?.Name, model?.Id);
var list = model == null ? _buildStorage.GetFullList() :
_buildStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList Build return null list");
return null;
}
_logger.LogInformation("ReadList Build. Count:{Count}", list.Count);
return list;
}
public BuildViewModel? ReadElement(BuildSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement Build. Name:{Name}. Id:{ Id}", model.Name, model.Id);
var element = _buildStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement Build element not found");
return null;
}
_logger.LogInformation("ReadElement Build find. Id:{Id}", element.Id);
return element;
}
public bool Create(BuildBindingModel model)
{
CheckModel(model);
if (_buildStorage.Insert(model) == null)
{
_logger.LogWarning("Insert Build operation failed");
return false;
}
return true;
}
public bool Update(BuildBindingModel model)
{
CheckModel(model);
if (_buildStorage.Update(model) == null)
{
_logger.LogWarning("Update Build operation failed");
return false;
}
return true;
}
public bool Delete(BuildBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_buildStorage.Delete(model) == null)
{
_logger.LogWarning("Delete Build operation failed");
return false;
}
return true;
}
private void CheckModel(BuildBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия сборки", nameof(model.Name));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена сборки должна быть больше 0", nameof(model.Price));
}
var element = _buildStorage.GetElement(new BuildSearchModel
{
Name = model.Name
});
if (element != null)
{
throw new InvalidOperationException("Сборка с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,101 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.BusinessLogicsContracts;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace ComputerHardwareStoreBusinessLogic.BusinessLogic
{
public class CommentLogic : ICommentLogic
{
private readonly ILogger _logger;
private readonly ICommentStorage _commentStorage;
public CommentLogic(ILogger<CommentLogic> logger, ICommentStorage commentStorage)
{
_logger = logger;
_commentStorage = commentStorage;
}
public List<CommentViewModel>? ReadList(CommentSearchModel? model)
{
_logger.LogInformation("ReadList Comment. Date:{Date}. Id:{Id}", model?.Date, model?.Id);
var list = model == null ? _commentStorage.GetFullList() :
_commentStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList Comment return null list");
return null;
}
_logger.LogInformation("ReadList Comment. Count:{Count}", list.Count);
return list;
}
public CommentViewModel? ReadElement(CommentSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement Comment. Date:{Date}. Id:{ Id}", model.Date, model.Id);
var element = _commentStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement Comment element not found");
return null;
}
_logger.LogInformation("ReadElement Comment find. Id:{Id}", element.Id);
return element;
}
public bool Create(CommentBindingModel model)
{
CheckModel(model);
if (_commentStorage.Insert(model) == null)
{
_logger.LogWarning("Insert Comment operation failed");
return false;
}
return true;
}
public bool Update(CommentBindingModel model)
{
CheckModel(model);
if (_commentStorage.Update(model) == null)
{
_logger.LogWarning("Update Comment operation failed");
return false;
}
return true;
}
public bool Delete(CommentBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_commentStorage.Delete(model) == null)
{
_logger.LogWarning("Delete Comment operation failed");
return false;
}
return true;
}
private void CheckModel(CommentBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Date))
{
throw new ArgumentNullException("Нет даты у комментария", nameof(model.Date));
}
}
}
}

View File

@ -0,0 +1,105 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.BusinessLogicsContracts;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace ComputerHardwareStoreBusinessLogic.BusinessLogic
{
public class PurchaseLogic : IPurchaseLogic
{
private readonly ILogger _logger;
private readonly IPurchaseStorage _purchaseStorage;
public PurchaseLogic(ILogger<PurchaseLogic> logger, IPurchaseStorage purchaseStorage)
{
_logger = logger;
_purchaseStorage = purchaseStorage;
}
public List<PurchaseViewModel>? ReadList(PurchaseSearchModel? model)
{
_logger.LogInformation("ReadList Purchase. Id:{ Id}", model?.Id);
var list = model == null ? _purchaseStorage.GetFullList() :
_purchaseStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList Purchase return null list");
return null;
}
_logger.LogInformation("ReadList Purchase. Count:{Count}", list.Count);
return list;
}
public PurchaseViewModel? ReadElement(PurchaseSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement Purchase. Id:{ Id}", model.Id);
var element = _purchaseStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement Purchase element not found");
return null;
}
_logger.LogInformation("ReadElement Purchase find. Id:{Id}", element.Id);
return element;
}
public bool Create(PurchaseBindingModel model)
{
CheckModel(model);
if (_purchaseStorage.Insert(model) == null)
{
_logger.LogWarning("Insert Purchase operation failed");
return false;
}
return true;
}
public bool Update(PurchaseBindingModel model)
{
CheckModel(model);
if (_purchaseStorage.Update(model) == null)
{
_logger.LogWarning("Update Purchase operation failed");
return false;
}
return true;
}
public bool Delete(PurchaseBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_purchaseStorage.Delete(model) == null)
{
_logger.LogWarning("Delete Purchase operation failed");
return false;
}
return true;
}
private void CheckModel(PurchaseBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.DateCreate))
{
throw new ArgumentNullException("Нет даты создания покупки", nameof(model.Date));
}
if (model.Cost <= 0)
{
throw new ArgumentNullException("Стоимость покупки должна быть больше 0", nameof(model.Cost));
}
}
}
}

View File

@ -5,10 +5,9 @@ namespace ComputerHardwareStoreContracts.BindingModels
public class PurchaseBindingModel : IPurchaseModel public class PurchaseBindingModel : IPurchaseModel
{ {
public int Id { get; set; } public int Id { get; set; }
public int VendorId { get; set; }
public double Cost { get; set; } public double Cost { get; set; }
public DateTime DateCreate { get; set; } public DateTime DateCreate { get; set; }
public int VendorId { get; set; }
public double Sum { get; set; }
public Dictionary<int, (IBuildModel, int)> PurchaseBuild { get; set; } = new(); public Dictionary<int, (IBuildModel, int)> PurchaseBuild { get; set; } = new();
public Dictionary<int, (IProductModel, int)> PurchaseProduct { get; set; } = new(); public Dictionary<int, (IProductModel, int)> PurchaseProduct { get; set; } = new();
} }

View File

@ -3,6 +3,6 @@
public class BuildSearchModel public class BuildSearchModel
{ {
public int? Id { get; set; } public int? Id { get; set; }
public string? BuildName { get; set; } public string? Name { get; set; }
} }
} }

View File

@ -2,10 +2,9 @@
{ {
public interface IPurchaseModel : IId public interface IPurchaseModel : IId
{ {
int VendorId { get; }
double Cost { get; } double Cost { get; }
DateTime DateCreate { get; } DateTime DateCreate { get; }
int VendorId { get; }
double Sum { get; }
public Dictionary<int, (IBuildModel, int)> PurchaseBuild { get; } public Dictionary<int, (IBuildModel, int)> PurchaseBuild { get; }
public Dictionary<int, (IProductModel, int)> PurchaseProduct { get; } public Dictionary<int, (IProductModel, int)> PurchaseProduct { get; }
} }