CourseWork_CompShop/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/AssemblyLogic.cs

155 lines
5.5 KiB
C#
Raw Normal View History

2023-04-05 18:31:49 +04:00
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
2023-05-17 13:52:06 +04:00
using ComputerShopDataModels.Models;
2023-04-05 18:31:49 +04:00
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopBusinessLogic.BusinessLogics
{
public class AssemblyLogic : IAssemblyLogic
{
private readonly ILogger _logger;
private readonly IAssemblyStorage _assemblyStorage;
2023-05-17 13:52:06 +04:00
private readonly IComponentStorage _componentStorage;
2023-04-05 18:31:49 +04:00
2023-05-17 13:52:06 +04:00
public AssemblyLogic(ILogger<AssemblyLogic> logger, IAssemblyStorage assemblyStorage, IComponentStorage componentStorage)
2023-04-05 18:31:49 +04:00
{
_logger = logger;
_assemblyStorage = assemblyStorage;
2023-05-17 13:52:06 +04:00
_componentStorage = componentStorage;
2023-04-05 18:31:49 +04:00
}
public bool Create(AssemblyBindingModel model)
{
CheckModel(model);
if (_assemblyStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(AssemblyBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_assemblyStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public AssemblyViewModel? ReadElement(AssemblySearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. AssemblyName:{AssemblyName}.Id:{ Id}", model.AssemblyName, model.Id);
var element = _assemblyStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<AssemblyViewModel>? ReadList(AssemblySearchModel? model)
{
_logger.LogInformation("ReadList. AssemblyName:{AssemblyName}.Id:{ Id}", model?.AssemblyName, model?.Id);
var list = model == null ? _assemblyStorage.GetFullList() : _assemblyStorage.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(AssemblyBindingModel model)
{
CheckModel(model);
if (_assemblyStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
2023-05-17 13:52:06 +04:00
public bool AddComponentToAssembly(AssemblySearchModel model, ComponentSearchModel componentmodel, int amount)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("AddComponentToAssembly. AssemblyName:{AssemblyName}.Id:{ Id}", model.AssemblyName, model.Id);
var element = _assemblyStorage.GetElement(model);
var component = _componentStorage.GetElement(componentmodel);
if (element == null || component == null)
{
return false;
}
_logger.LogInformation("AddComponentToAssembly find. Id:{Id}", element.Id);
element.AssemblyComponents[component.Id] = (component, amount);
_assemblyStorage.Update(new()
{
Id = element.Id,
AssemblyName = element.AssemblyName,
Price = element.Price + component.Cost * amount,
ClientId = element.ClientId,
AssemblyComponents = element.AssemblyComponents,
});
return true;
}
2023-04-05 18:31:49 +04:00
private void CheckModel(AssemblyBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.AssemblyName))
{
throw new ArgumentNullException("Нет названия сборки", nameof(model.AssemblyName));
}
2023-05-17 13:52:06 +04:00
if (model.Price <= 0)
{
throw new ArgumentNullException("Стоимость сборки должна быть больше 0", nameof(model.Price));
}
2023-04-05 18:31:49 +04:00
_logger.LogInformation("Assembly. ComputerName:{AssemblyName}.Price:{ Price}. Id: { Id}", model.AssemblyName, model.Price, model.Id);
var element = _assemblyStorage.GetElement(new AssemblySearchModel
{
AssemblyName = model.AssemblyName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Сборка с таким названием уже есть");
}
}
}
}