lab2 hard ready

This commit is contained in:
2025-03-11 21:44:01 +04:00
parent bdb510f098
commit fa6b1b64a2
2 changed files with 410 additions and 69 deletions

View File

@@ -1,56 +1,211 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using CandyHouseBase.DataModels;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
namespace CandyHouseBase.Implementations;
internal class StorageBusinessLogicContract
namespace CandyHouseBase.Implementations
{
private readonly IStorageStorageContact _storageStorageContact;
private readonly IIngredientStorageContact _ingredientStorageContact;
private readonly ILogger _logger;
public StorageBusinessLogicContract(
IStorageStorageContact storageStorageContact,
IIngredientStorageContact ingredientStorageContact,
ILogger logger)
internal class StorageBusinessLogicContract : IStorageBusinessLogicContact
{
_storageStorageContact = storageStorageContact;
_ingredientStorageContact = ingredientStorageContact;
_logger = logger;
}
private readonly IStorageStorageContact _storageStorageContact;
private readonly IIngredientStorageContact _ingredientStorageContact;
private readonly ILogger _logger;
public List<StorageDataModel> GetAllStorages()
{
return new List<StorageDataModel>();
}
public StorageDataModel GetStorageByData(string data)
{
if (data.IsGuid())
public StorageBusinessLogicContract(
IStorageStorageContact storageStorageContact,
IIngredientStorageContact ingredientStorageContact,
ILogger logger)
{
return new StorageDataModel("", "", CandyHouseBase.Enums.StorageType.Cabinet,
new List<IngredientDataModel>());
_storageStorageContact = storageStorageContact;
_ingredientStorageContact = ingredientStorageContact;
_logger = logger;
}
else
public List<StorageDataModel> GetAllStorages()
{
// If it's not a GUID, assume it's a title
return new StorageDataModel("", data, CandyHouseBase.Enums.StorageType.Cabinet,
new List<IngredientDataModel>());
try
{
var storages = _storageStorageContact.GetList();
if (storages == null)
{
_logger.LogError("Storage list is null");
throw new NullListException();
}
_logger.LogInformation($"Retrieved {storages.Count} storage records");
return storages;
}
catch (StorageException)
{
throw;
}
catch (Exception ex) when (!(ex is NullListException))
{
_logger.LogError($"Error retrieving storage list: {ex.Message}");
throw new StorageException(ex);
}
}
}
public void InsertStorage(StorageDataModel storage)
{
}
public StorageDataModel GetStorageByData(string data)
{
if (data.IsEmpty())
{
_logger.LogError("Storage data parameter is null or empty");
throw new ArgumentNullException(nameof(data), "Storage data parameter cannot be null or empty");
}
public void UpdateStorage(StorageDataModel storage)
{
}
try
{
StorageDataModel storage = null;
if (data.IsGuid())
{
storage = _storageStorageContact.GetElementById(data);
}
if (storage == null)
{
storage = _storageStorageContact.GetElementByTitle(data);
}
public void DeleteStorage(string id)
{
if (storage == null)
{
_logger.LogError($"Storage not found for data: {data}");
throw new ElementNotFoundException($"Storage not found for data: {data}");
}
_logger.LogInformation($"Retrieved storage: {JsonSerializer.Serialize(storage)}");
return storage;
}
catch (StorageException)
{
throw;
}
catch (Exception ex) when (!(ex is ElementNotFoundException || ex is ArgumentNullException))
{
_logger.LogError($"Error retrieving storage by data: {ex.Message}");
throw new StorageException(ex);
}
}
public void InsertStorage(StorageDataModel storage)
{
if (storage == null)
{
_logger.LogError("Storage is null");
throw new ArgumentNullException(nameof(storage), "Storage cannot be null");
}
try
{
storage.Validate();
var existingStorage = _storageStorageContact.GetElementById(storage.Id);
if (existingStorage != null)
{
throw new ElementExistsException("ID", storage.Id);
}
_storageStorageContact.AddElement(storage);
_logger.LogInformation($"Storage inserted: {JsonSerializer.Serialize(storage)}");
}
catch (ValidationException ex)
{
_logger.LogError($"Validation error during storage insertion: {ex.Message}");
throw;
}
catch (ElementExistsException ex)
{
_logger.LogError($"Storage already exists: {ex.Message}");
throw;
}
catch (StorageException)
{
throw;
}
catch (Exception ex) when (!(ex is ArgumentNullException || ex is ElementNotFoundException))
{
_logger.LogError($"Error inserting storage: {ex.Message}");
throw new StorageException(ex);
}
}
public void UpdateStorage(StorageDataModel storage)
{
if (storage == null)
{
_logger.LogError("Storage is null");
throw new ArgumentNullException(nameof(storage), "Storage cannot be null");
}
try
{
storage.Validate();
_storageStorageContact.UpdateElement(storage);
_logger.LogInformation($"Storage updated: {JsonSerializer.Serialize(storage)}");
}
catch (ValidationException ex)
{
_logger.LogError($"Validation error during storage update: {ex.Message}");
throw;
}
catch (ElementNotFoundException ex)
{
_logger.LogError($"Storage not found: {ex.Message}");
throw;
}
catch (StorageException)
{
throw;
}
catch (Exception ex) when (!(ex is ArgumentNullException))
{
_logger.LogError($"Error updating storage: {ex.Message}");
throw new StorageException(ex);
}
}
public void DeleteStorage(string id)
{
if (id.IsEmpty())
{
_logger.LogError("Storage ID is null or empty");
throw new ArgumentNullException(nameof(id), "Storage ID cannot be null or empty");
}
if (!id.IsGuid())
{
_logger.LogError($"Invalid Storage ID format: {id}");
throw new ValidationException($"Invalid Storage ID format: {id}");
}
try
{
var existingStorage = _storageStorageContact.GetElementById(id);
if (existingStorage == null)
{
_logger.LogError($"Storage not found with ID: {id}");
throw new ElementNotFoundException($"Storage not found with ID: {id}");
}
_storageStorageContact.DeleteElement(id);
_logger.LogInformation($"Storage deleted: {id}");
}
catch (StorageException)
{
throw;
}
catch (Exception ex) when (!(ex is ValidationException || ex is ElementNotFoundException))
{
_logger.LogError($"Error deleting storage: {ex.Message}");
throw new StorageException(ex);
}
}
}
}

View File

@@ -1,46 +1,232 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using CandyHouseBase.DataModels;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
namespace CandyHouseBase.Implementations;
internal class SupplyBusinessLogicContract
namespace CandyHouseBase.Implementations
{
private readonly ISupplyStorageContact _supplyStorageContact;
private readonly IIngredientStorageContact _ingredientStorageContact;
private readonly ILogger _logger;
public SupplyBusinessLogicContract(
ISupplyStorageContact supplyStorageContact,
IIngredientStorageContact ingredientStorageContact,
ILogger logger)
internal class SupplyBusinessLogicContract : ISupplyBusinessLogicContact
{
_supplyStorageContact = supplyStorageContact;
_ingredientStorageContact = ingredientStorageContact;
_logger = logger;
}
private readonly ISupplyStorageContact _supplyStorageContact;
private readonly IIngredientStorageContact _ingredientStorageContact;
private readonly ILogger _logger;
public List<SupplyDataModel> GetAllSupplies()
{
return new List<SupplyDataModel>();
}
public SupplyBusinessLogicContract(
ISupplyStorageContact supplyStorageContact,
IIngredientStorageContact ingredientStorageContact,
ILogger logger)
{
_supplyStorageContact = supplyStorageContact;
_ingredientStorageContact = ingredientStorageContact;
_logger = logger;
}
public SupplyDataModel GetSupplyByData(string data)
{
return new SupplyDataModel("", new DateTime(), 0, 0, new List<IngredientDataModel>());
}
public List<SupplyDataModel> GetAllSupplies()
{
try
{
var supplies = _supplyStorageContact.GetList();
if (supplies == null)
{
_logger.LogError("Supply list is null");
throw new NullListException();
}
public void InsertSupply(SupplyDataModel supply)
{
}
_logger.LogInformation($"Retrieved {supplies.Count} supply records");
return supplies;
}
catch (StorageException)
{
throw;
}
catch (Exception ex) when (!(ex is NullListException))
{
_logger.LogError($"Error retrieving supply list: {ex.Message}");
throw new StorageException(ex);
}
}
public void UpdateSupply(SupplyDataModel supply)
{
}
public SupplyDataModel GetSupplyByData(string data)
{
if (data.IsEmpty())
{
_logger.LogError("Supply data parameter is null or empty");
throw new ArgumentNullException(nameof(data), "Supply data parameter cannot be null or empty");
}
public void DeleteSupply(string id)
{
if (!data.IsGuid())
{
_logger.LogError($"Invalid Supply ID format: {data}");
throw new ValidationException($"Invalid Supply ID format: {data}");
}
try
{
var supply = _supplyStorageContact.GetElementById(data);
if (supply == null)
{
_logger.LogError($"Supply not found for ID: {data}");
throw new ElementNotFoundException($"Supply not found for ID: {data}");
}
_logger.LogInformation($"Retrieved supply: {JsonSerializer.Serialize(supply)}");
return supply;
}
catch (StorageException)
{
throw;
}
catch (Exception ex) when (!(ex is ElementNotFoundException || ex is ArgumentNullException || ex is ValidationException))
{
_logger.LogError($"Error retrieving supply by data: {ex.Message}");
throw new StorageException(ex);
}
}
public void InsertSupply(SupplyDataModel supply)
{
if (supply == null)
{
_logger.LogError("Supply is null");
throw new ArgumentNullException(nameof(supply), "Supply cannot be null");
}
try
{
supply.Validate();
if (supply.Ingredients != null)
{
foreach (var ingredient in supply.Ingredients)
{
if (ingredient != null && !ingredient.Id.IsEmpty() && ingredient.Id.IsGuid())
{
var existingIngredient = _ingredientStorageContact.GetElementById(ingredient.Id);
if (existingIngredient == null)
{
_logger.LogError($"Ingredient with ID {ingredient.Id} not found");
throw new ElementNotFoundException($"Ingredient with ID {ingredient.Id} not found");
}
}
}
}
_supplyStorageContact.AddElement(supply);
_logger.LogInformation($"Supply inserted: {JsonSerializer.Serialize(supply)}");
}
catch (ValidationException ex)
{
_logger.LogError($"Validation error during supply insertion: {ex.Message}");
throw;
}
catch (ElementExistsException ex)
{
_logger.LogError($"Supply already exists: {ex.Message}");
throw;
}
catch (StorageException)
{
throw;
}
catch (Exception ex) when (!(ex is ArgumentNullException || ex is ElementNotFoundException))
{
_logger.LogError($"Error inserting supply: {ex.Message}");
throw new StorageException(ex);
}
}
public void UpdateSupply(SupplyDataModel supply)
{
if (supply == null)
{
_logger.LogError("Supply is null");
throw new ArgumentNullException(nameof(supply), "Supply cannot be null");
}
try
{
supply.Validate();
if (supply.Ingredients != null)
{
foreach (var ingredient in supply.Ingredients)
{
if (ingredient != null && !ingredient.Id.IsEmpty() && ingredient.Id.IsGuid())
{
var existingIngredient = _ingredientStorageContact.GetElementById(ingredient.Id);
if (existingIngredient == null)
{
_logger.LogError($"Ingredient with ID {ingredient.Id} not found");
throw new ElementNotFoundException($"Ingredient with ID {ingredient.Id} not found");
}
}
}
}
_supplyStorageContact.UpdateElement(supply);
_logger.LogInformation($"Supply updated: {JsonSerializer.Serialize(supply)}");
}
catch (ValidationException ex)
{
_logger.LogError($"Validation error during supply update: {ex.Message}");
throw;
}
catch (ElementNotFoundException ex)
{
_logger.LogError($"Supply not found: {ex.Message}");
throw;
}
catch (StorageException)
{
throw;
}
catch (Exception ex) when (!(ex is ArgumentNullException))
{
_logger.LogError($"Error updating supply: {ex.Message}");
throw new StorageException(ex);
}
}
public void DeleteSupply(string id)
{
if (id.IsEmpty())
{
_logger.LogError("Supply ID is null or empty");
throw new ArgumentNullException(nameof(id), "Supply ID cannot be null or empty");
}
if (!id.IsGuid())
{
_logger.LogError($"Invalid Supply ID format: {id}");
throw new ValidationException($"Invalid Supply ID format: {id}");
}
try
{
var existingSupply = _supplyStorageContact.GetElementById(id);
if (existingSupply == null)
{
_logger.LogError($"Supply not found with ID: {id}");
throw new ElementNotFoundException($"Supply not found with ID: {id}");
}
_supplyStorageContact.DeleteElement(id);
_logger.LogInformation($"Supply deleted: {id}");
}
catch (StorageException)
{
throw;
}
catch (Exception ex) when (!(ex is ValidationException || ex is ElementNotFoundException))
{
_logger.LogError($"Error deleting supply: {ex.Message}");
throw new StorageException(ex);
}
}
}
}