126 lines
4.5 KiB
C#
126 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text.Json;
|
|
using CandyHouseBase.DataModels;
|
|
using CandyHouseBase.Enums;
|
|
using CandyHouseBase.Exceptions;
|
|
using CandyHouseBase.Extensions;
|
|
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
|
|
using CandyHouseBase.Interfaces.StoragesContracts;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace CandyHouseBase.Implementations
|
|
{
|
|
internal class OrderBusinessLogicContract(
|
|
IOrderStorageContact orderStorageContact,
|
|
IPekarStorageContact pekarStorageContact,
|
|
IProductStorageContact productStorageContact,
|
|
ILogger logger)
|
|
: IOrderBusinessLogicContact
|
|
{
|
|
private readonly IOrderStorageContact _orderStorageContact = orderStorageContact;
|
|
private readonly IPekarStorageContact _pekarStorageContact = pekarStorageContact;
|
|
private readonly IProductStorageContact _productStorageContact = productStorageContact;
|
|
private readonly ILogger _logger = logger;
|
|
|
|
public List<OrderDataModel> GetAllOrders()
|
|
{
|
|
_logger.LogInformation("GetAllOrders");
|
|
var orders = _orderStorageContact.GetOrders() ?? throw new NullListException();
|
|
return orders;
|
|
}
|
|
|
|
public OrderDataModel GetOrderByData(string data)
|
|
{
|
|
_logger.LogInformation("GetOrderByData for data: {data}", data);
|
|
if (data == null)
|
|
throw new ArgumentNullException(nameof(data));
|
|
if (string.IsNullOrEmpty(data))
|
|
throw new ArgumentNullException(nameof(data));
|
|
if (!data.IsGuid())
|
|
throw new ValidationException("data must be a GUID");
|
|
|
|
var order = _orderStorageContact.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
|
return order;
|
|
}
|
|
|
|
public void InsertOrder(OrderDataModel order)
|
|
{
|
|
_logger.LogInformation("InsertOrder: {json}", JsonSerializer.Serialize(order));
|
|
if (order == null)
|
|
throw new ArgumentNullException(nameof(order));
|
|
order.Validate();
|
|
|
|
// Check if Pekar exists
|
|
if (_pekarStorageContact.GetElementById(order.PekarId) == null)
|
|
throw new ElementNotFoundException(order.PekarId);
|
|
|
|
// Check if Product exists
|
|
if (_productStorageContact.GetElementById(order.ProductId) == null)
|
|
throw new ElementNotFoundException(order.ProductId);
|
|
|
|
// Check if order with this ID already exists
|
|
var existingOrder = _orderStorageContact.GetElementById(order.Id);
|
|
if (existingOrder != null)
|
|
throw new ElementExistsException("ID", order.Id);
|
|
|
|
try
|
|
{
|
|
_orderStorageContact.AddElement(order);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void UpdateOrder(OrderDataModel order)
|
|
{
|
|
_logger.LogInformation("UpdateOrder: {json}", JsonSerializer.Serialize(order));
|
|
if (order == null)
|
|
throw new ArgumentNullException(nameof(order));
|
|
order.Validate();
|
|
|
|
// Check if Pekar exists
|
|
if (_pekarStorageContact.GetElementById(order.PekarId) == null)
|
|
throw new ElementNotFoundException(order.PekarId);
|
|
|
|
// Check if Product exists
|
|
if (_productStorageContact.GetElementById(order.ProductId) == null)
|
|
throw new ElementNotFoundException(order.ProductId);
|
|
|
|
try
|
|
{
|
|
_orderStorageContact.UpdateElement(order);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void DeleteOrder(string id)
|
|
{
|
|
_logger.LogInformation("DeleteOrder for id: {id}", id);
|
|
if (id == null)
|
|
throw new ArgumentNullException(nameof(id));
|
|
if (string.IsNullOrEmpty(id))
|
|
throw new ArgumentNullException(nameof(id));
|
|
if (!id.IsGuid())
|
|
throw new ValidationException("id must be a GUID");
|
|
|
|
var order = _orderStorageContact.GetElementById(id);
|
|
if (order == null)
|
|
throw new ElementNotFoundException(id);
|
|
|
|
try
|
|
{
|
|
_orderStorageContact.DeleteElement(order);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
}
|
|
} |