using Microsoft.Extensions.Logging; using NorthBridgeContract.BusinessLogicsContracts; using NorthBridgeContract.DataModels; using NorthBridgeContract.Enums; using NorthBridgeContract.Exceptions; using NorthBridgeContract.Extensions; using NorthBridgeContract.StoragesContracts; using System.Text.Json; namespace NorthBridgeBusinessLogic.Implementations { internal class ComponentBusinessLogicContract(IComponentStorageContract componentStorageContract, ILogger logger) : IComponentBusinessLogicContract { private readonly ILogger _logger = logger; private readonly IComponentStorageContract _componentStorageContract = componentStorageContract; public List GetAllComponents(bool onlyActive) { _logger.LogInformation("GetAllComponents, onlyActive: {onlyActive}", onlyActive); return _componentStorageContract.GetList(onlyActive) ?? throw new NullListException(); } public ComponentDataModel GetComponentById(string id) { _logger.LogInformation("GetComponentById: {id}", id); if (id.IsEmpty()) { throw new ArgumentNullException(nameof(id)); } if (!id.IsGuid()) { throw new ValidationException("Id is not a unique identifier"); } return _componentStorageContract.GetElementById(id) ?? throw new ElementNotFoundException(id); } public ComponentDataModel GetComponentByName(string name) { _logger.LogInformation("GetComponentByName: {name}", name); if (name.IsEmpty()) { throw new ArgumentNullException(nameof(name)); } return _componentStorageContract.GetElementByName(name) ?? throw new ElementNotFoundException(name); } public ComponentDataModel GetComponentByData(string data) { _logger.LogInformation("GetComponentByData: {data}", data); if (data.IsEmpty()) { throw new ArgumentNullException(nameof(data)); } if (data.IsGuid()) { return GetComponentById(data); } return GetComponentByName(data); } public void InsertComponent(ComponentDataModel componentDataModel) { _logger.LogInformation("InsertComponent: {json}", JsonSerializer.Serialize(componentDataModel)); ArgumentNullException.ThrowIfNull(componentDataModel); componentDataModel.Validate(); _componentStorageContract.AddElement(componentDataModel); } public void UpdateComponent(ComponentDataModel componentDataModel) { _logger.LogInformation("UpdateComponent: {json}", JsonSerializer.Serialize(componentDataModel)); ArgumentNullException.ThrowIfNull(componentDataModel); componentDataModel.Validate(); var existingComponent = _componentStorageContract.GetElementById(componentDataModel.Id); if (existingComponent == null) { throw new ElementNotFoundException(componentDataModel.Id); } if (existingComponent.Price != componentDataModel.Price) { var historyEntry = new ComponentHistoryDataModel(existingComponent.Id, existingComponent.Price); historyEntry.Validate(); _componentStorageContract.AddComponentHistory(historyEntry); } _componentStorageContract.UpdElement(componentDataModel); } public void DeleteComponent(string id) { _logger.LogInformation("DeleteComponent by id: {id}", id); if (id.IsEmpty()) { throw new ArgumentNullException(nameof(id)); } if (!id.IsGuid()) { throw new ValidationException("Id is not a unique identifier"); } _componentStorageContract.DelElement(id); } public void RestoreComponent(string id) { _logger.LogInformation("RestoreComponent by id: {id}", id); if (id.IsEmpty()) { throw new ArgumentNullException(nameof(id)); } if (!id.IsGuid()) { throw new ValidationException("Id is not a unique identifier"); } _componentStorageContract.ResElement(id); } public List GetComponentHistory(string componentId) { _logger.LogInformation("GetComponentHistory for componentId: {componentId}", componentId); if (componentId.IsEmpty()) { throw new ArgumentNullException(nameof(componentId)); } if (!componentId.IsGuid()) { throw new ValidationException("ComponentId is not a unique identifier"); } return _componentStorageContract.GetComponentHistory(componentId) ?? []; } } }