PIbd-22 Pyzhov E.A. LabWork 5 Hard #13
@@ -5,14 +5,16 @@ using SquirrelContract.Exceptions;
|
|||||||
using SquirrelContract.Extensions;
|
using SquirrelContract.Extensions;
|
||||||
using SquirrelContract.StoragesContracts;
|
using SquirrelContract.StoragesContracts;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace SquirrelBusinessLogic.Implementations;
|
namespace SquirrelBusinessLogic.Implementations;
|
||||||
|
|
||||||
public class SaleBusinessLogicContract(ISaleStorageContract
|
public class SaleBusinessLogicContract(ISaleStorageContract saleStorageContract, IWarehouseStorageContract warehouseStorageContract, ILogger logger) : ISaleBusinessLogicContract
|
||||||
saleStorageContract, ILogger logger) : ISaleBusinessLogicContract
|
|
||||||
{
|
{
|
||||||
private readonly ILogger _logger = logger;
|
private readonly ILogger _logger = logger;
|
||||||
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
|
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
|
||||||
|
private readonly IWarehouseStorageContract _warehouseStorageContract = warehouseStorageContract;
|
||||||
|
private readonly Lock _lockObject = new();
|
||||||
|
|
||||||
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate)
|
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate)
|
||||||
{
|
{
|
||||||
@@ -24,6 +26,46 @@ saleStorageContract, ILogger logger) : ISaleBusinessLogicContract
|
|||||||
return _saleStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
|
return _saleStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double CalculateTotalRevenue(DateTime fromDate, DateTime toDate)
|
||||||
|
{
|
||||||
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
var sales = _saleStorageContract.GetList(fromDate, toDate);
|
||||||
|
|
||||||
|
var salesByDay = sales
|
||||||
|
.Where(x => !x.IsCancel)
|
||||||
|
.GroupBy(x => x.SaleDate.Date)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
var revenue = 0.0;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Parallel.ForEach(salesByDay, group =>
|
||||||
|
{
|
||||||
|
var daySum = group.Sum(x => x.Sum - x.Discount);
|
||||||
|
|
||||||
|
lock (_lockObject)
|
||||||
|
{
|
||||||
|
revenue += daySum;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (AggregateException agEx)
|
||||||
|
{
|
||||||
|
foreach (var ex in agEx.InnerExceptions)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error in the cashier payroll process");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return revenue;
|
||||||
|
}
|
||||||
|
|
||||||
public List<SaleDataModel> GetAllSalesByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate)
|
public List<SaleDataModel> GetAllSalesByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("GetAllSales params: {employeeId}, {fromDate}, {toDate}", employeeId, fromDate, toDate);
|
_logger.LogInformation("GetAllSales params: {employeeId}, {fromDate}, {toDate}", employeeId, fromDate, toDate);
|
||||||
@@ -97,6 +139,10 @@ saleStorageContract, ILogger logger) : ISaleBusinessLogicContract
|
|||||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(saleDataModel));
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(saleDataModel));
|
||||||
ArgumentNullException.ThrowIfNull(saleDataModel);
|
ArgumentNullException.ThrowIfNull(saleDataModel);
|
||||||
saleDataModel.Validate();
|
saleDataModel.Validate();
|
||||||
|
if (!_warehouseStorageContract.CheckCocktails(saleDataModel))
|
||||||
|
{
|
||||||
|
throw new InsufficientStockException("Dont have cocktails in warehouse");
|
||||||
|
}
|
||||||
_saleStorageContract.AddElement(saleDataModel);
|
_saleStorageContract.AddElement(saleDataModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,124 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using SquirrelContract.BusinessLogicContracts;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.Extensions;
|
||||||
|
using SquirrelContract.StoragesContracts;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace SquirrelBusinessLogic.Implementations;
|
||||||
|
|
||||||
|
public class SupplyBusinessLogicContract(ISupplyStorageContract supplyStorageContract, IWarehouseStorageContract warehouseStorageContract, ILogger logger) : ISupplyBusinessLogicContract
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = logger;
|
||||||
|
private readonly ISupplyStorageContract _supplyStorageContract = supplyStorageContract;
|
||||||
|
private readonly IWarehouseStorageContract _warehouseStorageContract = warehouseStorageContract;
|
||||||
|
public List<SupplyDataModel> GetAllSuppliesByPeriod(DateTime fromDate, DateTime toDate)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetAllSupplies params: {fromDate}, {toDate}", fromDate, toDate);
|
||||||
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
return _supplyStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SupplyDataModel> GetAllSuppliesByCocktailByPeriod(string cocktailId, DateTime fromDate, DateTime toDate)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetAllSupplies params: {cocktailId}, {fromDate}, {toDate}", cocktailId, fromDate, toDate);
|
||||||
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
if (cocktailId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(cocktailId));
|
||||||
|
}
|
||||||
|
if (!cocktailId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("The value in the field cocktailId is not a unique identifier.");
|
||||||
|
}
|
||||||
|
return _supplyStorageContract.GetList(fromDate, toDate, cocktailId: cocktailId) ?? throw new NullListException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int CountByPeriod(DateTime fromDate, DateTime toDate)
|
||||||
|
{
|
||||||
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
var supplies = _supplyStorageContract.GetList(fromDate, toDate);
|
||||||
|
int count = 0;
|
||||||
|
var lockObject = new object();
|
||||||
|
Parallel.ForEach(supplies, supply =>
|
||||||
|
{
|
||||||
|
int supplyCount = supply.Cocktails.Sum(c => c.Count);
|
||||||
|
if (supplyCount > 0)
|
||||||
|
{
|
||||||
|
Interlocked.Add(ref count, supplyCount);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SupplyDataModel GetSupplyByData(string data)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Get supply by data: {data}", data);
|
||||||
|
if (data.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(data));
|
||||||
|
}
|
||||||
|
if (!data.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ElementNotFoundException(data);
|
||||||
|
}
|
||||||
|
return _supplyStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ProcessSupply(SupplyDataModel supplyDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Processing supply: {json}", JsonSerializer.Serialize(supplyDataModel));
|
||||||
|
|
||||||
|
ArgumentNullException.ThrowIfNull(supplyDataModel);
|
||||||
|
supplyDataModel.Validate();
|
||||||
|
|
||||||
|
foreach (var supplyCocktail in supplyDataModel.Cocktails)
|
||||||
|
{
|
||||||
|
var warehouses = _warehouseStorageContract.GetList();
|
||||||
|
|
||||||
|
var targetWarehouse = warehouses.FirstOrDefault(w =>
|
||||||
|
w.Cocktails.Any(c => c.CocktailId == supplyCocktail.CocktailId));
|
||||||
|
|
||||||
|
if (targetWarehouse == null)
|
||||||
|
{
|
||||||
|
targetWarehouse = warehouses.FirstOrDefault();
|
||||||
|
if (targetWarehouse == null)
|
||||||
|
{
|
||||||
|
throw new ElementNotFoundException("No warehouse found for supply.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_warehouseStorageContract.UpdWarehouseOnSupply(targetWarehouse.Id, supplyCocktail.CocktailId, supplyCocktail.Count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void InsertSupply(SupplyDataModel supplyDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(supplyDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(supplyDataModel);
|
||||||
|
supplyDataModel.Validate();
|
||||||
|
_supplyStorageContract.AddElement(supplyDataModel);
|
||||||
|
ProcessSupply(supplyDataModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateSupply(SupplyDataModel supplyDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(supplyDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(supplyDataModel);
|
||||||
|
supplyDataModel.Validate();
|
||||||
|
_supplyStorageContract.UpdateElement(supplyDataModel);
|
||||||
|
ProcessSupply(supplyDataModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using SquirrelContract.BusinessLogicContracts;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.Extensions;
|
||||||
|
using SquirrelContract.StoragesContracts;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace SquirrelBusinessLogic.Implementations;
|
||||||
|
|
||||||
|
public class WarehouseBusinessLogicContract(IWarehouseStorageContract warehouseStorageContract, ISupplyStorageContract supplyStorageContract, ISaleStorageContract saleStorageContract, ILogger logger) : IWarehouseBusinessLogicContract
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = logger;
|
||||||
|
private readonly IWarehouseStorageContract _warehouseStorageContract = warehouseStorageContract;
|
||||||
|
private readonly ISupplyStorageContract _supplyStorageContract = supplyStorageContract;
|
||||||
|
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
|
||||||
|
|
||||||
|
public List<WarehouseDataModel> GetAllWarehouses()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetAllWarehouses");
|
||||||
|
return _warehouseStorageContract.GetList() ?? throw new NullListException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<int> GetCocktailStockCountAsync()
|
||||||
|
{
|
||||||
|
var incomingTask = Task.Run(() =>
|
||||||
|
{
|
||||||
|
var supplies = _supplyStorageContract.GetList();
|
||||||
|
return supplies.SelectMany(x => x.Cocktails).Sum(x => x.Count);
|
||||||
|
});
|
||||||
|
|
||||||
|
var outcomingTask = Task.Run(() =>
|
||||||
|
{
|
||||||
|
var sales = _saleStorageContract.GetList();
|
||||||
|
return sales.SelectMany(x => x.Cocktails).Sum(x => x.Count);
|
||||||
|
});
|
||||||
|
|
||||||
|
await Task.WhenAll(incomingTask, outcomingTask);
|
||||||
|
|
||||||
|
return incomingTask.Result - outcomingTask.Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public WarehouseDataModel GetWarehouseByData(string data)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Get element by data: {data}", data);
|
||||||
|
if (data.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(data));
|
||||||
|
}
|
||||||
|
if (data.IsGuid())
|
||||||
|
{
|
||||||
|
return _warehouseStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||||
|
}
|
||||||
|
return _warehouseStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void InsertWarehouse(WarehouseDataModel warehouseDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(warehouseDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(warehouseDataModel);
|
||||||
|
warehouseDataModel.Validate();
|
||||||
|
_warehouseStorageContract.AddElement(warehouseDataModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateWarehouse(WarehouseDataModel warehouseDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(warehouseDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(warehouseDataModel);
|
||||||
|
warehouseDataModel.Validate();
|
||||||
|
_warehouseStorageContract.UpdElement(warehouseDataModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteWarehouse(string id)
|
||||||
|
{
|
||||||
|
logger.LogInformation("Delete by id: {id}", id);
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("Id is not a unique identifier");
|
||||||
|
}
|
||||||
|
_warehouseStorageContract.DelElement(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using SquirrelContract.BindingModels;
|
||||||
|
using SquirrelContract.AdapterContracts.OperationResponses;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
|
||||||
|
namespace SquirrelContract.AdapterContracts;
|
||||||
|
|
||||||
|
public interface ISupplyAdapter
|
||||||
|
{
|
||||||
|
SupplyOperationResponse GetAllSuppliesByPeriod(DateTime fromDate, DateTime toDate);
|
||||||
|
|
||||||
|
SupplyOperationResponse GetCocktailList(string id, DateTime fromDate, DateTime toDate);
|
||||||
|
SupplyOperationResponse GetSupplyByData(string data);
|
||||||
|
SupplyOperationResponse InsertSupply(SupplyBindingModel supplyDataModel);
|
||||||
|
SupplyOperationResponse UpdateSupply(SupplyBindingModel supplyDataModel);
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using SquirrelContract.AdapterContracts.OperationResponses;
|
||||||
|
using SquirrelContract.BindingModels;
|
||||||
|
|
||||||
|
namespace SquirrelContract.AdapterContracts;
|
||||||
|
|
||||||
|
public interface IWarehouseAdapter
|
||||||
|
{
|
||||||
|
WarehouseOperationResponse GetAllWarehouses();
|
||||||
|
WarehouseOperationResponse GetWarehouseByData(string data);
|
||||||
|
WarehouseOperationResponse InsertWarehouse(WarehouseBindingModel warehouseDataModel);
|
||||||
|
WarehouseOperationResponse UpdateWarehouse(WarehouseBindingModel warehouseDataModel);
|
||||||
|
WarehouseOperationResponse DeleteWarehouse(string id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using SquirrelContract.Infrastructure;
|
||||||
|
using SquirrelContract.ViewModels;
|
||||||
|
|
||||||
|
namespace SquirrelContract.AdapterContracts.OperationResponses;
|
||||||
|
|
||||||
|
public class SupplyOperationResponse : OperationResponse
|
||||||
|
{
|
||||||
|
public static SupplyOperationResponse OK(List<SupplyViewModel> data) => OK<SupplyOperationResponse, List<SupplyViewModel>>(data);
|
||||||
|
|
||||||
|
public static SupplyOperationResponse OK(SupplyViewModel data) => OK<SupplyOperationResponse, SupplyViewModel>(data);
|
||||||
|
|
||||||
|
public static SupplyOperationResponse NoContent() => NoContent<SupplyOperationResponse>();
|
||||||
|
|
||||||
|
public static SupplyOperationResponse NotFound(string message) => NotFound<SupplyOperationResponse>(message);
|
||||||
|
|
||||||
|
public static SupplyOperationResponse BadRequest(string message) => BadRequest<SupplyOperationResponse>(message);
|
||||||
|
|
||||||
|
public static SupplyOperationResponse InternalServerError(string message) => InternalServerError<SupplyOperationResponse>(message);
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using SquirrelContract.Infrastructure;
|
||||||
|
using SquirrelContract.ViewModels;
|
||||||
|
|
||||||
|
namespace SquirrelContract.AdapterContracts.OperationResponses;
|
||||||
|
|
||||||
|
public class WarehouseOperationResponse : OperationResponse
|
||||||
|
{
|
||||||
|
public static WarehouseOperationResponse OK(List<WarehouseViewModel> data) => OK<WarehouseOperationResponse, List<WarehouseViewModel>>(data);
|
||||||
|
|
||||||
|
public static WarehouseOperationResponse OK(WarehouseViewModel data) => OK<WarehouseOperationResponse, WarehouseViewModel>(data);
|
||||||
|
|
||||||
|
public static WarehouseOperationResponse NoContent() => NoContent<WarehouseOperationResponse>();
|
||||||
|
|
||||||
|
public static WarehouseOperationResponse NotFound(string message) => NotFound<WarehouseOperationResponse>(message);
|
||||||
|
|
||||||
|
public static WarehouseOperationResponse BadRequest(string message) => BadRequest<WarehouseOperationResponse>(message);
|
||||||
|
|
||||||
|
public static WarehouseOperationResponse InternalServerError(string message) => InternalServerError<WarehouseOperationResponse>(message);
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace SquirrelContract.BindingModels;
|
||||||
|
|
||||||
|
public class SupplyBindingModel
|
||||||
|
{
|
||||||
|
public string? Id { get; set; }
|
||||||
|
public DateTime SupplyDate { get; set; }
|
||||||
|
public int Count { get; set; }
|
||||||
|
public List<SupplyCocktailBindingModel>? Cocktails { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace SquirrelContract.BindingModels;
|
||||||
|
|
||||||
|
public class SupplyCocktailBindingModel
|
||||||
|
{
|
||||||
|
public string? SupplyId { get; set; }
|
||||||
|
public string? CocktailId { get; set; }
|
||||||
|
public int Count { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace SquirrelContract.BindingModels;
|
||||||
|
|
||||||
|
public class WarehouseBindingModel
|
||||||
|
{
|
||||||
|
public string? Id { get; set; }
|
||||||
|
public string? Name { get; set; }
|
||||||
|
public int Count { get; set; }
|
||||||
|
public List<WarehouseCocktailBindingModel>? Cocktails { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace SquirrelContract.BindingModels;
|
||||||
|
|
||||||
|
public class WarehouseCocktailBindingModel
|
||||||
|
{
|
||||||
|
public string? WarehouseId { get; set; }
|
||||||
|
public string? CocktailId { get; set; }
|
||||||
|
public int Count { get; set; }
|
||||||
|
}
|
||||||
@@ -6,6 +6,8 @@ public interface ISaleBusinessLogicContract
|
|||||||
{
|
{
|
||||||
List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate);
|
List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate);
|
||||||
|
|
||||||
|
double CalculateTotalRevenue(DateTime fromDate, DateTime toDate);
|
||||||
|
|
||||||
List<SaleDataModel> GetAllSalesByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate);
|
List<SaleDataModel> GetAllSalesByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate);
|
||||||
|
|
||||||
List<SaleDataModel> GetAllSalesByClientByPeriod(string clientId, DateTime fromDate, DateTime toDate);
|
List<SaleDataModel> GetAllSalesByClientByPeriod(string clientId, DateTime fromDate, DateTime toDate);
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using SquirrelContract.DataModels;
|
||||||
|
|
||||||
|
namespace SquirrelContract.BusinessLogicContracts;
|
||||||
|
|
||||||
|
public interface ISupplyBusinessLogicContract
|
||||||
|
{
|
||||||
|
List<SupplyDataModel> GetAllSuppliesByPeriod(DateTime fromDate, DateTime toDate);
|
||||||
|
|
||||||
|
List<SupplyDataModel> GetAllSuppliesByCocktailByPeriod(string cocktailId, DateTime fromDate, DateTime toDate);
|
||||||
|
|
||||||
|
int CountByPeriod(DateTime fromDate, DateTime toDate);
|
||||||
|
|
||||||
|
SupplyDataModel GetSupplyByData(string data);
|
||||||
|
|
||||||
|
void InsertSupply(SupplyDataModel supplyDataModel);
|
||||||
|
|
||||||
|
void UpdateSupply(SupplyDataModel supplyDataModel);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
using SquirrelContract.DataModels;
|
||||||
|
|
||||||
|
namespace SquirrelContract.BusinessLogicContracts;
|
||||||
|
|
||||||
|
public interface IWarehouseBusinessLogicContract
|
||||||
|
{
|
||||||
|
List<WarehouseDataModel> GetAllWarehouses();
|
||||||
|
|
||||||
|
Task<int> GetCocktailStockCountAsync();
|
||||||
|
|
||||||
|
WarehouseDataModel GetWarehouseByData(string data);
|
||||||
|
|
||||||
|
void InsertWarehouse(WarehouseDataModel cocktailDataModel);
|
||||||
|
|
||||||
|
void UpdateWarehouse(WarehouseDataModel cocktailDataModel);
|
||||||
|
|
||||||
|
void DeleteWarehouse(string id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.Extensions;
|
||||||
|
using SquirrelContract.Infastructure;
|
||||||
|
using System.ComponentModel;
|
||||||
|
|
||||||
|
namespace SquirrelContract.DataModels;
|
||||||
|
|
||||||
|
public class SupplyCocktailDataModel(string? supplyId, string? cocktailId, int count) : IValidation
|
||||||
|
{
|
||||||
|
public string SupplyId { get; private set; } = supplyId;
|
||||||
|
|
||||||
|
public string CocktailId { get; private set; } = cocktailId;
|
||||||
|
|
||||||
|
public int Count { get; private set; } = count;
|
||||||
|
|
||||||
|
public void Validate()
|
||||||
|
{
|
||||||
|
if (SupplyId.IsEmpty())
|
||||||
|
throw new ValidationException("Field SupplyId is empty");
|
||||||
|
|
||||||
|
if (!SupplyId.IsGuid())
|
||||||
|
throw new ValidationException("The value in the field SupplyId is not a unique identifier");
|
||||||
|
|
||||||
|
if (CocktailId.IsEmpty())
|
||||||
|
throw new ValidationException("Field CocktailId is empty");
|
||||||
|
|
||||||
|
if (!CocktailId.IsGuid())
|
||||||
|
throw new ValidationException("The value in the field CocktailId is not a unique identifier");
|
||||||
|
|
||||||
|
if (Count <= 0)
|
||||||
|
throw new ValidationException("Field Count is less than or equal to 0");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.Extensions;
|
||||||
|
using SquirrelContract.Infastructure;
|
||||||
|
|
||||||
|
namespace SquirrelContract.DataModels;
|
||||||
|
|
||||||
|
public class SupplyDataModel(string id, DateTime supplyDate, List<SupplyCocktailDataModel> cocktails) : IValidation
|
||||||
|
{
|
||||||
|
public string Id { get; private set; } = id;
|
||||||
|
|
||||||
|
public DateTime SupplyDate { get; private set; } = supplyDate;
|
||||||
|
|
||||||
|
public List<SupplyCocktailDataModel> Cocktails { get; private set; } = cocktails;
|
||||||
|
|
||||||
|
public void Validate()
|
||||||
|
{
|
||||||
|
if (Id.IsEmpty())
|
||||||
|
throw new ValidationException("Field Id is empty");
|
||||||
|
|
||||||
|
if (!Id.IsGuid())
|
||||||
|
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||||
|
|
||||||
|
if (SupplyDate.Date > DateTime.Now)
|
||||||
|
throw new ValidationException($"It is impossible to supply things in the future");
|
||||||
|
|
||||||
|
if ((Cocktails?.Count ?? 0) == 0)
|
||||||
|
throw new ValidationException("The components must include products");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.Extensions;
|
||||||
|
using SquirrelContract.Infastructure;
|
||||||
|
|
||||||
|
namespace SquirrelContract.DataModels;
|
||||||
|
|
||||||
|
public class WarehouseCocktailDataModel(string? warehouseId, string? cocktailId, int count) : IValidation
|
||||||
|
{
|
||||||
|
public string WarehouseId { get; private set; } = warehouseId;
|
||||||
|
|
||||||
|
public string CocktailId { get; private set; } = cocktailId;
|
||||||
|
|
||||||
|
public int Count { get; private set; } = count;
|
||||||
|
|
||||||
|
public void Validate()
|
||||||
|
{
|
||||||
|
if (WarehouseId.IsEmpty())
|
||||||
|
throw new ValidationException("Field WarehouseId is empty");
|
||||||
|
|
||||||
|
if (!WarehouseId.IsGuid())
|
||||||
|
throw new ValidationException("The value in the field WarehouseId is not a unique identifier");
|
||||||
|
|
||||||
|
if (CocktailId.IsEmpty())
|
||||||
|
throw new ValidationException("Field CocktailId is empty");
|
||||||
|
|
||||||
|
if (!CocktailId.IsGuid())
|
||||||
|
throw new ValidationException("The value in the field CocktailId is not a unique identifier");
|
||||||
|
|
||||||
|
if (Count <= 0)
|
||||||
|
throw new ValidationException("Field Count is less than or equal to 0");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.Extensions;
|
||||||
|
using SquirrelContract.Infastructure;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
|
namespace SquirrelContract.DataModels;
|
||||||
|
|
||||||
|
public class WarehouseDataModel(string? id, string name, List<WarehouseCocktailDataModel> cocktails) : IValidation
|
||||||
|
{
|
||||||
|
public string Id { get; private set; } = id;
|
||||||
|
|
||||||
|
public string Name { get; private set; } = name;
|
||||||
|
|
||||||
|
public List<WarehouseCocktailDataModel> Cocktails { get; private set; } = cocktails;
|
||||||
|
|
||||||
|
public void Validate()
|
||||||
|
{
|
||||||
|
if (Id.IsEmpty())
|
||||||
|
throw new ValidationException("Field Id is empty");
|
||||||
|
|
||||||
|
if (!Id.IsGuid())
|
||||||
|
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||||
|
|
||||||
|
if (Name.IsEmpty())
|
||||||
|
throw new ValidationException("Field Name is empty");
|
||||||
|
|
||||||
|
if ((Cocktails?.Count ?? 0) == 0)
|
||||||
|
throw new ValidationException("The cocktails must include drinks");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace SquirrelContract.Exceptions;
|
||||||
|
|
||||||
|
public class InsufficientStockException(string message) : Exception(message)
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -6,9 +6,9 @@ namespace SquirrelContract.Infrastructure;
|
|||||||
|
|
||||||
public class OperationResponse
|
public class OperationResponse
|
||||||
{
|
{
|
||||||
protected HttpStatusCode StatusCode { get; set; }
|
public HttpStatusCode StatusCode { get; set; }
|
||||||
|
|
||||||
protected object? Result { get; set; }
|
public object? Result { get; set; }
|
||||||
|
|
||||||
public IActionResult GetResponse(HttpRequest request, HttpResponse response)
|
public IActionResult GetResponse(HttpRequest request, HttpResponse response)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using SquirrelContract.DataModels;
|
||||||
|
|
||||||
|
namespace SquirrelContract.StoragesContracts;
|
||||||
|
|
||||||
|
public interface ISupplyStorageContract
|
||||||
|
{
|
||||||
|
List<SupplyDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? cocktailId = null);
|
||||||
|
SupplyDataModel? GetElementById(string id);
|
||||||
|
void AddElement(SupplyDataModel warehouseDataModel);
|
||||||
|
void UpdateElement(SupplyDataModel warehouseDataModel);
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using SquirrelContract.DataModels;
|
||||||
|
|
||||||
|
namespace SquirrelContract.StoragesContracts;
|
||||||
|
|
||||||
|
public interface IWarehouseStorageContract
|
||||||
|
{
|
||||||
|
List<WarehouseDataModel> GetList();
|
||||||
|
WarehouseDataModel? GetElementByName(string name);
|
||||||
|
WarehouseDataModel? GetElementById(string id);
|
||||||
|
void AddElement(WarehouseDataModel warehouseDataModel);
|
||||||
|
void UpdElement(WarehouseDataModel warehouseDataModel);
|
||||||
|
void UpdWarehouseOnSupply(string warehouseId, string cocktailId, int count);
|
||||||
|
void DelElement(string id);
|
||||||
|
bool CheckCocktails(SaleDataModel saleDataModel);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace SquirrelContract.ViewModels;
|
||||||
|
|
||||||
|
public class SupplyCocktailViewModel
|
||||||
|
{
|
||||||
|
public required string SupplyId { get; set; }
|
||||||
|
public required string CocktailId { get; set; }
|
||||||
|
public int Count { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace SquirrelContract.ViewModels;
|
||||||
|
|
||||||
|
public class SupplyViewModel
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
public DateTime SupplyDate { get; set; }
|
||||||
|
public int Count { get; set; }
|
||||||
|
public required List<SupplyCocktailViewModel> Cocktails { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace SquirrelContract.ViewModels;
|
||||||
|
|
||||||
|
public class WarehouseCocktailViewModel
|
||||||
|
{
|
||||||
|
public required string WarehouseId { get; set; }
|
||||||
|
public required string CocktailId { get; set; }
|
||||||
|
public int Count { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace SquirrelContract.ViewModels;
|
||||||
|
|
||||||
|
public class WarehouseViewModel
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
public required string Name { get; set; }
|
||||||
|
public required int Count { get; set; }
|
||||||
|
public required List<WarehouseCocktailViewModel> Cocktails { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.StoragesContracts;
|
||||||
|
using SquirrelDatabase.Models;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace SquirrelDatabase.Implementations;
|
||||||
|
|
||||||
|
public class SupplyStorageContract : ISupplyStorageContract
|
||||||
|
{
|
||||||
|
private readonly SquirrelDbContext _dbContext;
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
|
public SupplyStorageContract(SquirrelDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<SupplyCocktail, SupplyCocktailDataModel>();
|
||||||
|
cfg.CreateMap<SupplyCocktailDataModel, SupplyCocktail>();
|
||||||
|
cfg.CreateMap<Supply, SupplyDataModel>();
|
||||||
|
cfg.CreateMap<SupplyDataModel, Supply>()
|
||||||
|
.ForMember(x => x.Cocktails, x => x.MapFrom(src => src.Cocktails));
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SupplyDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? cocktailId = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Supplies.Include(x => x.Cocktails).AsQueryable();
|
||||||
|
if (startDate.HasValue)
|
||||||
|
query = query.Where(x => x.SupplyDate >= DateTime.SpecifyKind(startDate ?? DateTime.UtcNow, DateTimeKind.Utc));
|
||||||
|
if (endDate.HasValue)
|
||||||
|
query = query.Where(x => x.SupplyDate <= DateTime.SpecifyKind(endDate ?? DateTime.UtcNow, DateTimeKind.Utc));
|
||||||
|
if (cocktailId is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.Cocktails!.Any(y => y.CocktailId == cocktailId));
|
||||||
|
}
|
||||||
|
return [.. query.Select(x => _mapper.Map<SupplyDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public SupplyDataModel GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<SupplyDataModel>(GetSuppliesById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(SupplyDataModel suppliesDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Supplies.Add(_mapper.Map<Supply>(suppliesDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateElement(SupplyDataModel suppliesDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetSuppliesById(suppliesDataModel.Id) ?? throw new ElementNotFoundException(suppliesDataModel.Id);
|
||||||
|
_dbContext.Supplies.Update(_mapper.Map(suppliesDataModel, element));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private Supply? GetSuppliesById(string id) => _dbContext.Supplies.FirstOrDefault(x => x.Id == id);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,211 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.StoragesContracts;
|
||||||
|
using SquirrelDatabase.Models;
|
||||||
|
|
||||||
|
namespace SquirrelDatabase.Implementations;
|
||||||
|
|
||||||
|
public class WarehouseStorageContract : IWarehouseStorageContract
|
||||||
|
{
|
||||||
|
private readonly SquirrelDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public WarehouseStorageContract(SquirrelDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<WarehouseCocktail, WarehouseCocktailDataModel>();
|
||||||
|
cfg.CreateMap<WarehouseCocktailDataModel, WarehouseCocktail>();
|
||||||
|
cfg.CreateMap<Warehouse, WarehouseDataModel>();
|
||||||
|
cfg.CreateMap<WarehouseDataModel, Warehouse>()
|
||||||
|
.ForMember(x => x.Cocktails, x => x.MapFrom(src => src.Cocktails));
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
|
||||||
|
}
|
||||||
|
public List<WarehouseDataModel> GetList()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return [.. _dbContext.Warehouses.Select(x => _mapper.Map<WarehouseDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public WarehouseDataModel GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<WarehouseDataModel>(GetWarehouseById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public WarehouseDataModel GetElementByName(string name)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<WarehouseDataModel>(_dbContext.Warehouses.FirstOrDefault(x => x.Name == name));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(WarehouseDataModel warehouseDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Warehouses.Add(_mapper.Map<Warehouse>(warehouseDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdElement(WarehouseDataModel warehouseDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetWarehouseById(warehouseDataModel.Id) ?? throw new ElementNotFoundException(warehouseDataModel.Id);
|
||||||
|
_dbContext.Warehouses.Update(_mapper.Map(warehouseDataModel, element));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DelElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetWarehouseById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
_dbContext.Warehouses.Remove(element);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void UpdWarehouseOnSupply(string warehouseId, string cocktailId, int count)
|
||||||
|
{
|
||||||
|
using var transaction = _dbContext.Database.BeginTransaction();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var warehouse = _dbContext.Warehouses
|
||||||
|
.Include(w => w.Cocktails)
|
||||||
|
.FirstOrDefault(w => w.Id == warehouseId);
|
||||||
|
if (warehouse == null)
|
||||||
|
{
|
||||||
|
throw new ElementNotFoundException($"Warehouse with id {warehouseId} not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var warehouseCocktail = warehouse.Cocktails.FirstOrDefault(c => c.CocktailId == cocktailId);
|
||||||
|
|
||||||
|
if (warehouseCocktail != null)
|
||||||
|
{
|
||||||
|
warehouseCocktail.Count += count;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
warehouse.Cocktails.Add(new WarehouseCocktail
|
||||||
|
{
|
||||||
|
WarehouseId = warehouseId,
|
||||||
|
CocktailId = cocktailId,
|
||||||
|
Count = count
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public bool CheckCocktails(SaleDataModel saleDataModel)
|
||||||
|
{
|
||||||
|
using var transaction = _dbContext.Database.BeginTransaction();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
foreach (var saleCocktail in saleDataModel.Cocktails)
|
||||||
|
{
|
||||||
|
int requiredCount = saleCocktail.Count;
|
||||||
|
|
||||||
|
var warehousesWithCocktail = _dbContext.Warehouses.Include(w => w.Cocktails)
|
||||||
|
.Where(w => w.Cocktails.Any(c => c.CocktailId == saleCocktail.CocktailId && c.Count > 0))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
foreach (var warehouse in warehousesWithCocktail)
|
||||||
|
{
|
||||||
|
var warehouseCocktail = warehouse.Cocktails
|
||||||
|
.FirstOrDefault(c => c.CocktailId == saleCocktail.CocktailId);
|
||||||
|
|
||||||
|
if (warehouseCocktail == null || warehouseCocktail.Count == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int toWithdraw = Math.Min(warehouseCocktail.Count, requiredCount);
|
||||||
|
warehouseCocktail.Count -= toWithdraw;
|
||||||
|
requiredCount -= toWithdraw;
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
if (requiredCount == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requiredCount > 0)
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
transaction.Commit();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Warehouse? GetWarehouseById(string id) => _dbContext.Warehouses.FirstOrDefault(x => x.Id == id);
|
||||||
|
}
|
||||||
14
SquirrelContract/SquirrelDatabase/Models/Supply.cs
Normal file
14
SquirrelContract/SquirrelDatabase/Models/Supply.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
|
namespace SquirrelDatabase.Models;
|
||||||
|
|
||||||
|
public class Supply
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
public DateTime SupplyDate { get; set; }
|
||||||
|
|
||||||
|
[ForeignKey("SupplyId")]
|
||||||
|
public List<SupplyCocktail>? Cocktails { get; set; }
|
||||||
|
}
|
||||||
13
SquirrelContract/SquirrelDatabase/Models/SupplyCocktail.cs
Normal file
13
SquirrelContract/SquirrelDatabase/Models/SupplyCocktail.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
|
||||||
|
namespace SquirrelDatabase.Models;
|
||||||
|
|
||||||
|
public class SupplyCocktail
|
||||||
|
{
|
||||||
|
public required string SupplyId { get; set; }
|
||||||
|
public required string CocktailId { get; set; }
|
||||||
|
public int Count { get; set; }
|
||||||
|
public Supply? Supply { get; set; }
|
||||||
|
public Cocktail? Cocktails { get; set; }
|
||||||
|
}
|
||||||
14
SquirrelContract/SquirrelDatabase/Models/Warehouse.cs
Normal file
14
SquirrelContract/SquirrelDatabase/Models/Warehouse.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
|
namespace SquirrelDatabase.Models;
|
||||||
|
|
||||||
|
public class Warehouse
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
public required string Name { get; set; }
|
||||||
|
|
||||||
|
[ForeignKey("WarehouseId")]
|
||||||
|
public List<WarehouseCocktail> Cocktails { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace SquirrelDatabase.Models;
|
||||||
|
|
||||||
|
public class WarehouseCocktail
|
||||||
|
{
|
||||||
|
public required string WarehouseId { get; set; }
|
||||||
|
public required string CocktailId { get; set; }
|
||||||
|
public int Count { get; set; }
|
||||||
|
public Warehouse? Warehouse { get; set; }
|
||||||
|
public Cocktail? Cocktails { get; set; }
|
||||||
|
}
|
||||||
@@ -48,6 +48,12 @@ public class SquirrelDbContext: DbContext
|
|||||||
.IsUnique()
|
.IsUnique()
|
||||||
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
||||||
|
|
||||||
|
modelBuilder.Entity<Warehouse>().HasIndex(x => x.Name).IsUnique();
|
||||||
|
|
||||||
|
modelBuilder.Entity<SupplyCocktail>().HasKey(x => new { x.SupplyId, x.CocktailId });
|
||||||
|
|
||||||
|
modelBuilder.Entity<WarehouseCocktail>().HasKey(x => new { x.WarehouseId, x.CocktailId });
|
||||||
|
|
||||||
modelBuilder.Entity<SaleCocktail>().HasKey(x => new { x.SaleId, x.CocktailId });
|
modelBuilder.Entity<SaleCocktail>().HasKey(x => new { x.SaleId, x.CocktailId });
|
||||||
|
|
||||||
modelBuilder.Entity<Post>()
|
modelBuilder.Entity<Post>()
|
||||||
@@ -74,6 +80,14 @@ public class SquirrelDbContext: DbContext
|
|||||||
|
|
||||||
public DbSet<Employee> Employees { get; set; }
|
public DbSet<Employee> Employees { get; set; }
|
||||||
|
|
||||||
|
public DbSet<Supply> Supplies { get; set; }
|
||||||
|
|
||||||
|
public DbSet<Warehouse> Warehouses { get; set; }
|
||||||
|
|
||||||
|
public DbSet<SupplyCocktail> SupplyCocktails { get; set; }
|
||||||
|
|
||||||
|
public DbSet<WarehouseCocktail> WarehouseCocktails { get; set; }
|
||||||
|
|
||||||
private static string SerializePostConfiguration(PostConfiguration postConfiguration) => JsonConvert.SerializeObject(postConfiguration);
|
private static string SerializePostConfiguration(PostConfiguration postConfiguration) => JsonConvert.SerializeObject(postConfiguration);
|
||||||
private static PostConfiguration DeserialzePostConfiguration(string jsonString) => JToken.Parse(jsonString).Value<string>("Type") switch
|
private static PostConfiguration DeserialzePostConfiguration(string jsonString) => JToken.Parse(jsonString).Value<string>("Type") switch
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,18 +13,21 @@ internal class SaleBusinessLogicContractTests
|
|||||||
{
|
{
|
||||||
private SaleBusinessLogicContract _saleBusinessLogicContract;
|
private SaleBusinessLogicContract _saleBusinessLogicContract;
|
||||||
private Mock<ISaleStorageContract> _saleStorageContract;
|
private Mock<ISaleStorageContract> _saleStorageContract;
|
||||||
|
private Mock<IWarehouseStorageContract> _warehouseStorageContract;
|
||||||
|
|
||||||
[OneTimeSetUp]
|
[OneTimeSetUp]
|
||||||
public void OneTimeSetUp()
|
public void OneTimeSetUp()
|
||||||
{
|
{
|
||||||
_saleStorageContract = new Mock<ISaleStorageContract>();
|
_saleStorageContract = new Mock<ISaleStorageContract>();
|
||||||
_saleBusinessLogicContract = new SaleBusinessLogicContract(_saleStorageContract.Object, new Mock<ILogger>().Object);
|
_warehouseStorageContract = new Mock<IWarehouseStorageContract>();
|
||||||
|
_saleBusinessLogicContract = new SaleBusinessLogicContract(_saleStorageContract.Object, _warehouseStorageContract.Object, new Mock<ILogger>().Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TearDown]
|
[TearDown]
|
||||||
public void TearDown()
|
public void TearDown()
|
||||||
{
|
{
|
||||||
_saleStorageContract.Reset();
|
_saleStorageContract.Reset();
|
||||||
|
_warehouseStorageContract.Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@@ -391,6 +394,7 @@ internal class SaleBusinessLogicContractTests
|
|||||||
var flag = false;
|
var flag = false;
|
||||||
var record = new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DiscountType.None,
|
var record = new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DiscountType.None,
|
||||||
false, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)]);
|
false, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)]);
|
||||||
|
_warehouseStorageContract.Setup(x => x.CheckCocktails(It.IsAny<SaleDataModel>())).Returns(true);
|
||||||
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>()))
|
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>()))
|
||||||
.Callback((SaleDataModel x) =>
|
.Callback((SaleDataModel x) =>
|
||||||
{
|
{
|
||||||
@@ -404,6 +408,7 @@ internal class SaleBusinessLogicContractTests
|
|||||||
//Act
|
//Act
|
||||||
_saleBusinessLogicContract.InsertSale(record);
|
_saleBusinessLogicContract.InsertSale(record);
|
||||||
//Assert
|
//Assert
|
||||||
|
_warehouseStorageContract.Verify(x => x.CheckCocktails(It.IsAny<SaleDataModel>()), Times.Once);
|
||||||
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||||
Assert.That(flag);
|
Assert.That(flag);
|
||||||
}
|
}
|
||||||
@@ -412,11 +417,13 @@ internal class SaleBusinessLogicContractTests
|
|||||||
public void InsertSale_RecordWithExistsData_ThrowException_Test()
|
public void InsertSale_RecordWithExistsData_ThrowException_Test()
|
||||||
{
|
{
|
||||||
//Arrange
|
//Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.CheckCocktails(It.IsAny<SaleDataModel>())).Returns(true);
|
||||||
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
//Act&Assert
|
//Act&Assert
|
||||||
Assert.That(() => _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
Assert.That(() => _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||||
Guid.NewGuid().ToString(), DiscountType.None, false, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])), Throws.TypeOf<ElementExistsException>());
|
Guid.NewGuid().ToString(), DiscountType.None, false, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])), Throws.TypeOf<ElementExistsException>());
|
||||||
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||||
|
_warehouseStorageContract.Verify(x => x.CheckCocktails(It.IsAny<SaleDataModel>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@@ -439,13 +446,26 @@ internal class SaleBusinessLogicContractTests
|
|||||||
public void InsertSale_StorageThrowError_ThrowException_Test()
|
public void InsertSale_StorageThrowError_ThrowException_Test()
|
||||||
{
|
{
|
||||||
//Arrange
|
//Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.CheckCocktails(It.IsAny<SaleDataModel>())).Returns(true);
|
||||||
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
//Act&Assert
|
//Act&Assert
|
||||||
Assert.That(() => _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
Assert.That(() => _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||||
Guid.NewGuid().ToString(), DiscountType.None, false, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])), Throws.TypeOf<StorageException>());
|
Guid.NewGuid().ToString(), DiscountType.None, false, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])), Throws.TypeOf<StorageException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.CheckCocktails(It.IsAny<SaleDataModel>()), Times.Once);
|
||||||
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertSale_InsufficientError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.CheckCocktails(It.IsAny<SaleDataModel>())).Returns(false);
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.InsertSale(new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(), DiscountType.None, false, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])), Throws.TypeOf<InsufficientStockException>());
|
||||||
|
//Act&Assert
|
||||||
|
_warehouseStorageContract.Verify(x => x.CheckCocktails(It.IsAny<SaleDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void CancelSale_CorrectRecord_Test()
|
public void CancelSale_CorrectRecord_Test()
|
||||||
{
|
{
|
||||||
@@ -497,4 +517,45 @@ internal class SaleBusinessLogicContractTests
|
|||||||
Assert.That(() => _saleBusinessLogicContract.CancelSale(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
Assert.That(() => _saleBusinessLogicContract.CancelSale(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CalculateTotalRevenue_ReturnsCorrectSum_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
DateTime fromDate = DateTime.UtcNow.AddDays(-10);
|
||||||
|
DateTime toDate = DateTime.UtcNow;
|
||||||
|
|
||||||
|
var sales = new List<SaleDataModel>
|
||||||
|
{
|
||||||
|
new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DiscountType.RegularCustomer, false, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 50.5)]),
|
||||||
|
new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DiscountType.None, true, []),
|
||||||
|
new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DiscountType.None, true, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 50.5)])
|
||||||
|
};
|
||||||
|
_saleStorageContract
|
||||||
|
.Setup(x => x.GetList(fromDate, toDate, null, null, null))
|
||||||
|
.Returns(sales);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double revenue = _saleBusinessLogicContract.CalculateTotalRevenue(fromDate, toDate);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(revenue, Is.EqualTo(126.25));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CalculateTotalRevenue_NoSales_ReturnsZero()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
DateTime fromDate = DateTime.UtcNow.AddDays(-10);
|
||||||
|
DateTime toDate = DateTime.UtcNow;
|
||||||
|
_saleStorageContract
|
||||||
|
.Setup(x => x.GetList(fromDate, toDate, null, null, null))
|
||||||
|
.Returns(new List<SaleDataModel>());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
double revenue = _saleBusinessLogicContract.CalculateTotalRevenue(fromDate, toDate);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(revenue, Is.EqualTo(0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,328 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using SquirrelBusinessLogic.Implementations;
|
||||||
|
using SquirrelContract.BusinessLogicContracts;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Enums;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.StoragesContracts;
|
||||||
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||||
|
|
||||||
|
namespace SquirrelTests.BusinessLogicContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class SupplyBusinessLogicContractTests
|
||||||
|
{
|
||||||
|
private SupplyBusinessLogicContract _supplyBusinessLogicContract;
|
||||||
|
private Mock<ISupplyStorageContract> _supplyStorageContract;
|
||||||
|
private Mock<IWarehouseStorageContract> _warehouseStorageContract;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_supplyStorageContract = new Mock<ISupplyStorageContract>();
|
||||||
|
_warehouseStorageContract = new Mock<IWarehouseStorageContract>();
|
||||||
|
_supplyBusinessLogicContract = new SupplyBusinessLogicContract(_supplyStorageContract.Object, _warehouseStorageContract.Object, new Mock<ILogger>().Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
_supplyStorageContract.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSuppliesByPeriod_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
// Arrange
|
||||||
|
var listOriginal = new List<SupplyDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.Now, []),
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.Now, []),
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.Now, [])
|
||||||
|
};
|
||||||
|
_supplyStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Returns(listOriginal);
|
||||||
|
// Act
|
||||||
|
var list = _supplyBusinessLogicContract.GetAllSuppliesByPeriod(date, date.AddDays(1));
|
||||||
|
// Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
_supplyStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSuppliesByPeriod_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_supplyStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var list = _supplyBusinessLogicContract.GetAllSuppliesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
_supplyStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSuppliesByPeriod_IncorrectDates_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetAllSuppliesByPeriod(date, date), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetAllSuppliesByPeriod(date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSuppliesByPeriod_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetAllSuppliesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSuppliesByPeriod_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_supplyStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetAllSuppliesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSuppliesByCocktailByPeriod_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
var cocktailId = Guid.NewGuid().ToString();
|
||||||
|
var listOriginal = new List<SupplyDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.Now,
|
||||||
|
[new SupplyCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.Now, []),
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.Now, []),
|
||||||
|
};
|
||||||
|
_supplyStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var list = _supplyBusinessLogicContract.GetAllSuppliesByCocktailByPeriod(cocktailId, date, date.AddDays(1));
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
_supplyStorageContract.Verify(x => x.GetList(date, date.AddDays(1), cocktailId), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSuppliesByCocktailByPeriod_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_supplyStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var list = _supplyBusinessLogicContract.GetAllSuppliesByCocktailByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
_supplyStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSuppliesByCocktailByPeriod_IncorrectDates_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetAllSuppliesByCocktailByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetAllSuppliesByCocktailByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSuppliesByCocktailByPeriod_CocktailIdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetAllSuppliesByCocktailByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetAllSuppliesByCocktailByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSuppliesByCocktailByPeriod_CocktailIdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetAllSuppliesByCocktailByPeriod("cocktailId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSuppliesByCocktailByPeriod_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetAllSuppliesByCocktailByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSuppliesByCocktailByPeriod_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_supplyStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetAllSuppliesByCocktailByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void GetSupplyByData_GetById_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var record = new SupplyDataModel(id, DateTime.Now, []);
|
||||||
|
_supplyStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||||
|
// Act
|
||||||
|
var element = _supplyBusinessLogicContract.GetSupplyByData(id);
|
||||||
|
// Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.Id, Is.EqualTo(id));
|
||||||
|
_supplyStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetSupplyByData_EmptyData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetSupplyByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetSupplyByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetSupplyByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_supplyStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetSupplyByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetSuppliesByData_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_supplyStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.GetSupplyByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertSupply_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var supplyId = Guid.NewGuid().ToString();
|
||||||
|
var cocktailId = Guid.NewGuid().ToString();
|
||||||
|
var warehouseId = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
|
var record = new SupplyDataModel(supplyId, DateTime.Now,
|
||||||
|
[new SupplyCocktailDataModel(supplyId, cocktailId, 5)]);
|
||||||
|
|
||||||
|
var warehouse = new WarehouseDataModel(warehouseId, "Main Warehouse",
|
||||||
|
[new WarehouseCocktailDataModel(warehouseId, cocktailId, 10)]);
|
||||||
|
|
||||||
|
_supplyStorageContract.Setup(x => x.AddElement(It.IsAny<SupplyDataModel>()));
|
||||||
|
_warehouseStorageContract.Setup(x => x.GetList()).Returns([warehouse]);
|
||||||
|
_warehouseStorageContract.Setup(x => x.UpdWarehouseOnSupply(warehouseId, cocktailId, 5));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_supplyBusinessLogicContract.InsertSupply(record);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_supplyStorageContract.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Once);
|
||||||
|
_warehouseStorageContract.Verify(x => x.UpdWarehouseOnSupply(warehouseId, cocktailId, 5), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertSupply_RecordWithExistsData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_supplyStorageContract.Setup(x => x.AddElement(It.IsAny<SupplyDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.InsertSupply(new(Guid.NewGuid().ToString(), DateTime.Now,
|
||||||
|
[new SupplyCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertSupply_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.InsertSupply(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertSupply_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.InsertSupply(new SupplyDataModel("id", DateTime.UtcNow, [])), Throws.TypeOf<ValidationException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertSupply_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_supplyStorageContract.Setup(x => x.AddElement(It.IsAny<SupplyDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.InsertSupply(new(Guid.NewGuid().ToString(), DateTime.Now,
|
||||||
|
[new SupplyCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CountByPeriod_ReturnInt_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
string id = Guid.NewGuid().ToString();
|
||||||
|
var listOriginal = new List<SupplyDataModel>()
|
||||||
|
{
|
||||||
|
new(id, DateTime.UtcNow, new List<SupplyCocktailDataModel> { new SupplyCocktailDataModel(id, Guid.NewGuid().ToString(), 5), new SupplyCocktailDataModel(id, Guid.NewGuid().ToString(), 6) })
|
||||||
|
};
|
||||||
|
_supplyStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Returns(listOriginal);
|
||||||
|
// Act
|
||||||
|
var count = _supplyBusinessLogicContract.CountByPeriod(DateTime.UtcNow.AddDays(-5), DateTime.UtcNow.AddDays(5));
|
||||||
|
// Assert
|
||||||
|
Assert.That(count, Is.EqualTo(11));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CountByPeriod_ReturnIntZero_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
string id1 = Guid.NewGuid().ToString();
|
||||||
|
string id2 = Guid.NewGuid().ToString();
|
||||||
|
string id3 = Guid.NewGuid().ToString();
|
||||||
|
var listOriginal = new List<SupplyDataModel>()
|
||||||
|
{
|
||||||
|
new(id1, DateTime.UtcNow, [])
|
||||||
|
};
|
||||||
|
_supplyStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Returns(listOriginal);
|
||||||
|
// Act
|
||||||
|
var count = _supplyBusinessLogicContract.CountByPeriod(DateTime.UtcNow.AddDays(-5), DateTime.UtcNow.AddDays(5));
|
||||||
|
// Assert
|
||||||
|
Assert.That(count, Is.EqualTo(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CountByPeriod_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_supplyStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _supplyBusinessLogicContract.CountByPeriod(DateTime.UtcNow.AddDays(-5), DateTime.UtcNow.AddDays(5)), Throws.TypeOf<StorageException>());
|
||||||
|
_supplyStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,371 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using SquirrelBusinessLogic.Implementations;
|
||||||
|
using SquirrelContract.BusinessLogicContracts;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Enums;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.StoragesContracts;
|
||||||
|
|
||||||
|
namespace SquirrelTests.BusinessLogicContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class WarehouseBusinessLogicContractTests
|
||||||
|
{
|
||||||
|
private WarehouseBusinessLogicContract _warehouseBusinessLogicContract;
|
||||||
|
private Mock<ISaleStorageContract> _saleStorageContract;
|
||||||
|
private Mock<ISupplyStorageContract> _supplyStorageContract;
|
||||||
|
private Mock<IWarehouseStorageContract> _warehouseStorageContract;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_warehouseStorageContract = new Mock<IWarehouseStorageContract>();
|
||||||
|
_saleStorageContract = new Mock<ISaleStorageContract>();
|
||||||
|
_supplyStorageContract = new Mock<ISupplyStorageContract>();
|
||||||
|
_warehouseBusinessLogicContract = new WarehouseBusinessLogicContract(_warehouseStorageContract.Object,
|
||||||
|
_supplyStorageContract.Object, _saleStorageContract.Object, new Mock<ILogger>().Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
_warehouseStorageContract.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWarehouses_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var listOriginal = new List<WarehouseDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), "aba", []),
|
||||||
|
new(Guid.NewGuid().ToString(), "aaa", []),
|
||||||
|
new(Guid.NewGuid().ToString(), "aaa", []),
|
||||||
|
};
|
||||||
|
_warehouseStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
|
||||||
|
// Act
|
||||||
|
var list = _warehouseBusinessLogicContract.GetAllWarehouses();
|
||||||
|
// Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWarehouses_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.GetList()).Returns([]);
|
||||||
|
// Act
|
||||||
|
var list = _warehouseBusinessLogicContract.GetAllWarehouses();
|
||||||
|
// Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
_warehouseStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWarehouses_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.GetList()).Returns((List<WarehouseDataModel>)null);
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.GetAllWarehouses(), Throws.TypeOf<NullListException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWarehouses_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.GetAllWarehouses(), Throws.TypeOf<StorageException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWarehousesByData_GetById_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var record = new WarehouseDataModel(id, "aba", []);
|
||||||
|
_warehouseStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||||
|
// Act
|
||||||
|
var element = _warehouseBusinessLogicContract.GetWarehouseByData(id);
|
||||||
|
// Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.Id, Is.EqualTo(id));
|
||||||
|
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetCocktailByData_GetByName_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var name = "name";
|
||||||
|
var record = new WarehouseDataModel(Guid.NewGuid().ToString(), name, []);
|
||||||
|
_warehouseStorageContract.Setup(x => x.GetElementByName(name)).Returns(record);
|
||||||
|
//Act
|
||||||
|
var element = _warehouseBusinessLogicContract.GetWarehouseByData(name);
|
||||||
|
//Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.Name, Is.EqualTo(name));
|
||||||
|
_warehouseStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetWarehouseByData_EmptyData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.GetWarehouseByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.GetWarehouseByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||||
|
_warehouseStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetWarehouseByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.GetWarehouseByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetWarehouseByData_GetByName_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.GetWarehouseByData("name"), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetWarehouseByData_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
_warehouseStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.GetWarehouseByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.GetWarehouseByData("name"), Throws.TypeOf<StorageException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
_warehouseStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWarehouse_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var record = new WarehouseDataModel(Guid.NewGuid().ToString(), "aba",
|
||||||
|
[new WarehouseCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||||
|
_warehouseStorageContract.Setup(x => x.AddElement(It.IsAny<WarehouseDataModel>()));
|
||||||
|
// Act
|
||||||
|
_warehouseBusinessLogicContract.InsertWarehouse(record);
|
||||||
|
// Assert
|
||||||
|
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWarehouse_RecordWithExistsData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.AddElement(It.IsAny<WarehouseDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.InsertWarehouse(new(Guid.NewGuid().ToString(), "aba",
|
||||||
|
[new WarehouseCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWarehouse_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.InsertWarehouse(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<WarehouseDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWarehouse_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.InsertWarehouse(new WarehouseDataModel("id", "aba", [])), Throws.TypeOf<ValidationException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<WarehouseDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWarehouse_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.AddElement(It.IsAny<WarehouseDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.InsertWarehouse(new(Guid.NewGuid().ToString(), "aba",
|
||||||
|
[new WarehouseCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWarehouse_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var record = new WarehouseDataModel(Guid.NewGuid().ToString(), "aba",
|
||||||
|
[new WarehouseCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||||
|
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<WarehouseDataModel>()));
|
||||||
|
// Act
|
||||||
|
_warehouseBusinessLogicContract.UpdateWarehouse(record);
|
||||||
|
// Assert
|
||||||
|
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWarehouse_RecordWithIncorrectData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<WarehouseDataModel>())).Throws(new ElementNotFoundException(""));
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.UpdateWarehouse(new(Guid.NewGuid().ToString(), "aba",
|
||||||
|
[new WarehouseCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWarehouse_RecordWithExistsData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<WarehouseDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.UpdateWarehouse(new(Guid.NewGuid().ToString(), "aba",
|
||||||
|
[new WarehouseCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWarehouse_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.UpdateWarehouse(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWarehouse_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.UpdateWarehouse(new WarehouseDataModel(Guid.NewGuid().ToString(), "aba", [])), Throws.TypeOf<ValidationException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWarehouse_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<WarehouseDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.UpdateWarehouse(new(Guid.NewGuid().ToString(), "aba",
|
||||||
|
[new WarehouseCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<WarehouseDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteWarehouse_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var flag = false;
|
||||||
|
_warehouseStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||||
|
//Act
|
||||||
|
_warehouseBusinessLogicContract.DeleteWarehouse(id);
|
||||||
|
//Assert
|
||||||
|
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once); Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteWarehouse_RecordWithIncorrectId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
_warehouseStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.DeleteWarehouse(Guid.NewGuid().ToString()),
|
||||||
|
Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteWarehouse_IdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.DeleteWarehouse(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.DeleteWarehouse(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteWarehouse_IdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.DeleteWarehouse("id"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteWarehouse_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_warehouseStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _warehouseBusinessLogicContract.DeleteWarehouse(Guid.NewGuid().ToString()),
|
||||||
|
Throws.TypeOf<StorageException>());
|
||||||
|
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetCocktailStockCountAsync_CorrectlyCalculatesStock_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var supplies = new List<SupplyDataModel>
|
||||||
|
{
|
||||||
|
new SupplyDataModel(Guid.NewGuid().ToString(), DateTime.UtcNow, new List<SupplyCocktailDataModel>
|
||||||
|
{
|
||||||
|
new SupplyCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10),
|
||||||
|
new SupplyCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 20)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
var sales = new List<SaleDataModel>
|
||||||
|
{
|
||||||
|
new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DiscountType.None, false,
|
||||||
|
new List<SaleCocktailDataModel>
|
||||||
|
{
|
||||||
|
new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 100.0),
|
||||||
|
new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 8, 150.0)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
_supplyStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Returns(supplies);
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(sales);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await _warehouseBusinessLogicContract.GetCocktailStockCountAsync();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.EqualTo(17));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetCocktailStockCountAsync_NoSuppliesOrSales_ReturnsZero_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_supplyStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Returns(new List<SupplyDataModel>());
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(new List<SaleDataModel>());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await _warehouseBusinessLogicContract.GetCocktailStockCountAsync();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.EqualTo(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
|
||||||
|
namespace SquirrelTests.DataModelsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class SupplyCocktailDataModelTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void SupplyIdIsNullOrEmptyTest()
|
||||||
|
{
|
||||||
|
var supplyCocktail = CreateDataModel(null, Guid.NewGuid().ToString(), 10);
|
||||||
|
Assert.That(() => supplyCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
supplyCocktail = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
|
||||||
|
Assert.That(() => supplyCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void SupplyIdIsNotGuidTest()
|
||||||
|
{
|
||||||
|
var supplyCocktail = CreateDataModel("SupplyId", Guid.NewGuid().ToString(), 10);
|
||||||
|
Assert.That(() => supplyCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CocktailIdIsNullOrEmptyTest()
|
||||||
|
{
|
||||||
|
var supplyCocktail = CreateDataModel(Guid.NewGuid().ToString(), null, 10);
|
||||||
|
Assert.That(() => supplyCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
supplyCocktail = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 10);
|
||||||
|
Assert.That(() => supplyCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CocktailIdIsNotGuidTest()
|
||||||
|
{
|
||||||
|
var supplyCocktail = CreateDataModel(Guid.NewGuid().ToString(), "ComponentId", 10);
|
||||||
|
Assert.That(() => supplyCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CountIsLessOrZeroTest()
|
||||||
|
{
|
||||||
|
var supplyCocktail = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||||
|
Assert.That(() => supplyCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
supplyCocktail = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
|
||||||
|
Assert.That(() => supplyCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void AllFieldsIsCorrectTest()
|
||||||
|
{
|
||||||
|
var cocktailId = Guid.NewGuid().ToString();
|
||||||
|
var supplyId = Guid.NewGuid().ToString();
|
||||||
|
var count = 5;
|
||||||
|
var supplyCocktail = CreateDataModel(supplyId, cocktailId, count);
|
||||||
|
Assert.That(() => supplyCocktail.Validate(), Throws.Nothing);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(supplyCocktail.SupplyId, Is.EqualTo(supplyId));
|
||||||
|
Assert.That(supplyCocktail.CocktailId, Is.EqualTo(cocktailId));
|
||||||
|
Assert.That(supplyCocktail.Count, Is.EqualTo(count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SupplyCocktailDataModel CreateDataModel(string? supplyId, string? cocktailId, int count) =>
|
||||||
|
new(supplyId, cocktailId, count);
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
|
||||||
|
namespace SquirrelTests.DataModelsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class SupplyDataModelTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void IdIsNullOrEmptyTest()
|
||||||
|
{
|
||||||
|
var supply = CreateDataModel(null, DateTime.Now, CreateSubDataModel());
|
||||||
|
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
supply = CreateDataModel(string.Empty, DateTime.Now, CreateSubDataModel());
|
||||||
|
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void IdIsNotGuidTest()
|
||||||
|
{
|
||||||
|
var supply = CreateDataModel("id", DateTime.Now, CreateSubDataModel());
|
||||||
|
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CocktailsIsNullOrEmptyTest()
|
||||||
|
{
|
||||||
|
var supply = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, null);
|
||||||
|
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
supply = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, []);
|
||||||
|
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DateIsNotCorrectTest()
|
||||||
|
{
|
||||||
|
var supply = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now.AddDays(5), CreateSubDataModel());
|
||||||
|
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void AllFieldsIsCorrectTest()
|
||||||
|
{
|
||||||
|
var supplyId = Guid.NewGuid().ToString();
|
||||||
|
var date = DateTime.Now;
|
||||||
|
var cocktails = CreateSubDataModel();
|
||||||
|
var supply = CreateDataModel(supplyId, date, cocktails);
|
||||||
|
Assert.That(() => supply.Validate(), Throws.Nothing);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(supply.Id, Is.EqualTo(supplyId));
|
||||||
|
Assert.That(supply.SupplyDate, Is.EqualTo(date));
|
||||||
|
Assert.That(supply.Cocktails, Is.EquivalentTo(cocktails));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SupplyDataModel CreateDataModel(string? id, DateTime date, List<SupplyCocktailDataModel> cocktails) =>
|
||||||
|
new(id, date, cocktails);
|
||||||
|
|
||||||
|
private static List<SupplyCocktailDataModel> CreateSubDataModel()
|
||||||
|
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
|
||||||
|
namespace SquirrelTests.DataModelsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class WarehouseCocktailDataModelTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void WarehouseIdIsNullOrEmptyTest()
|
||||||
|
{
|
||||||
|
var warehouseCocktail = CreateDataModel(null, Guid.NewGuid().ToString(), 10);
|
||||||
|
Assert.That(() => warehouseCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
warehouseCocktail = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
|
||||||
|
Assert.That(() => warehouseCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void WarehouseIdIsNotGuidTest()
|
||||||
|
{
|
||||||
|
var warehouseCocktail = CreateDataModel("id", Guid.NewGuid().ToString(), 10);
|
||||||
|
Assert.That(() => warehouseCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CocktailIdIsNullOrEmptyTest()
|
||||||
|
{
|
||||||
|
var warehouseCocktail = CreateDataModel(Guid.NewGuid().ToString(), null, 10);
|
||||||
|
Assert.That(() => warehouseCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
warehouseCocktail = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 10);
|
||||||
|
Assert.That(() => warehouseCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CocktailIdIsNotGuidTest()
|
||||||
|
{
|
||||||
|
var warehouseCocktail = CreateDataModel(Guid.NewGuid().ToString(), "id", 10);
|
||||||
|
Assert.That(() => warehouseCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CountIsLessOrZeroTest()
|
||||||
|
{
|
||||||
|
var warehouseCocktail = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||||
|
Assert.That(() => warehouseCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
warehouseCocktail = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
|
||||||
|
Assert.That(() => warehouseCocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void AllFieldsIsCorrectTest()
|
||||||
|
{
|
||||||
|
var cocktailId = Guid.NewGuid().ToString();
|
||||||
|
var warehouseId = Guid.NewGuid().ToString();
|
||||||
|
var count = 5;
|
||||||
|
var warehouseCocktail = CreateDataModel(warehouseId, cocktailId, count);
|
||||||
|
Assert.That(() => warehouseCocktail.Validate(), Throws.Nothing);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(warehouseCocktail.WarehouseId, Is.EqualTo(warehouseId));
|
||||||
|
Assert.That(warehouseCocktail.CocktailId, Is.EqualTo(cocktailId));
|
||||||
|
Assert.That(warehouseCocktail.Count, Is.EqualTo(count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static WarehouseCocktailDataModel CreateDataModel(string? storageId, string? cocktailId, int count) =>
|
||||||
|
new(storageId, cocktailId, count);
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
|
||||||
|
namespace SquirrelTests.DataModelsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class WarehouseDataModelTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void IdIsNullOrEmptyTest()
|
||||||
|
{
|
||||||
|
var warehouse = CreateDataModel(null, "name", CreateSubDataModel());
|
||||||
|
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
warehouse = CreateDataModel(string.Empty, "name", CreateSubDataModel());
|
||||||
|
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void IdIsNotGuidTest()
|
||||||
|
{
|
||||||
|
var warehouse = CreateDataModel("id", "name", CreateSubDataModel());
|
||||||
|
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void WarehouseNameIsEmptyTest()
|
||||||
|
{
|
||||||
|
var warehouse = CreateDataModel(Guid.NewGuid().ToString(), null, CreateSubDataModel());
|
||||||
|
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
warehouse = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, CreateSubDataModel());
|
||||||
|
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CocktailsIsNullOrEmptyTest()
|
||||||
|
{
|
||||||
|
var warehouse = CreateDataModel(Guid.NewGuid().ToString(), "name", null);
|
||||||
|
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
warehouse = CreateDataModel(Guid.NewGuid().ToString(), "name", []);
|
||||||
|
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void AllFieldsIsCorrectTest()
|
||||||
|
{
|
||||||
|
var warehouseId = Guid.NewGuid().ToString();
|
||||||
|
var warehouseName = "name";
|
||||||
|
var cocktails = CreateSubDataModel();
|
||||||
|
var warehouse = CreateDataModel(warehouseId, warehouseName, cocktails);
|
||||||
|
Assert.That(() => warehouse.Validate(), Throws.Nothing);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(warehouse.Id, Is.EqualTo(warehouseId));
|
||||||
|
Assert.That(warehouse.Name, Is.EqualTo(warehouseName));
|
||||||
|
Assert.That(warehouse.Cocktails, Is.EquivalentTo(cocktails));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static WarehouseDataModel CreateDataModel(string? id, string? warehouseName, List<WarehouseCocktailDataModel> cocktails) =>
|
||||||
|
new(id, warehouseName, cocktails);
|
||||||
|
|
||||||
|
private static List<WarehouseCocktailDataModel> CreateSubDataModel()
|
||||||
|
=> [new(Guid.NewGuid().ToString(), "name", 1)];
|
||||||
|
}
|
||||||
@@ -71,6 +71,36 @@ internal static class SquirrelDbContextExtensions
|
|||||||
return worker;
|
return worker;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Warehouse InsertWarehouseToDatabaseAndReturn(this SquirrelDbContext dbContext, string? id = null, string name = "test", List<(string, int)>? cocktails = null)
|
||||||
|
{
|
||||||
|
var warehouse = new Warehouse() { Id = id ?? Guid.NewGuid().ToString(), Name = name, Cocktails = [] };
|
||||||
|
if (cocktails is not null)
|
||||||
|
{
|
||||||
|
foreach (var elem in cocktails)
|
||||||
|
{
|
||||||
|
warehouse.Cocktails.Add(new WarehouseCocktail { WarehouseId = warehouse.Id, CocktailId = elem.Item1, Count = elem.Item2 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dbContext.Warehouses.Add(warehouse);
|
||||||
|
dbContext.SaveChanges();
|
||||||
|
return warehouse;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Supply InsertSupplyToDatabaseAndReturn(this SquirrelDbContext dbContext, string? id = null, DateTime? supplyDate = null, List<(string, int)>? cocktails = null)
|
||||||
|
{
|
||||||
|
var supply = new Supply() { Id = id ?? Guid.NewGuid().ToString(), SupplyDate = supplyDate ?? DateTime.UtcNow, Cocktails = [] };
|
||||||
|
if (cocktails is not null)
|
||||||
|
{
|
||||||
|
foreach (var elem in cocktails)
|
||||||
|
{
|
||||||
|
supply.Cocktails.Add(new SupplyCocktail { SupplyId = supply.Id, CocktailId = elem.Item1, Count = elem.Item2 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dbContext.Supplies.Add(supply);
|
||||||
|
dbContext.SaveChanges();
|
||||||
|
return supply;
|
||||||
|
}
|
||||||
|
|
||||||
public static Client? GetClientFromDatabase(this SquirrelDbContext dbContext, string id) => dbContext.Clients.FirstOrDefault(x => x.Id == id);
|
public static Client? GetClientFromDatabase(this SquirrelDbContext dbContext, string id) => dbContext.Clients.FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
public static Post? GetPostFromDatabaseByPostId(this SquirrelDbContext dbContext, string id) => dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual);
|
public static Post? GetPostFromDatabaseByPostId(this SquirrelDbContext dbContext, string id) => dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual);
|
||||||
@@ -87,6 +117,10 @@ internal static class SquirrelDbContextExtensions
|
|||||||
|
|
||||||
public static Employee? GetEmployeeFromDatabaseById(this SquirrelDbContext dbContext, string id) => dbContext.Employees.FirstOrDefault(x => x.Id == id);
|
public static Employee? GetEmployeeFromDatabaseById(this SquirrelDbContext dbContext, string id) => dbContext.Employees.FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
|
public static Warehouse? GetWarehouseFromDatabaseById(this SquirrelDbContext dbContext, string id) => dbContext.Warehouses.Include(x => x.Cocktails).FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
|
public static Supply? GetSuppliesFromDatabaseById(this SquirrelDbContext dbContext, string id) => dbContext.Supplies.Include(x => x.Cocktails).FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
public static void RemoveClientsFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Clients\" CASCADE;");
|
public static void RemoveClientsFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Clients\" CASCADE;");
|
||||||
|
|
||||||
public static void RemovePostsFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Posts\" CASCADE;");
|
public static void RemovePostsFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Posts\" CASCADE;");
|
||||||
@@ -99,5 +133,9 @@ internal static class SquirrelDbContextExtensions
|
|||||||
|
|
||||||
public static void RemoveEmployeesFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
|
public static void RemoveEmployeesFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
|
||||||
|
|
||||||
|
public static void RemoveWarehousesFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Warehouses\" CASCADE;");
|
||||||
|
|
||||||
|
public static void RemoveSuppliesFromDatabase(this SquirrelDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Supplies\" CASCADE;");
|
||||||
|
|
||||||
private static void ExecuteSqlRaw(this SquirrelDbContext dbContext, string command) => dbContext.Database.ExecuteSqlRaw(command);
|
private static void ExecuteSqlRaw(this SquirrelDbContext dbContext, string command) => dbContext.Database.ExecuteSqlRaw(command);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,207 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Enums;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.StoragesContracts;
|
||||||
|
using SquirrelDatabase.Implementations;
|
||||||
|
using SquirrelDatabase.Models;
|
||||||
|
using SquirrelTests.StoragesContracts;
|
||||||
|
|
||||||
|
namespace SquirrelTests.StorageContracts;
|
||||||
|
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class SupplyStorageContractTests : BaseStorageContractTest
|
||||||
|
{
|
||||||
|
private SupplyStorageContract _supplyStorageContract;
|
||||||
|
private Cocktail _cocktail;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_supplyStorageContract = new SupplyStorageContract(SquirrelDbContext);
|
||||||
|
_cocktail = InsertCocktailToDatabaseAndReturn();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
SquirrelDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Supplies\" CASCADE;");
|
||||||
|
SquirrelDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Cocktails\" CASCADE;");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var supply1 = InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
var supply2 = InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
var supply3 = InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
var list = _supplyStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
var expectedSupplies = new List<Supply> { supply1, supply2, supply3 }.OrderBy(s => s.Id).ToList();
|
||||||
|
|
||||||
|
for (int i = 0; i < expectedSupplies.Count; i++)
|
||||||
|
{
|
||||||
|
AssertElement(list[i], expectedSupplies[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var list = _supplyStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByPeriod_Test()
|
||||||
|
{
|
||||||
|
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), supplyDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-3), cocktails: [(_cocktail.Id, 1)]);
|
||||||
|
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), supplyDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), cocktails: [(_cocktail.Id, 1)]);
|
||||||
|
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), supplyDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), cocktails: [(_cocktail.Id, 1)]);
|
||||||
|
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), supplyDate: DateTime.UtcNow.AddDays(1).AddMinutes(3), cocktails: [(_cocktail.Id, 1)]);
|
||||||
|
var list = _supplyStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByCocktailId_Test()
|
||||||
|
{
|
||||||
|
var cocktail = InsertCocktailToDatabaseAndReturn("other");
|
||||||
|
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), cocktails: [(_cocktail.Id, 1)]);
|
||||||
|
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), cocktails: [(_cocktail.Id, 1)]);
|
||||||
|
InsertSupplyToDatabaseAndReturn(cocktail.Id, cocktails: [(cocktail.Id, 1)]);
|
||||||
|
var list = _supplyStorageContract.GetList(cocktailId: _cocktail.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
Assert.That(list.All(x => x.Cocktails.Any(y => y.CocktailId == _cocktail.Id)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var supply = InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_supplyStorageContract.GetElementById(supply.Id), supply);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _supplyStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_Test()
|
||||||
|
{
|
||||||
|
var supply = CreateModel(Guid.NewGuid().ToString(), [_cocktail.Id]);
|
||||||
|
_supplyStorageContract.AddElement(supply);
|
||||||
|
AssertElement(GetSupplyFromDatabaseById(supply.Id), supply);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||||
|
{
|
||||||
|
var supply = CreateModel(Guid.NewGuid().ToString(), [_cocktail.Id]);
|
||||||
|
InsertSupplyToDatabaseAndReturn(supply.Id, cocktails: [(_cocktail.Id, 3)]);
|
||||||
|
Assert.That(() => _supplyStorageContract.AddElement(supply), Throws.TypeOf<StorageException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_Test()
|
||||||
|
{
|
||||||
|
var supply = CreateModel(Guid.NewGuid().ToString(), [_cocktail.Id]);
|
||||||
|
InsertSupplyToDatabaseAndReturn(supply.Id, null);
|
||||||
|
_supplyStorageContract.UpdateElement(supply);
|
||||||
|
AssertElement(GetSupplyFromDatabaseById(supply.Id), supply);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _supplyStorageContract.UpdateElement(CreateModel(Guid.NewGuid().ToString(), [_cocktail.Id])), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Cocktail InsertCocktailToDatabaseAndReturn(string cocktailName = "test")
|
||||||
|
{
|
||||||
|
var cocktail = new Cocktail { Id = Guid.NewGuid().ToString(), CocktailName = cocktailName, BaseAlcohol = AlcoholType.Vodka };
|
||||||
|
SquirrelDbContext.Cocktails.Add(cocktail);
|
||||||
|
SquirrelDbContext.SaveChanges();
|
||||||
|
return cocktail;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Supply InsertSupplyToDatabaseAndReturn(string id, DateTime? supplyDate = null, List<(string, int)>? cocktails = null)
|
||||||
|
{
|
||||||
|
var supply = new Supply { Id = id, SupplyDate = supplyDate ?? DateTime.UtcNow, Cocktails = [] };
|
||||||
|
if (cocktails is not null)
|
||||||
|
{
|
||||||
|
foreach (var elem in cocktails)
|
||||||
|
{
|
||||||
|
supply.Cocktails.Add(new SupplyCocktail { SupplyId = supply.Id, CocktailId = elem.Item1, Count = elem.Item2 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SquirrelDbContext.Supplies.Add(supply);
|
||||||
|
SquirrelDbContext.SaveChanges();
|
||||||
|
return supply;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(SupplyDataModel? actual, Supply expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
if (expected.Cocktails is not null)
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Not.Null);
|
||||||
|
Assert.That(actual.Cocktails, Has.Count.EqualTo(expected.Cocktails.Count));
|
||||||
|
for (int i = 0; i < actual.Cocktails.Count; ++i)
|
||||||
|
{
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails[i].CocktailId, Is.EqualTo(expected.Cocktails[i].CocktailId));
|
||||||
|
Assert.That(actual.Cocktails[i].Count, Is.EqualTo(expected.Cocktails[i].Count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(Supply? actual, SupplyDataModel expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
if (expected.Cocktails is not null)
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Not.Null);
|
||||||
|
Assert.That(actual.Cocktails, Has.Count.EqualTo(expected.Cocktails.Count));
|
||||||
|
for (int i = 0; i < actual.Cocktails.Count; ++i)
|
||||||
|
{
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails[i].CocktailId, Is.EqualTo(expected.Cocktails[i].CocktailId));
|
||||||
|
Assert.That(actual.Cocktails[i].Count, Is.EqualTo(expected.Cocktails[i].Count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SupplyDataModel CreateModel(string id, List<string> cocktailsIds)
|
||||||
|
{
|
||||||
|
var cocktails = cocktailsIds.Select(x => new SupplyCocktailDataModel(id, x, 1)).ToList();
|
||||||
|
return new(id, DateTime.UtcNow, cocktails);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Supply? GetSupplyFromDatabaseById(string id)
|
||||||
|
{
|
||||||
|
return SquirrelDbContext.Supplies.FirstOrDefault(x => x.Id == id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,230 @@
|
|||||||
|
|
||||||
|
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Enums;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelDatabase.Implementations;
|
||||||
|
using SquirrelDatabase.Models;
|
||||||
|
using SquirrelTests.StoragesContracts;
|
||||||
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace SquirrelTests.StorageContracts;
|
||||||
|
|
||||||
|
internal class WarehouseStorageContractTests : BaseStorageContractTest
|
||||||
|
{
|
||||||
|
private WarehouseStorageContract _warehouseStorageContract;
|
||||||
|
private Cocktail _cocktail;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_warehouseStorageContract = new WarehouseStorageContract(SquirrelDbContext);
|
||||||
|
_cocktail = InsertCocktailToDatabaseAndReturn(Guid.NewGuid().ToString(), "name", AlcoholType.Vodka);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
SquirrelDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Warehouses\" CASCADE;");
|
||||||
|
SquirrelDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Cocktails\" CASCADE;");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var warehouse = InsertWarehouseToDatabaseAndReturn(Guid.NewGuid().ToString(), "name1");
|
||||||
|
InsertWarehouseToDatabaseAndReturn(Guid.NewGuid().ToString(), "name2");
|
||||||
|
InsertWarehouseToDatabaseAndReturn(Guid.NewGuid().ToString(), "name3");
|
||||||
|
var list = _warehouseStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
AssertElement(list.First(), warehouse);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var list = _warehouseStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var warehouse = InsertWarehouseToDatabaseAndReturn(Guid.NewGuid().ToString(), "name");
|
||||||
|
var result = _warehouseStorageContract.GetElementById(warehouse.Id);
|
||||||
|
AssertElement(result, warehouse);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _warehouseStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByName_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var name = "name";
|
||||||
|
var warehouse = InsertWarehouseToDatabaseAndReturn(Guid.NewGuid().ToString(), name);
|
||||||
|
var result = _warehouseStorageContract.GetElementByName(name);
|
||||||
|
AssertElement(result, warehouse);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByName_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _warehouseStorageContract.GetElementByName("name"), Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenValidData_Test()
|
||||||
|
{
|
||||||
|
var warehouse = CreateModel(Guid.NewGuid().ToString(), "name", [_cocktail.Id]);
|
||||||
|
_warehouseStorageContract.AddElement(warehouse);
|
||||||
|
var result = GetWarehouseFromDatabaseById(warehouse.Id);
|
||||||
|
AssertElement(result, warehouse);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||||
|
{
|
||||||
|
var warehouse = CreateModel(Guid.NewGuid().ToString(), "name", [_cocktail.Id]);
|
||||||
|
InsertWarehouseToDatabaseAndReturn(warehouse.Id, "name", [(_cocktail.Id, 3)]);
|
||||||
|
Assert.That(() => _warehouseStorageContract.AddElement(warehouse), Throws.TypeOf<StorageException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenNoHaveCocktail_Test()
|
||||||
|
{
|
||||||
|
var warehouse = CreateModel(Guid.NewGuid().ToString(), "name", [_cocktail.Id]);
|
||||||
|
InsertWarehouseToDatabaseAndReturn(warehouse.Id, "name", [(_cocktail.Id, 3)]);
|
||||||
|
Assert.That(() => _warehouseStorageContract.AddElement(warehouse), Throws.TypeOf<StorageException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenValidData_Test()
|
||||||
|
{
|
||||||
|
var warehouse = CreateModel(Guid.NewGuid().ToString(), "name", [_cocktail.Id]);
|
||||||
|
InsertWarehouseToDatabaseAndReturn(warehouse.Id, "name", null);
|
||||||
|
_warehouseStorageContract.UpdElement(warehouse);
|
||||||
|
var result = GetWarehouseFromDatabaseById(warehouse.Id);
|
||||||
|
AssertElement(result, warehouse);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
var warehouse = CreateModel(Guid.NewGuid().ToString(), "name", [_cocktail.Id]);
|
||||||
|
Assert.That(() => _warehouseStorageContract.UpdElement(warehouse), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenRecordExists_Test()
|
||||||
|
{
|
||||||
|
var warehouse = InsertWarehouseToDatabaseAndReturn(Guid.NewGuid().ToString(), "name");
|
||||||
|
_warehouseStorageContract.DelElement(warehouse.Id);
|
||||||
|
var result = GetWarehouseFromDatabaseById(warehouse.Id);
|
||||||
|
Assert.That(result, Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenRecordDoesNotExist_ThrowsElementNotFoundException()
|
||||||
|
{
|
||||||
|
Assert.That(() => _warehouseStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Cocktail InsertCocktailToDatabaseAndReturn(string id, string name, AlcoholType type)
|
||||||
|
{
|
||||||
|
var cocktail = new Cocktail { Id = id, CocktailName = name, BaseAlcohol = type };
|
||||||
|
SquirrelDbContext.Cocktails.Add(cocktail);
|
||||||
|
SquirrelDbContext.SaveChanges();
|
||||||
|
return cocktail;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Warehouse InsertWarehouseToDatabaseAndReturn(string id, string name, List<(string, int)>? cocktails = null)
|
||||||
|
{
|
||||||
|
var warehouse = new Warehouse { Id = id, Name = name, Cocktails = [] };
|
||||||
|
if (cocktails is not null)
|
||||||
|
{
|
||||||
|
foreach (var elem in cocktails)
|
||||||
|
{
|
||||||
|
warehouse.Cocktails.Add(new WarehouseCocktail { WarehouseId = warehouse.Id, CocktailId = elem.Item1, Count = elem.Item2 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SquirrelDbContext.Warehouses.Add(warehouse);
|
||||||
|
SquirrelDbContext.SaveChanges();
|
||||||
|
return warehouse;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(WarehouseDataModel? actual, Warehouse expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||||
|
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||||
|
});
|
||||||
|
if (expected.Cocktails is not null)
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Not.Null);
|
||||||
|
Assert.That(actual.Cocktails, Has.Count.EqualTo(expected.Cocktails.Count));
|
||||||
|
for (int i = 0; i < actual.Cocktails.Count; ++i)
|
||||||
|
{
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails[i].CocktailId, Is.EqualTo(expected.Cocktails[i].CocktailId));
|
||||||
|
Assert.That(actual.Cocktails[i].Count, Is.EqualTo(expected.Cocktails[i].Count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(Warehouse? actual, WarehouseDataModel expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||||
|
});
|
||||||
|
if (expected.Cocktails is not null)
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Not.Null);
|
||||||
|
Assert.That(actual.Cocktails, Has.Count.EqualTo(expected.Cocktails.Count));
|
||||||
|
for (int i = 0; i < actual.Cocktails.Count; ++i)
|
||||||
|
{
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails[i].CocktailId, Is.EqualTo(expected.Cocktails[i].CocktailId));
|
||||||
|
Assert.That(actual.Cocktails[i].Count, Is.EqualTo(expected.Cocktails[i].Count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static WarehouseDataModel CreateModel(string id, string name, List<string> cocktailsIds)
|
||||||
|
{
|
||||||
|
var components = cocktailsIds.Select(x => new WarehouseCocktailDataModel(id, x, 1)).ToList();
|
||||||
|
return new(id, name, components);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Warehouse? GetWarehouseFromDatabaseById(string id)
|
||||||
|
{
|
||||||
|
return SquirrelDbContext.Warehouses.FirstOrDefault(x => x.Id == id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,316 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using Moq;
|
||||||
|
using SquirrelContract.AdapterContracts.OperationResponses;
|
||||||
|
using SquirrelContract.BindingModels;
|
||||||
|
using SquirrelContract.BusinessLogicContracts;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.ViewModels;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace SquirrelWebApi.Adapters.Tests
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class WarehouseAdapterTests
|
||||||
|
{
|
||||||
|
private Mock<IWarehouseBusinessLogicContract> _businessLogicMock;
|
||||||
|
private Mock<ILogger<WarehouseAdapter>> _loggerMock;
|
||||||
|
private WarehouseAdapter _adapter;
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_businessLogicMock = new Mock<IWarehouseBusinessLogicContract>();
|
||||||
|
_loggerMock = new Mock<ILogger<WarehouseAdapter>>();
|
||||||
|
_adapter = new WarehouseAdapter(
|
||||||
|
_businessLogicMock.Object,
|
||||||
|
_loggerMock.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWarehouses_WhenWarehousesExist_ReturnOk()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var warehouses = new List<WarehouseDataModel>
|
||||||
|
{
|
||||||
|
new WarehouseDataModel(Guid.NewGuid().ToString(), "Warehouse A", []),
|
||||||
|
new WarehouseDataModel(Guid.NewGuid().ToString(), "Warehouse B", [])
|
||||||
|
};
|
||||||
|
|
||||||
|
_businessLogicMock.Setup(x => x.GetAllWarehouses()).Returns(warehouses);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _adapter.GetAllWarehouses();
|
||||||
|
var list = (List<WarehouseViewModel>)result.Result!;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||||
|
Assert.That(list.Count, Is.EqualTo(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWarehouses_WhenListNull_ThrowsNotFound()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_businessLogicMock.Setup(x => x.GetAllWarehouses()).Throws(new NullListException());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _adapter.GetAllWarehouses();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWarehouses_WhenStorageException_ReturnsInternalServerError()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_businessLogicMock.Setup(x => x.GetAllWarehouses()).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
// Act
|
||||||
|
var result = _adapter.GetAllWarehouses();
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.InternalServerError));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWarehouse_ValidModel_ReturnsNoContent()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var model = new WarehouseBindingModel
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
Name = "Test Name",
|
||||||
|
Cocktails = [new WarehouseCocktailBindingModel { WarehouseId = Guid.NewGuid().ToString(), CocktailId = Guid.NewGuid().ToString(), Count = 5 }]
|
||||||
|
};
|
||||||
|
_businessLogicMock.Setup(x => x.InsertWarehouse(It.IsAny<WarehouseDataModel>()));
|
||||||
|
// Act
|
||||||
|
var result = _adapter.InsertWarehouse(model);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWarehouse_WhenDataIsNull_ReturnsBadRequest()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_businessLogicMock.Setup(x => x.InsertWarehouse(null)).Throws(new ArgumentNullException());
|
||||||
|
// Act
|
||||||
|
var result = _adapter.InsertWarehouse(null);
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWarehouse_WhenValidationError_ReturnBadRequest()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var warehouse = new WarehouseBindingModel
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
Name = "name",
|
||||||
|
Count = 5,
|
||||||
|
Cocktails = []
|
||||||
|
};
|
||||||
|
// Act
|
||||||
|
var result = _adapter.InsertWarehouse(warehouse);
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWarehouse_WhenElementExistException_ReturnBadRequest()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var warehouse1 = new WarehouseBindingModel
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
Name = "name",
|
||||||
|
Count = 5,
|
||||||
|
Cocktails = []
|
||||||
|
};
|
||||||
|
_businessLogicMock.Setup(x => x.InsertWarehouse(It.IsAny<WarehouseDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
|
// Act
|
||||||
|
var result = _adapter.InsertWarehouse(warehouse1);
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWarehouse_WhenStorageException_ReturnBadRequest()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var warehouse = new WarehouseBindingModel
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
Name = "name",
|
||||||
|
Count = 5,
|
||||||
|
Cocktails = new List<WarehouseCocktailBindingModel>()
|
||||||
|
};
|
||||||
|
_businessLogicMock.Setup(x => x.InsertWarehouse(It.IsAny<WarehouseDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
// Act
|
||||||
|
var result = _adapter.InsertWarehouse(warehouse);
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWarehouse_WhenValidData_ReturnsNoContent()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var warehouse = new WarehouseBindingModel { Id = Guid.NewGuid().ToString() };
|
||||||
|
_businessLogicMock.Setup(x => x.UpdateWarehouse(It.IsAny<WarehouseDataModel>()));
|
||||||
|
// Act
|
||||||
|
var result = _adapter.UpdateWarehouse(warehouse);
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWarehouse_WhenDataIsNull_ReturnsBadRequest()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var component = new WarehouseBindingModel { Id = Guid.NewGuid().ToString() };
|
||||||
|
_businessLogicMock.Setup(x => x.UpdateWarehouse(It.IsAny<WarehouseDataModel>())).Throws(new ElementNotFoundException(""));
|
||||||
|
// Act
|
||||||
|
var result = _adapter.UpdateWarehouse(component);
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWarehouse_WhenValidationError_ReturnBadRequest()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var warehouse = new WarehouseBindingModel
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
Name = "name",
|
||||||
|
Count = 5,
|
||||||
|
Cocktails = []
|
||||||
|
};
|
||||||
|
_businessLogicMock.Setup(x => x.UpdateWarehouse(It.IsAny<WarehouseDataModel>())).Throws(new ValidationException(""));
|
||||||
|
// Act
|
||||||
|
var result = _adapter.UpdateWarehouse(warehouse);
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWarehouse_WhenElementNotFoundException_ReturnBadRequest()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var warehouse = new WarehouseBindingModel
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
Name = "name",
|
||||||
|
Count = 5,
|
||||||
|
Cocktails = []
|
||||||
|
};
|
||||||
|
_businessLogicMock.Setup(x => x.UpdateWarehouse(It.IsAny<WarehouseDataModel>())).Throws(new ElementNotFoundException(""));
|
||||||
|
// Act
|
||||||
|
var result = _adapter.UpdateWarehouse(warehouse);
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWarehouse_WhenElementExistException_ReturnBadRequest()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var warehouse1 = new WarehouseBindingModel
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
Name = "name",
|
||||||
|
Count = 5,
|
||||||
|
Cocktails = []
|
||||||
|
};
|
||||||
|
_businessLogicMock.Setup(x => x.UpdateWarehouse(It.IsAny<WarehouseDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
|
// Act
|
||||||
|
var result = _adapter.UpdateWarehouse(warehouse1);
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWarehouse_WhenStorageException_ReturnBadRequest()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var warehouse = new WarehouseBindingModel
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
Name = "name",
|
||||||
|
Count = 5,
|
||||||
|
Cocktails = new List<WarehouseCocktailBindingModel>()
|
||||||
|
};
|
||||||
|
_businessLogicMock.Setup(x => x.UpdateWarehouse(It.IsAny<WarehouseDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
// Act
|
||||||
|
var result = _adapter.UpdateWarehouse(warehouse);
|
||||||
|
// Assert
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteWarehouse_ElementNotFound_ReturnsBadRequest()
|
||||||
|
{
|
||||||
|
_businessLogicMock
|
||||||
|
.Setup(x => x.DeleteWarehouse(It.IsAny<string>()))
|
||||||
|
.Throws(new ElementNotFoundException(""));
|
||||||
|
|
||||||
|
var result = _adapter.DeleteWarehouse("id");
|
||||||
|
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteWarehouse_StorageException_ReturnsBadRequest()
|
||||||
|
{
|
||||||
|
_businessLogicMock
|
||||||
|
.Setup(x => x.DeleteWarehouse(It.IsAny<string>()))
|
||||||
|
.Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
|
||||||
|
var result = _adapter.DeleteWarehouse("id");
|
||||||
|
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetWarehouseByData_ValidId_ReturnsWarehouse()
|
||||||
|
{
|
||||||
|
var id = "id";
|
||||||
|
var data = new WarehouseDataModel(id, "Warehouse X", []);
|
||||||
|
_businessLogicMock.Setup(x => x.GetWarehouseByData(id)).Returns(data);
|
||||||
|
|
||||||
|
var result = _adapter.GetWarehouseByData(id);
|
||||||
|
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetWarehouseByData_ElementNotFound_ReturnsNotFound()
|
||||||
|
{
|
||||||
|
var id = "test";
|
||||||
|
_businessLogicMock.Setup(x => x.GetWarehouseByData(id)).Throws(new ElementNotFoundException(""));
|
||||||
|
|
||||||
|
var result = _adapter.GetWarehouseByData(id);
|
||||||
|
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetWarehouseByData_StorageException_ReturnsInternalServerError()
|
||||||
|
{
|
||||||
|
_businessLogicMock.Setup(x => x.GetWarehouseByData("id")).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
|
||||||
|
var result = _adapter.GetWarehouseByData("id");
|
||||||
|
|
||||||
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.InternalServerError));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ using SquirrelContract.ViewModels;
|
|||||||
using SquirrelDatabase.Models;
|
using SquirrelDatabase.Models;
|
||||||
using SquirrelTests.Infrastructure;
|
using SquirrelTests.Infrastructure;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
namespace SquirrelTests.WebApiControllersTests;
|
namespace SquirrelTests.WebApiControllersTests;
|
||||||
|
|
||||||
@@ -29,6 +30,7 @@ internal class SaleControllerTests : BaseWebApiControllerTest
|
|||||||
SquirrelDbContext.RemoveEmployeesFromDatabase();
|
SquirrelDbContext.RemoveEmployeesFromDatabase();
|
||||||
SquirrelDbContext.RemoveClientsFromDatabase();
|
SquirrelDbContext.RemoveClientsFromDatabase();
|
||||||
SquirrelDbContext.RemoveCocktailsFromDatabase();
|
SquirrelDbContext.RemoveCocktailsFromDatabase();
|
||||||
|
SquirrelDbContext.RemoveWarehousesFromDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@@ -305,21 +307,24 @@ internal class SaleControllerTests : BaseWebApiControllerTest
|
|||||||
public async Task Post_ShouldSuccess_Test()
|
public async Task Post_ShouldSuccess_Test()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
SquirrelDbContext.InsertSaleToDatabaseAndReturn(_employeeId, null, cocktails: [(_cocktailId, 5, 1.1)]);
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn(cocktails: [(_cocktailId, 10)]);
|
||||||
var saleModel = CreateModel(_employeeId, _clientId, _cocktailId);
|
|
||||||
|
var saleModel = CreateModel(_employeeId, _clientId, cocktails: [(_cocktailId, 5, 1.1)]);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(saleModel));
|
var response = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(saleModel));
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||||
AssertElement(SquirrelDbContext.GetSalesByClientId(_clientId)[0], saleModel);
|
AssertElement(SquirrelDbContext.GetSaleFromDatabaseById(saleModel.Id!), saleModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task Post_WhenNoClient_ShouldSuccess_Test()
|
public async Task Post_WhenNoClient_ShouldSuccess_Test()
|
||||||
{
|
{
|
||||||
//Arrange
|
//Arrange
|
||||||
SquirrelDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, cocktails: [(_cocktailId, 5, 1.1)]);
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn(cocktails: [(_cocktailId, 10)]);
|
||||||
var saleModel = CreateModel(_employeeId, null, _cocktailId);
|
var saleModel = CreateModel(_employeeId, null, cocktails: [(_cocktailId, 5, 1.1)]);
|
||||||
//Act
|
//Act
|
||||||
var response = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(saleModel));
|
var response = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(saleModel));
|
||||||
//Assert
|
//Assert
|
||||||
@@ -457,17 +462,18 @@ internal class SaleControllerTests : BaseWebApiControllerTest
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SaleBindingModel CreateModel(string employeeId, string? clientId, string cocktailId, string? id = null, DiscountType discountType = DiscountType.OnSale, int count = 1, double price = 1.1)
|
private static SaleBindingModel CreateModel(string employeeId, string? clientId, string? id = null, DiscountType discountType = DiscountType.OnSale, List<(string, int, double)>? cocktails = null)
|
||||||
{
|
{
|
||||||
var saleId = id ?? Guid.NewGuid().ToString();
|
var sale = new SaleBindingModel() { Id = id ?? Guid.NewGuid().ToString(), EmployeeId = employeeId, ClientId = clientId, DiscountType = (int)discountType, Cocktails = [] };
|
||||||
return new()
|
if (cocktails is not null)
|
||||||
|
if (cocktails is not null)
|
||||||
{
|
{
|
||||||
Id = saleId,
|
foreach (var elem in cocktails)
|
||||||
EmployeeId = employeeId,
|
{
|
||||||
ClientId = clientId,
|
sale.Cocktails.Add(new SaleCocktailBindingModel { SaleId = sale.Id, CocktailId = elem.Item1, Count = elem.Item2, Price = elem.Item3 });
|
||||||
DiscountType = (int)discountType,
|
}
|
||||||
Cocktails = [new SaleCocktailBindingModel { SaleId = saleId, CocktailId = cocktailId, Count = count, Price = price }]
|
}
|
||||||
};
|
return sale;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void AssertElement(Sale? actual, SaleBindingModel expected)
|
private static void AssertElement(Sale? actual, SaleBindingModel expected)
|
||||||
|
|||||||
@@ -0,0 +1,306 @@
|
|||||||
|
using SquirrelContract.BindingModels;
|
||||||
|
using SquirrelContract.Enums;
|
||||||
|
using SquirrelContract.ViewModels;
|
||||||
|
using SquirrelDatabase.Models;
|
||||||
|
using SquirrelTests.Infrastructure;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace SquirrelTests.WebApiControllersTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class SupplyControllerTests : BaseWebApiControllerTest
|
||||||
|
{
|
||||||
|
private string _cocktailId;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_cocktailId = SquirrelDbContext.InsertCocktailToDatabaseAndReturn().Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
SquirrelDbContext.RemoveCocktailsFromDatabase();
|
||||||
|
SquirrelDbContext.RemoveSuppliesFromDatabase();
|
||||||
|
SquirrelDbContext.RemoveWarehousesFromDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetList_WhenHaveRecords_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var supply = SquirrelDbContext.InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), cocktails: [(_cocktailId, 10)]);
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), cocktails: [(_cocktailId, 10)]);
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), cocktails: [(_cocktailId, 10)]);
|
||||||
|
// Act
|
||||||
|
var response = await HttpClient.GetAsync($"/api/supplies/getrecords?fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}");
|
||||||
|
// Assert
|
||||||
|
var data = await GetModelFromResponseAsync<List<SupplyViewModel>>(response);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(data, Is.Not.Null);
|
||||||
|
Assert.That(data, Has.Count.EqualTo(3));
|
||||||
|
});
|
||||||
|
AssertElement(data.First(x => x.Id == supply.Id), supply);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetList_WhenNoRecords_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.GetAsync($"/api/supplies/getrecords?fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}");
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||||
|
var data = await GetModelFromResponseAsync<List<SupplyViewModel>>(response);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(data, Is.Not.Null);
|
||||||
|
Assert.That(data, Has.Count.EqualTo(0));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetList_ByPeriod_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), supplyDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-3), cocktails: [(_cocktailId, 1)]);
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), supplyDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), cocktails: [(_cocktailId, 1)]);
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), supplyDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), cocktails: [(_cocktailId, 1)]);
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), supplyDate: DateTime.UtcNow.AddDays(1).AddMinutes(3), cocktails: [(_cocktailId, 1)]);
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.GetAsync($"/api/supplies/getrecords?fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}");
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||||
|
var data = await GetModelFromResponseAsync<List<SupplyViewModel>>(response);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(data, Is.Not.Null);
|
||||||
|
Assert.That(data, Has.Count.EqualTo(2));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetList_ByCocktailId_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var cocktail = SquirrelDbContext.InsertCocktailToDatabaseAndReturn(Guid.NewGuid().ToString(), cocktailName: "name1");
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(cocktails: [(_cocktailId, 5)]);
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(cocktails: [(_cocktailId, 1), (cocktail.Id, 4)]);
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(cocktails: [(cocktail.Id, 1)]);
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(cocktails: [(cocktail.Id, 1), (_cocktailId, 1)]);
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.GetAsync($"/api/supplies/getcocktailrecords?id={_cocktailId}&fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}");
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||||
|
var data = await GetModelFromResponseAsync<List<SupplyViewModel>>(response);
|
||||||
|
Assert.That(data, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(data, Has.Count.EqualTo(3));
|
||||||
|
Assert.That(data.All(x => x.Cocktails.Any(y => y.CocktailId == _cocktailId)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetList_ByCocktailId_WhenNoRecords_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var cocktail = SquirrelDbContext.InsertCocktailToDatabaseAndReturn(Guid.NewGuid().ToString(), cocktailName: "Other cocktail");
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(cocktails: [(_cocktailId, 1)]);
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.GetAsync($"/api/supplies/getcocktailrecords?id={cocktail.Id}&fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}");
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||||
|
var data = await GetModelFromResponseAsync<List<SupplyViewModel>>(response);
|
||||||
|
Assert.That(data, Is.Not.Null);
|
||||||
|
Assert.That(data, Has.Count.EqualTo(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var supplies = SquirrelDbContext.InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.GetAsync($"/api/supplies/getcomponentbydata/{supplies.Id}");
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||||
|
AssertElement(await GetModelFromResponseAsync<SupplyViewModel>(response), supplies);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.GetAsync($"/api/supplies/{Guid.NewGuid()}");
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Post_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), cocktails: [(_cocktailId, 5)]);
|
||||||
|
var supplies = CreateModel(cocktails: [(_cocktailId, 5)]);
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PostAsync($"/api/supplies/register", MakeContent(supplies));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||||
|
AssertElement(SquirrelDbContext.GetSuppliesFromDatabaseById(supplies.Id!), supplies);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PostAsync($"/api/supplies/register", MakeContent(string.Empty));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Post_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PostAsync($"/api/supplies/register", MakeContent(new { Data = "test", Position = 10 }));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var suppliesIdIncorrect = new SupplyViewModel { Id = "Id", Cocktails = [] };
|
||||||
|
var suppliesWithTypeIncorrect = new SupplyViewModel { Id = Guid.NewGuid().ToString(), Cocktails = [] };
|
||||||
|
//Act
|
||||||
|
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/supplies/register", MakeContent(suppliesIdIncorrect));
|
||||||
|
var responseWithTypeIncorrect = await HttpClient.PostAsync($"/api/supplies/register", MakeContent(suppliesWithTypeIncorrect));
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
||||||
|
Assert.That(responseWithTypeIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Type is incorrect");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Put_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn();
|
||||||
|
var supplies = CreateModel(cocktails: [(_cocktailId, 5)]);
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(supplies.Id);
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PutAsync($"/api/supplies/changeinfo", MakeContent(supplies));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||||
|
SquirrelDbContext.ChangeTracker.Clear();
|
||||||
|
AssertElement(SquirrelDbContext.GetSuppliesFromDatabaseById(supplies.Id!), supplies);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var sale = CreateModel();
|
||||||
|
SquirrelDbContext.InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PutAsync($"/api/supplies/changeinfo", MakeContent(sale));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PutAsync($"/api/supplies/changeinfo", MakeContent(string.Empty));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Put_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PutAsync($"/api/supplies/changeinfo", MakeContent(new { Data = "test", Position = 10 }));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static SupplyBindingModel CreateModel(string? id = null, DateTime? date = null, List<(string, int)>? cocktails = null)
|
||||||
|
{
|
||||||
|
var Supply = new SupplyBindingModel() { Id = id ?? Guid.NewGuid().ToString(), SupplyDate = date ?? DateTime.UtcNow, Cocktails = [] };
|
||||||
|
if (cocktails is not null)
|
||||||
|
{
|
||||||
|
foreach (var elem in cocktails)
|
||||||
|
{
|
||||||
|
Supply.Cocktails.Add(new SupplyCocktailBindingModel { SupplyId = Supply.Id, CocktailId = elem.Item1, Count = elem.Item2 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Supply;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(SupplyViewModel? actual, Supply expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.SupplyDate.Date, Is.EqualTo(expected.SupplyDate.Date));
|
||||||
|
});
|
||||||
|
if (expected.Cocktails is not null)
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Not.Null);
|
||||||
|
Assert.That(actual.Cocktails, Has.Count.EqualTo(expected.Cocktails.Count));
|
||||||
|
for (int i = 0; i < actual.Cocktails.Count; ++i)
|
||||||
|
{
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails[i].SupplyId, Is.EqualTo(expected.Cocktails[i].SupplyId));
|
||||||
|
Assert.That(actual.Cocktails[i].Count, Is.EqualTo(expected.Cocktails[i].Count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(Supply? actual, SupplyBindingModel expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.SupplyDate.Date, Is.EqualTo(expected.SupplyDate.Date));
|
||||||
|
});
|
||||||
|
if (expected.Cocktails is not null)
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Not.Null);
|
||||||
|
Assert.That(actual.Cocktails, Has.Count.EqualTo(expected.Cocktails.Count));
|
||||||
|
for (int i = 0; i < actual.Cocktails.Count; ++i)
|
||||||
|
{
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails[i].SupplyId, Is.EqualTo(expected.Cocktails[i].SupplyId));
|
||||||
|
Assert.That(actual.Cocktails[i].Count, Is.EqualTo(expected.Cocktails[i].Count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,306 @@
|
|||||||
|
using SquirrelContract.BindingModels;
|
||||||
|
using SquirrelContract.Enums;
|
||||||
|
using SquirrelContract.ViewModels;
|
||||||
|
using SquirrelDatabase.Models;
|
||||||
|
using SquirrelTests.Infrastructure;
|
||||||
|
using System.Net;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
|
namespace SquirrelTests.WebApiControllersTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class WarehouseControllerTests : BaseWebApiControllerTest
|
||||||
|
{
|
||||||
|
private string _cocktailId;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_cocktailId = SquirrelDbContext.InsertCocktailToDatabaseAndReturn().Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
SquirrelDbContext.RemoveCocktailsFromDatabase();
|
||||||
|
SquirrelDbContext.RemoveWarehousesFromDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetAllWarehouses_WhenHaveRecords_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var warehouse = SquirrelDbContext.InsertWarehouseToDatabaseAndReturn(name: "1");
|
||||||
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn(name: "2");
|
||||||
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn(name: "3");
|
||||||
|
// Act
|
||||||
|
var response = await HttpClient.GetAsync("/api/warehouses");
|
||||||
|
// Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||||
|
var data = await GetModelFromResponseAsync<List<WarehouseViewModel>>(response);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(data, Is.Not.Null);
|
||||||
|
Assert.That(data, Has.Count.EqualTo(3));
|
||||||
|
});
|
||||||
|
AssertElement(data.First(x => x.Id == warehouse.Id), warehouse);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetList_WhenNoRecords_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.GetAsync("/api/warehouses");
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||||
|
var data = await GetModelFromResponseAsync<List<WarehouseViewModel>>(response);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(data, Is.Not.Null);
|
||||||
|
Assert.That(data, Has.Count.EqualTo(0));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var warehouse = SquirrelDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.GetAsync($"/api/warehouses/{warehouse.Id}");
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||||
|
AssertElement(await GetModelFromResponseAsync<WarehouseViewModel>(response), warehouse);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.GetAsync($"/api/warehouses/{Guid.NewGuid()}");
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task InsertWarehouseToDatabaseAndReturn()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var warehouse = SquirrelDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.GetAsync($"/api/warehouses/{warehouse.Name}");
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||||
|
AssertElement(await GetModelFromResponseAsync<WarehouseViewModel>(response), warehouse);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetElement_ByName_WhenNoRecord_ShouldNotFound_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.GetAsync($"/api/warehouses/New%20Name");
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Post_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn(cocktails: [(_cocktailId, 5)]);
|
||||||
|
var warehouse = CreateModel(cocktails: [(_cocktailId, 5)]);
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PostAsync($"/api/warehouses", MakeContent(warehouse));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||||
|
AssertElement(SquirrelDbContext.GetWarehouseFromDatabaseById(warehouse.Id!), warehouse);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PostAsync($"/api/warehouses", MakeContent(string.Empty));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Post_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PostAsync($"/api/warehouses", MakeContent(new { Data = "test", Position = 10 }));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var warehouseIdIncorrect = new WarehouseBindingModel { Id = "Id" };
|
||||||
|
var warehouseWithNameIncorrect = new WarehouseBindingModel { Id = Guid.NewGuid().ToString(), Name = "name" };
|
||||||
|
//Act
|
||||||
|
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/warehouses", MakeContent(warehouseIdIncorrect));
|
||||||
|
var responseWithNameIncorrect = await HttpClient.PostAsync($"/api/warehouses", MakeContent(warehouseWithNameIncorrect));
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
||||||
|
Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Type is incorrect");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Put_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn(name: "1");
|
||||||
|
var warehouse = CreateModel(cocktails: [(_cocktailId, 5)]);
|
||||||
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn(warehouse.Id, name: "2");
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PutAsync($"/api/warehouses", MakeContent(warehouse));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||||
|
SquirrelDbContext.ChangeTracker.Clear();
|
||||||
|
AssertElement(SquirrelDbContext.GetWarehouseFromDatabaseById(warehouse.Id!), warehouse);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var warehouse = CreateModel();
|
||||||
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PutAsync($"/api/warehouses", MakeContent(warehouse));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PutAsync($"/api/warehouses", MakeContent(string.Empty));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Put_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.PutAsync($"/api/warehouses", MakeContent(new { Data = "test", Position = 10 }));
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Delete_ShouldSuccess_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var cocktail = Guid.NewGuid().ToString();
|
||||||
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn(cocktail);
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.DeleteAsync($"/api/warehouses/{cocktail}");
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||||
|
Assert.That(SquirrelDbContext.GetWarehouseFromDatabaseById(cocktail), Is.Null);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
SquirrelDbContext.InsertWarehouseToDatabaseAndReturn();
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.DeleteAsync($"/api/warehouses/{Guid.NewGuid()}");
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task Delete_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||||
|
{
|
||||||
|
//Act
|
||||||
|
var response = await HttpClient.DeleteAsync($"/api/warehouses/id");
|
||||||
|
//Assert
|
||||||
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static WarehouseBindingModel CreateModel(string? id = null, string name = "name", List<(string, int)>? cocktails = null)
|
||||||
|
{
|
||||||
|
var warehouse = new WarehouseBindingModel() { Id = id ?? Guid.NewGuid().ToString(), Name = name, Cocktails = [] };
|
||||||
|
if (cocktails is not null)
|
||||||
|
{
|
||||||
|
foreach (var elem in cocktails)
|
||||||
|
{
|
||||||
|
warehouse.Cocktails.Add(new WarehouseCocktailBindingModel { WarehouseId = warehouse.Id, CocktailId = elem.Item1, Count = elem.Item2 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return warehouse;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(WarehouseViewModel? actual, Warehouse expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||||
|
});
|
||||||
|
if (expected.Cocktails is not null)
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Not.Null);
|
||||||
|
Assert.That(actual.Cocktails, Has.Count.EqualTo(expected.Cocktails.Count));
|
||||||
|
for (int i = 0; i < actual.Cocktails.Count; ++i)
|
||||||
|
{
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails[i].CocktailId, Is.EqualTo(expected.Cocktails[i].CocktailId));
|
||||||
|
Assert.That(actual.Cocktails[i].Count, Is.EqualTo(expected.Cocktails[i].Count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(Warehouse? actual, WarehouseBindingModel expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||||
|
});
|
||||||
|
if (expected.Cocktails is not null)
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Not.Null);
|
||||||
|
Assert.That(actual.Cocktails, Has.Count.EqualTo(expected.Cocktails.Count));
|
||||||
|
for (int i = 0; i < actual.Cocktails.Count; ++i)
|
||||||
|
{
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails[i].CocktailId, Is.EqualTo(expected.Cocktails[i].CocktailId));
|
||||||
|
Assert.That(actual.Cocktails[i].Count, Is.EqualTo(expected.Cocktails[i].Count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.That(actual.Cocktails, Is.Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -195,7 +195,6 @@ public class SaleAdapter : ISaleAdapter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var data = _mapper.Map<SaleDataModel>(saleModel);
|
|
||||||
_saleBusinessLogicContract.InsertSale(_mapper.Map<SaleDataModel>(saleModel));
|
_saleBusinessLogicContract.InsertSale(_mapper.Map<SaleDataModel>(saleModel));
|
||||||
return SaleOperationResponse.NoContent();
|
return SaleOperationResponse.NoContent();
|
||||||
}
|
}
|
||||||
@@ -214,11 +213,17 @@ public class SaleAdapter : ISaleAdapter
|
|||||||
_logger.LogError(ex, "StorageException");
|
_logger.LogError(ex, "StorageException");
|
||||||
return SaleOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
return SaleOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
||||||
}
|
}
|
||||||
|
catch (InsufficientStockException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "InsufficientStockException");
|
||||||
|
return SaleOperationResponse.BadRequest("Not enough items in stock");
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Exception");
|
_logger.LogError(ex, "Exception");
|
||||||
return SaleOperationResponse.InternalServerError(ex.Message);
|
return SaleOperationResponse.InternalServerError(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SaleOperationResponse CancelSale(string id)
|
public SaleOperationResponse CancelSale(string id)
|
||||||
|
|||||||
184
SquirrelContract/SquirrelWebApi/Adapters/SupplyAdapter.cs
Normal file
184
SquirrelContract/SquirrelWebApi/Adapters/SupplyAdapter.cs
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using SquirrelBusinessLogic.Implementations;
|
||||||
|
using SquirrelContract.AdapterContracts;
|
||||||
|
using SquirrelContract.AdapterContracts.OperationResponses;
|
||||||
|
using SquirrelContract.BindingModels;
|
||||||
|
using SquirrelContract.BusinessLogicContracts;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.ViewModels;
|
||||||
|
|
||||||
|
namespace SquirrelWebApi.Adapters;
|
||||||
|
|
||||||
|
public class SupplyAdapter : ISupplyAdapter
|
||||||
|
{
|
||||||
|
private readonly ISupplyBusinessLogicContract _suppliesBusinessLogicContract;
|
||||||
|
private readonly ILogger<SupplyAdapter> _logger;
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
|
public SupplyAdapter(ISupplyBusinessLogicContract suppliesBusinessLogicContract,
|
||||||
|
ILogger<SupplyAdapter> logger)
|
||||||
|
{
|
||||||
|
_suppliesBusinessLogicContract = suppliesBusinessLogicContract;
|
||||||
|
_logger = logger;
|
||||||
|
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<SupplyBindingModel, SupplyDataModel>();
|
||||||
|
cfg.CreateMap<SupplyDataModel, SupplyViewModel>();
|
||||||
|
cfg.CreateMap<SupplyCocktailBindingModel, SupplyCocktailDataModel>();
|
||||||
|
cfg.CreateMap<SupplyCocktailDataModel, SupplyCocktailViewModel>();
|
||||||
|
});
|
||||||
|
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SupplyOperationResponse GetAllSuppliesByPeriod(DateTime fromDate, DateTime toDate)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return SupplyOperationResponse.OK([.. _suppliesBusinessLogicContract.GetAllSuppliesByPeriod(fromDate, toDate).Select(x => _mapper.Map<SupplyViewModel>(x))]);
|
||||||
|
}
|
||||||
|
catch (NullListException)
|
||||||
|
{
|
||||||
|
_logger.LogError("NullListException: The list of supplies is null");
|
||||||
|
return SupplyOperationResponse.NotFound("The list of supplies is not initialized");
|
||||||
|
}
|
||||||
|
catch (StorageException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||||
|
return SupplyOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||||
|
return SupplyOperationResponse.InternalServerError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public SupplyOperationResponse GetCocktailList(string id, DateTime fromDate, DateTime toDate)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return SupplyOperationResponse.OK([.. _suppliesBusinessLogicContract.GetAllSuppliesByCocktailByPeriod(id, fromDate, toDate).Select(x => _mapper.Map<SupplyViewModel>(x))]);
|
||||||
|
}
|
||||||
|
catch (NullListException)
|
||||||
|
{
|
||||||
|
_logger.LogError("NullListException: The list of supplies is null");
|
||||||
|
return SupplyOperationResponse.NotFound("The list of supplies is not initialized");
|
||||||
|
}
|
||||||
|
catch (StorageException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||||
|
return SupplyOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||||
|
return SupplyOperationResponse.InternalServerError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public SupplyOperationResponse GetSupplyByData(string data)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return SupplyOperationResponse.OK(_mapper.Map<SupplyViewModel>(_suppliesBusinessLogicContract.GetSupplyByData(data)));
|
||||||
|
}
|
||||||
|
catch (ArgumentNullException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ArgumentNullException: Data is null or empty");
|
||||||
|
return SupplyOperationResponse.BadRequest("Data is empty");
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ElementNotFoundException: Supply not found");
|
||||||
|
return SupplyOperationResponse.NotFound($"Supply with data '{data}' not found");
|
||||||
|
}
|
||||||
|
catch (StorageException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||||
|
return SupplyOperationResponse.InternalServerError(
|
||||||
|
$"Error while working with data storage: {ex.InnerException?.Message}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||||
|
return SupplyOperationResponse.InternalServerError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public SupplyOperationResponse InsertSupply(SupplyBindingModel supplyDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_suppliesBusinessLogicContract.InsertSupply(_mapper.Map<SupplyDataModel>(supplyDataModel));
|
||||||
|
return SupplyOperationResponse.NoContent();
|
||||||
|
}
|
||||||
|
catch (ArgumentNullException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ArgumentNullException: Supply data is null");
|
||||||
|
return SupplyOperationResponse.BadRequest("Supply data is empty");
|
||||||
|
}
|
||||||
|
catch (ValidationException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ValidationException: Invalid component data");
|
||||||
|
return SupplyOperationResponse.BadRequest($"Invalid component data: {ex.Message}");
|
||||||
|
}
|
||||||
|
catch (ElementExistsException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ElementExistsException: Supply already exists");
|
||||||
|
return SupplyOperationResponse.BadRequest(ex.Message);
|
||||||
|
}
|
||||||
|
catch (StorageException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||||
|
return SupplyOperationResponse.BadRequest(
|
||||||
|
$"Error while working with data storage: {ex.InnerException?.Message}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||||
|
return SupplyOperationResponse.InternalServerError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public SupplyOperationResponse UpdateSupply(SupplyBindingModel supplyModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_suppliesBusinessLogicContract.UpdateSupply(_mapper.Map<SupplyDataModel>(supplyModel));
|
||||||
|
return SupplyOperationResponse.NoContent();
|
||||||
|
}
|
||||||
|
catch (ArgumentNullException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ArgumentNullException: Supply data is null");
|
||||||
|
return SupplyOperationResponse.BadRequest("Supply data is empty");
|
||||||
|
}
|
||||||
|
catch (ValidationException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ValidationException: Invalid component data");
|
||||||
|
return SupplyOperationResponse.BadRequest($"Invalid component data: {ex.Message}");
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ElementNotFoundException: Supply not found");
|
||||||
|
return SupplyOperationResponse.BadRequest($"Supply with id '{supplyModel.Id}' not found");
|
||||||
|
}
|
||||||
|
catch (ElementExistsException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ElementExistsException: Supply already exists");
|
||||||
|
return SupplyOperationResponse.BadRequest(ex.Message);
|
||||||
|
}
|
||||||
|
catch (StorageException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||||
|
return SupplyOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||||
|
return SupplyOperationResponse.InternalServerError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
190
SquirrelContract/SquirrelWebApi/Adapters/WarehouseAdapter.cs
Normal file
190
SquirrelContract/SquirrelWebApi/Adapters/WarehouseAdapter.cs
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using SquirrelContract.AdapterContracts;
|
||||||
|
using SquirrelContract.AdapterContracts.OperationResponses;
|
||||||
|
using SquirrelContract.BindingModels;
|
||||||
|
using SquirrelContract.BusinessLogicContracts;
|
||||||
|
using SquirrelContract.DataModels;
|
||||||
|
using SquirrelContract.Exceptions;
|
||||||
|
using SquirrelContract.ViewModels;
|
||||||
|
|
||||||
|
namespace SquirrelWebApi.Adapters;
|
||||||
|
|
||||||
|
public class WarehouseAdapter : IWarehouseAdapter
|
||||||
|
{
|
||||||
|
private readonly IWarehouseBusinessLogicContract _warehouseBusinessLogicContract;
|
||||||
|
private readonly ILogger<WarehouseAdapter> _logger;
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
|
public WarehouseAdapter(IWarehouseBusinessLogicContract warehouseBusinessLogicContract, ILogger<WarehouseAdapter> logger)
|
||||||
|
{
|
||||||
|
_warehouseBusinessLogicContract = warehouseBusinessLogicContract;
|
||||||
|
_logger = logger;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<WarehouseBindingModel, WarehouseDataModel>();
|
||||||
|
cfg.CreateMap<WarehouseDataModel, WarehouseViewModel>();
|
||||||
|
cfg.CreateMap<WarehouseCocktailBindingModel, WarehouseCocktailDataModel>();
|
||||||
|
cfg.CreateMap<WarehouseCocktailDataModel, WarehouseCocktailViewModel>();
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public WarehouseOperationResponse GetAllWarehouses()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Console.WriteLine(_warehouseBusinessLogicContract.GetAllWarehouses());
|
||||||
|
return WarehouseOperationResponse.OK([.. _warehouseBusinessLogicContract.GetAllWarehouses().Select(x => _mapper.Map<WarehouseViewModel>(x))]);
|
||||||
|
}
|
||||||
|
catch (NullListException)
|
||||||
|
{
|
||||||
|
_logger.LogError("NullListException: The list of warehouse is null");
|
||||||
|
return WarehouseOperationResponse.NotFound("The list of warehouse is not initialized");
|
||||||
|
}
|
||||||
|
catch (StorageException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||||
|
return WarehouseOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||||
|
return WarehouseOperationResponse.InternalServerError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public WarehouseOperationResponse GetWarehouseByData(string data)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return WarehouseOperationResponse.OK(_mapper.Map<WarehouseViewModel>(_warehouseBusinessLogicContract.GetWarehouseByData(data)));
|
||||||
|
}
|
||||||
|
catch (ArgumentNullException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ArgumentNullException: Data is null or empty");
|
||||||
|
return WarehouseOperationResponse.BadRequest("Data is empty");
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ElementNotFoundException: warehouse not found");
|
||||||
|
return WarehouseOperationResponse.NotFound($"warehouse with data '{data}' not found");
|
||||||
|
}
|
||||||
|
catch (StorageException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||||
|
return WarehouseOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||||
|
return WarehouseOperationResponse.InternalServerError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public WarehouseOperationResponse InsertWarehouse(WarehouseBindingModel warehouseDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_warehouseBusinessLogicContract.InsertWarehouse(_mapper.Map<WarehouseDataModel>(warehouseDataModel));
|
||||||
|
return WarehouseOperationResponse.NoContent();
|
||||||
|
}
|
||||||
|
catch (ArgumentNullException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ArgumentNullException: warehouse data is null");
|
||||||
|
return WarehouseOperationResponse.BadRequest("furniture data is empty");
|
||||||
|
}
|
||||||
|
catch (ValidationException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ValidationException: Invalid warehouse data");
|
||||||
|
return WarehouseOperationResponse.BadRequest($"Invalid warehouse data: {ex.Message}");
|
||||||
|
}
|
||||||
|
catch (ElementExistsException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ElementExistsException: warehouse already exists");
|
||||||
|
return WarehouseOperationResponse.BadRequest(ex.Message);
|
||||||
|
}
|
||||||
|
catch (StorageException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||||
|
return WarehouseOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||||
|
return WarehouseOperationResponse.InternalServerError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public WarehouseOperationResponse UpdateWarehouse(WarehouseBindingModel warehouseDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_warehouseBusinessLogicContract.UpdateWarehouse(_mapper.Map<WarehouseDataModel>(warehouseDataModel));
|
||||||
|
return WarehouseOperationResponse.NoContent();
|
||||||
|
}
|
||||||
|
catch (ArgumentNullException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ArgumentNullException: warehouse data is null");
|
||||||
|
return WarehouseOperationResponse.BadRequest("warehouse data is empty");
|
||||||
|
}
|
||||||
|
catch (ValidationException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ValidationException: Invalid warehouse data");
|
||||||
|
return WarehouseOperationResponse.BadRequest($"Invalid warehouse data: {ex.Message}");
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ElementNotFoundException: warehouse not found");
|
||||||
|
return WarehouseOperationResponse.BadRequest($"warehouse with id '{warehouseDataModel.Id}' not found");
|
||||||
|
}
|
||||||
|
catch (ElementExistsException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ElementExistsException: warehouse already exists");
|
||||||
|
return WarehouseOperationResponse.BadRequest(ex.Message);
|
||||||
|
}
|
||||||
|
catch (StorageException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||||
|
return WarehouseOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||||
|
return WarehouseOperationResponse.InternalServerError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public WarehouseOperationResponse DeleteWarehouse(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_warehouseBusinessLogicContract.DeleteWarehouse(id);
|
||||||
|
return WarehouseOperationResponse.NoContent();
|
||||||
|
}
|
||||||
|
catch (ArgumentNullException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ArgumentNullException: Id is null or empty");
|
||||||
|
return WarehouseOperationResponse.BadRequest("Id is empty");
|
||||||
|
}
|
||||||
|
catch (ValidationException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ValidationException: Invalid id");
|
||||||
|
return WarehouseOperationResponse.BadRequest($"Invalid id: {ex.Message}");
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "ElementNotFoundException: warehouse not found");
|
||||||
|
return WarehouseOperationResponse.BadRequest($"furniture with id '{id}' not found");
|
||||||
|
}
|
||||||
|
catch (StorageException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||||
|
return WarehouseOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||||
|
return WarehouseOperationResponse.InternalServerError(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
using SquirrelContract.AdapterContracts;
|
||||||
|
using SquirrelContract.BindingModels;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace SquirrelWebApi.Controllers;
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[Route("api/[controller]/[action]")]
|
||||||
|
[ApiController]
|
||||||
|
[Produces("application/json")]
|
||||||
|
public class SuppliesController(ISupplyAdapter adapter) : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly ISupplyAdapter _adapter = adapter;
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult GetRecords(DateTime fromDate, DateTime toDate)
|
||||||
|
{
|
||||||
|
return _adapter.GetAllSuppliesByPeriod(fromDate, toDate).GetResponse(Request, Response);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult GetCocktailRecords(string id, DateTime fromDate, DateTime toDate)
|
||||||
|
{
|
||||||
|
return _adapter.GetCocktailList(id, fromDate, toDate).GetResponse(Request, Response);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{data}")]
|
||||||
|
public IActionResult GetComponentByData(string data)
|
||||||
|
{
|
||||||
|
return _adapter.GetSupplyByData(data).GetResponse(Request, Response);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public IActionResult Register([FromBody] SupplyBindingModel supplyBindingModel)
|
||||||
|
{
|
||||||
|
return _adapter.InsertSupply(supplyBindingModel).GetResponse(Request, Response);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut]
|
||||||
|
public IActionResult ChangeInfo([FromBody] SupplyBindingModel supplyBindingModel)
|
||||||
|
{
|
||||||
|
return _adapter.UpdateSupply(supplyBindingModel).GetResponse(Request, Response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
using SquirrelContract.AdapterContracts;
|
||||||
|
using SquirrelContract.BindingModels;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace SquirrelWebApi.Controllers;
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
[Produces("application/json")]
|
||||||
|
public class WarehousesController(IWarehouseAdapter adapter) : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly IWarehouseAdapter _adapter = adapter;
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult GetAll()
|
||||||
|
{
|
||||||
|
return _adapter.GetAllWarehouses().GetResponse(Request, Response);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{data}")]
|
||||||
|
public IActionResult GetComponentByData(string data)
|
||||||
|
{
|
||||||
|
return _adapter.GetWarehouseByData(data).GetResponse(Request, Response);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public IActionResult Register([FromBody] WarehouseBindingModel warehouseBindingModel)
|
||||||
|
{
|
||||||
|
return _adapter.InsertWarehouse(warehouseBindingModel).GetResponse(Request, Response);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut]
|
||||||
|
public IActionResult ChangeInfo([FromBody] WarehouseBindingModel warehouseBindingModel)
|
||||||
|
{
|
||||||
|
return _adapter.UpdateWarehouse(warehouseBindingModel).GetResponse(Request, Response);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
public IActionResult DeleteComponent(string id)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(id))
|
||||||
|
return BadRequest("Id is null or empty");
|
||||||
|
|
||||||
|
return _adapter.DeleteWarehouse(id).GetResponse(Request, Response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -57,6 +57,8 @@ builder.Services.AddTransient<ICocktailBusinessLogicContract, CocktailBusinessLo
|
|||||||
builder.Services.AddTransient<ISalaryBusinessLogicContract, SalaryBusinessLogicContract>();
|
builder.Services.AddTransient<ISalaryBusinessLogicContract, SalaryBusinessLogicContract>();
|
||||||
builder.Services.AddTransient<ISaleBusinessLogicContract, SaleBusinessLogicContract>();
|
builder.Services.AddTransient<ISaleBusinessLogicContract, SaleBusinessLogicContract>();
|
||||||
builder.Services.AddTransient<IEmployeeBusinessLogicContract, EmployeeBusinessLogicContract>();
|
builder.Services.AddTransient<IEmployeeBusinessLogicContract, EmployeeBusinessLogicContract>();
|
||||||
|
builder.Services.AddTransient<IWarehouseBusinessLogicContract, WarehouseBusinessLogicContract>();
|
||||||
|
builder.Services.AddTransient<ISupplyBusinessLogicContract, SupplyBusinessLogicContract>();
|
||||||
|
|
||||||
builder.Services.AddTransient<SquirrelDbContext>();
|
builder.Services.AddTransient<SquirrelDbContext>();
|
||||||
builder.Services.AddTransient<IClientStorageContract, ClientStorageContract>();
|
builder.Services.AddTransient<IClientStorageContract, ClientStorageContract>();
|
||||||
@@ -65,6 +67,8 @@ builder.Services.AddTransient<ICocktailStorageContract, CocktailStorageContract>
|
|||||||
builder.Services.AddTransient<ISalaryStorageContract, SalaryStorageContract>();
|
builder.Services.AddTransient<ISalaryStorageContract, SalaryStorageContract>();
|
||||||
builder.Services.AddTransient<ISaleStorageContract, SaleStorageContract>();
|
builder.Services.AddTransient<ISaleStorageContract, SaleStorageContract>();
|
||||||
builder.Services.AddTransient<IEmployeeStorageContract, EmployeeStorageContract>();
|
builder.Services.AddTransient<IEmployeeStorageContract, EmployeeStorageContract>();
|
||||||
|
builder.Services.AddTransient<IWarehouseStorageContract, WarehouseStorageContract>();
|
||||||
|
builder.Services.AddTransient<ISupplyStorageContract, SupplyStorageContract>();
|
||||||
|
|
||||||
builder.Services.AddTransient<IClientAdapter, ClientAdapter>();
|
builder.Services.AddTransient<IClientAdapter, ClientAdapter>();
|
||||||
builder.Services.AddTransient<IPostAdapter, PostAdapter>();
|
builder.Services.AddTransient<IPostAdapter, PostAdapter>();
|
||||||
@@ -72,6 +76,8 @@ builder.Services.AddTransient<ICocktailAdapter, CocktailAdapter>();
|
|||||||
builder.Services.AddTransient<ISaleAdapter, SaleAdapter>();
|
builder.Services.AddTransient<ISaleAdapter, SaleAdapter>();
|
||||||
builder.Services.AddTransient<IEmployeeAdapter, EmployeeAdapter>();
|
builder.Services.AddTransient<IEmployeeAdapter, EmployeeAdapter>();
|
||||||
builder.Services.AddTransient<ISalaryAdapter, SalaryAdapter>();
|
builder.Services.AddTransient<ISalaryAdapter, SalaryAdapter>();
|
||||||
|
builder.Services.AddTransient<IWarehouseAdapter, WarehouseAdapter>();
|
||||||
|
builder.Services.AddTransient<ISupplyAdapter, SupplyAdapter>();
|
||||||
|
|
||||||
builder.Services.AddOpenApi();
|
builder.Services.AddOpenApi();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user