Compare commits
21 Commits
lab05_Sala
...
lab05_Sala
| Author | SHA1 | Date | |
|---|---|---|---|
| 20fd6e399e | |||
| 3875eaa4e4 | |||
| a0e1b0d14d | |||
| 741fccb38a | |||
| 59aaa5cb04 | |||
| 47c8badaa0 | |||
| 2cbfda6122 | |||
| ac4aa4a146 | |||
| ea790d5d17 | |||
| ce473a3725 | |||
| 1475434fef | |||
| 0df46ec709 | |||
| 0a00160b2f | |||
| 39dada6bca | |||
| f7b2442b3e | |||
| 9e9cfe3adf | |||
| da93722d9f | |||
| 43dc07e662 | |||
| ddb84536c0 | |||
| ec2eea3184 | |||
| b7cb388d19 |
@@ -0,0 +1,74 @@
|
||||
using MagicCarpetContracts.BusinessLogicContracts;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.Extensions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetBusinessLogic.Implementations;
|
||||
|
||||
public class AgencyBusinessLogicContract(IAgencyStorageContract agencyStorageContract, ILogger logger) : IAgencyBusinessLogicContract
|
||||
{
|
||||
private readonly IAgencyStorageContract _agencyStorageContract = agencyStorageContract;
|
||||
private readonly ILogger _logger = logger;
|
||||
public List<AgencyDataModel> GetAllComponents()
|
||||
{
|
||||
_logger.LogInformation("GetAllComponents");
|
||||
return _agencyStorageContract.GetList() ?? throw new NullListException();
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public AgencyDataModel GetComponentByData(string data)
|
||||
{
|
||||
_logger.LogInformation("Get element by data: {data}", data);
|
||||
if (data.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
if (!data.IsGuid())
|
||||
{
|
||||
throw new ElementNotFoundException(data);
|
||||
}
|
||||
return _agencyStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||
|
||||
return new("", TourType.None, 0, []);
|
||||
}
|
||||
|
||||
public void InsertComponent(AgencyDataModel agencyDataModel)
|
||||
{
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(agencyDataModel));
|
||||
ArgumentNullException.ThrowIfNull(agencyDataModel);
|
||||
agencyDataModel.Validate();
|
||||
_agencyStorageContract.AddElement(agencyDataModel);
|
||||
}
|
||||
|
||||
public void UpdateComponent(AgencyDataModel agencyDataModel)
|
||||
{
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(agencyDataModel));
|
||||
ArgumentNullException.ThrowIfNull(agencyDataModel);
|
||||
agencyDataModel.Validate();
|
||||
_agencyStorageContract.UpdElement(agencyDataModel);
|
||||
}
|
||||
|
||||
public void DeleteComponent(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");
|
||||
}
|
||||
_agencyStorageContract.DelElement(id);
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,11 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetBusinessLogic.Implementations;
|
||||
|
||||
public class SaleBusinessLogicContract(ISaleStorageContract saleStorageContract, ILogger logger) : ISaleBusinessLogicContract
|
||||
public class SaleBusinessLogicContract(ISaleStorageContract saleStorageContract,IAgencyStorageContract agencyStorageContract, ILogger logger) : ISaleBusinessLogicContract
|
||||
{
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
|
||||
private readonly IAgencyStorageContract _agencyStorageContract = agencyStorageContract;
|
||||
|
||||
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate)
|
||||
{
|
||||
@@ -101,6 +102,10 @@ public class SaleBusinessLogicContract(ISaleStorageContract saleStorageContract,
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(saleDataModel));
|
||||
ArgumentNullException.ThrowIfNull(saleDataModel);
|
||||
saleDataModel.Validate();
|
||||
if (!_agencyStorageContract.CheckComponents(saleDataModel))
|
||||
{
|
||||
throw new InsufficientException("Dont have tour in agency");
|
||||
}
|
||||
_saleStorageContract.AddElement(saleDataModel);
|
||||
}
|
||||
|
||||
@@ -117,4 +122,35 @@ public class SaleBusinessLogicContract(ISaleStorageContract saleStorageContract,
|
||||
}
|
||||
_saleStorageContract.DelElement(id);
|
||||
}
|
||||
//третье требование
|
||||
public double CalculateSaleRevenue(DateTime fromDate, DateTime toDate)
|
||||
{
|
||||
_logger.LogInformation("CalculateSaleRevenue params: {fromDate}, {toDate}", fromDate, toDate);
|
||||
if (fromDate.IsDateNotOlder(toDate))
|
||||
{
|
||||
throw new IncorrectDatesException(fromDate, toDate);
|
||||
}
|
||||
var sales = GetAllSalesByPeriod(fromDate, toDate);
|
||||
if (sales == null || !sales.Any())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
double totalRevenue = 0;
|
||||
object lockObj = new object();
|
||||
|
||||
Parallel.ForEach(sales, sale =>
|
||||
{
|
||||
if (sale.IsCancel)
|
||||
return;
|
||||
|
||||
lock (lockObj)
|
||||
{
|
||||
totalRevenue += sale.Sum;
|
||||
}
|
||||
});
|
||||
|
||||
_logger.LogInformation("Total revenue calculated: {totalRevenue}", totalRevenue);
|
||||
return totalRevenue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
using MagicCarpetContracts.BusinessLogicContracts;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.Extensions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetBusinessLogic.Implementations;
|
||||
|
||||
public class SuppliesBusinessLogicContract(ISuppliesStorageContract suppliesStorageContract, ILogger logger) : ISuppliesBusinessLogicContract
|
||||
{
|
||||
private readonly ISuppliesStorageContract _suppliesStorageContract = suppliesStorageContract;
|
||||
private readonly ILogger _logger = logger;
|
||||
|
||||
public List<SuppliesDataModel> GetAllComponents()
|
||||
{
|
||||
_logger.LogInformation("GetAllComponents");
|
||||
return _suppliesStorageContract.GetList() ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public SuppliesDataModel GetComponentByData(string data)
|
||||
{
|
||||
_logger.LogInformation("Get element by data: {data}", data);
|
||||
if (data.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
if (!data.IsGuid())
|
||||
{
|
||||
throw new ElementNotFoundException(data);
|
||||
}
|
||||
return _suppliesStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
|
||||
public void InsertComponent(SuppliesDataModel suppliesDataModel)
|
||||
{
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(suppliesDataModel));
|
||||
ArgumentNullException.ThrowIfNull(suppliesDataModel);
|
||||
suppliesDataModel.Validate();
|
||||
_suppliesStorageContract.AddElement(suppliesDataModel);
|
||||
}
|
||||
|
||||
public void UpdateComponent(SuppliesDataModel suppliesDataModel)
|
||||
{
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(suppliesDataModel));
|
||||
ArgumentNullException.ThrowIfNull(suppliesDataModel);
|
||||
suppliesDataModel.Validate();
|
||||
_suppliesStorageContract.UpdElement(suppliesDataModel);
|
||||
}
|
||||
//первое требование
|
||||
public int CalculateTourSupplies(DateTime? startDate, DateTime? endDate)
|
||||
{
|
||||
var supplies = _suppliesStorageContract.GetList(startDate, endDate);
|
||||
if (supplies == null || supplies.Count == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int count = 0;
|
||||
var lockObject = new object();
|
||||
Parallel.ForEach(supplies, supply =>
|
||||
{
|
||||
int supplyCount = supply.Tours.Sum(component => component.Count);
|
||||
lock (lockObject)
|
||||
{
|
||||
count += supplyCount;
|
||||
}
|
||||
});
|
||||
return count;
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,6 @@ public class TourBusinessLogicContract(ITourStorageContract tourStorageContract,
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(tourDataModel));
|
||||
ArgumentNullException.ThrowIfNull(tourDataModel);
|
||||
tourDataModel.Validate();
|
||||
_tourStorageContract.AddElement(tourDataModel);
|
||||
}
|
||||
|
||||
public void UpdateTour(TourDataModel tourDataModel)
|
||||
@@ -64,7 +63,6 @@ public class TourBusinessLogicContract(ITourStorageContract tourStorageContract,
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(tourDataModel));
|
||||
ArgumentNullException.ThrowIfNull(tourDataModel);
|
||||
tourDataModel.Validate();
|
||||
_tourStorageContract.UpdElement(tourDataModel);
|
||||
}
|
||||
|
||||
public void DeleteTour(string id)
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using MagicCarpetContracts.AdapterContracts.OperationResponses;
|
||||
using MagicCarpetContracts.BindingModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.AdapterContracts;
|
||||
|
||||
public interface IAgencyAdapter
|
||||
{
|
||||
AgencyOperationResponse GetAllComponents();
|
||||
AgencyOperationResponse GetComponentByData(string data);
|
||||
AgencyOperationResponse InsertComponent(AgencyBindingModel warehouseDataModel);
|
||||
AgencyOperationResponse UpdateComponent(AgencyBindingModel warehouseDataModel);
|
||||
AgencyOperationResponse DeleteComponent(string id);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using MagicCarpetContracts.AdapterContracts.OperationResponses;
|
||||
using MagicCarpetContracts.BindingModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.AdapterContracts;
|
||||
|
||||
public interface ISuppliesAdapter
|
||||
{
|
||||
SuppliesOperationResponse GetAllComponents();
|
||||
SuppliesOperationResponse GetComponentByData(string data);
|
||||
SuppliesOperationResponse InsertComponent(SuppliesBindingModel componentDataModel);
|
||||
SuppliesOperationResponse UpdateComponent(SuppliesBindingModel componentDataModel);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using MagicCarpetContracts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.AdapterContracts.OperationResponses;
|
||||
|
||||
public class AgencyOperationResponse : OperationResponse
|
||||
{
|
||||
public static AgencyOperationResponse OK(List<AgencyViewModel> data) => OK<AgencyOperationResponse, List<AgencyViewModel>>(data);
|
||||
|
||||
public static AgencyOperationResponse OK(AgencyViewModel data) => OK<AgencyOperationResponse, AgencyViewModel>(data);
|
||||
|
||||
public static AgencyOperationResponse NoContent() => NoContent<AgencyOperationResponse>();
|
||||
|
||||
public static AgencyOperationResponse NotFound(string message) => NotFound<AgencyOperationResponse>(message);
|
||||
|
||||
public static AgencyOperationResponse BadRequest(string message) => BadRequest<AgencyOperationResponse>(message);
|
||||
|
||||
public static AgencyOperationResponse InternalServerError(string message) => InternalServerError<AgencyOperationResponse>(message);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using MagicCarpetContracts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.AdapterContracts.OperationResponses;
|
||||
|
||||
public class SuppliesOperationResponse : OperationResponse
|
||||
{
|
||||
public static SuppliesOperationResponse OK(List<SuppliesViewModel> data) => OK<SuppliesOperationResponse, List<SuppliesViewModel>>(data);
|
||||
|
||||
public static SuppliesOperationResponse OK(SuppliesViewModel data) => OK<SuppliesOperationResponse, SuppliesViewModel>(data);
|
||||
|
||||
public static SuppliesOperationResponse NoContent() => NoContent<SuppliesOperationResponse>();
|
||||
|
||||
public static SuppliesOperationResponse NotFound(string message) => NotFound<SuppliesOperationResponse>(message);
|
||||
|
||||
public static SuppliesOperationResponse BadRequest(string message) => BadRequest<SuppliesOperationResponse>(message);
|
||||
|
||||
public static SuppliesOperationResponse InternalServerError(string message) => InternalServerError<SuppliesOperationResponse>(message);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using MagicCarpetContracts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.BindingModels;
|
||||
|
||||
public class AgencyBindingModel
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public TourType? TourType { get; set; }
|
||||
public int Count { get; set; }
|
||||
public List<TourAgencyBindingModel>? Tours { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using MagicCarpetContracts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.BindingModels;
|
||||
|
||||
public class SuppliesBindingModel
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public TourType? TourType { get; set; }
|
||||
public DateTime ProductuionDate { get; set; }
|
||||
public int Count { get; set; }
|
||||
public List<TourSuppliesBindingModel>? Tours { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.BindingModels;
|
||||
|
||||
public class TourAgencyBindingModel
|
||||
{
|
||||
public string? AgencyId { get; set; }
|
||||
public string? TourId { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.BindingModels;
|
||||
|
||||
public class TourSuppliesBindingModel
|
||||
{
|
||||
public string? SuppliesId { get; set; }
|
||||
public string? TourId { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.BusinessLogicContracts;
|
||||
|
||||
public interface IAgencyBusinessLogicContract
|
||||
{
|
||||
List<AgencyDataModel> GetAllComponents();
|
||||
AgencyDataModel GetComponentByData(string data);
|
||||
void InsertComponent(AgencyDataModel agencyDataModel);
|
||||
void UpdateComponent(AgencyDataModel agencyDataModel);
|
||||
void DeleteComponent(string id);
|
||||
}
|
||||
@@ -22,4 +22,6 @@ public interface ISaleBusinessLogicContract
|
||||
void InsertSale(SaleDataModel saleDataModel);
|
||||
|
||||
void CancelSale(string id);
|
||||
|
||||
public double CalculateSaleRevenue(DateTime fromDate, DateTime toDate);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.BusinessLogicContracts;
|
||||
|
||||
public interface ISuppliesBusinessLogicContract
|
||||
{
|
||||
List<SuppliesDataModel> GetAllComponents();
|
||||
SuppliesDataModel GetComponentByData(string data);
|
||||
void InsertComponent(SuppliesDataModel componentDataModel);
|
||||
void UpdateComponent(SuppliesDataModel componentDataModel);
|
||||
int CalculateTourSupplies(DateTime? startDate, DateTime? endDate);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.Extensions;
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.DataModels;
|
||||
|
||||
public class AgencyDataModel(string id, TourType tourType, int count, List<TourAgencyDataModel> tours) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public TourType TourType { get; private set; } = tourType;
|
||||
public int Count { get; private set; } = count;
|
||||
public List<TourAgencyDataModel> Tours { get; private set; } = tours;
|
||||
|
||||
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 (TourType == TourType.None)
|
||||
throw new ValidationException("Field Type is empty");
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
if ((Tours?.Count ?? 0) == 0)
|
||||
throw new ValidationException("The sale must include tours");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.Extensions;
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace MagicCarpetContracts.DataModels;
|
||||
|
||||
public class SuppliesDataModel(string id, TourType tourType, DateTime productuionDate, int count, List<TourSuppliesDataModel> tours) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public TourType TourType { get; private set; } = tourType;
|
||||
public DateTime ProductuionDate { get; private set; } = productuionDate;
|
||||
public int Count { get; private set; } = count;
|
||||
public List<TourSuppliesDataModel> Tours { get; private set; } = tours;
|
||||
|
||||
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 (TourType == TourType.None)
|
||||
throw new ValidationException("Field Type is empty");
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
if ((Tours?.Count ?? 0) == 0)
|
||||
throw new ValidationException("The sale must include tours");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.Extensions;
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.DataModels;
|
||||
|
||||
public class TourAgencyDataModel(string agencyId, string tourId, int count) : IValidation
|
||||
{
|
||||
public string AgencyId { get; private set; } = agencyId;
|
||||
public string TourId { get; private set; } = tourId;
|
||||
public int Count { get; private set; } = count;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (AgencyId.IsEmpty())
|
||||
throw new ValidationException("Field AgencyId is empty");
|
||||
if (!AgencyId.IsGuid())
|
||||
throw new ValidationException("The value in the field AgencyId is not a unique identifier");
|
||||
if (TourId.IsEmpty())
|
||||
throw new ValidationException("Field TourId is empty");
|
||||
if (!TourId.IsGuid())
|
||||
throw new ValidationException("The value in the field TourId is not a unique identifier");
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.Extensions;
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.DataModels;
|
||||
|
||||
public class TourSuppliesDataModel(string suppliesId, string tourId, int count) : IValidation
|
||||
{
|
||||
public string SuppliesId { get; private set; } = suppliesId;
|
||||
public string TourId { get; private set; } = tourId;
|
||||
public int Count { get; private set; } = count;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (SuppliesId.IsEmpty())
|
||||
throw new ValidationException("Field SuppliesId is empty");
|
||||
if (!SuppliesId.IsGuid())
|
||||
throw new ValidationException("The value in the field SuppliesId is not a unique identifier");
|
||||
if (TourId.IsEmpty())
|
||||
throw new ValidationException("Field TourId is empty");
|
||||
if (!TourId.IsGuid())
|
||||
throw new ValidationException("The value in the field BlandId is not a unique identifier");
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.Exceptions;
|
||||
|
||||
public class InsufficientException(string message) : Exception(message)
|
||||
{
|
||||
}
|
||||
@@ -11,9 +11,9 @@ namespace MagicCarpetContracts.Infrastructure;
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.StoragesContracts;
|
||||
|
||||
public interface IAgencyStorageContract
|
||||
{
|
||||
List<AgencyDataModel> GetList();
|
||||
AgencyDataModel GetElementById(string id);
|
||||
void AddElement(AgencyDataModel agencyDataModel);
|
||||
void UpdElement(AgencyDataModel agencyDataModel);
|
||||
void DelElement(string id);
|
||||
bool CheckComponents(SaleDataModel saleDataModel);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.StoragesContracts;
|
||||
|
||||
public interface ISuppliesStorageContract
|
||||
{
|
||||
List<SuppliesDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null);
|
||||
SuppliesDataModel GetElementById(string id);
|
||||
void AddElement(SuppliesDataModel suppliesDataModel);
|
||||
void UpdElement(SuppliesDataModel suppliesDataModel);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using MagicCarpetContracts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.ViewModels;
|
||||
|
||||
public class AgencyViewModel
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required TourType TourType { get; set; }
|
||||
public required int Count { get; set; }
|
||||
public required List<TourAgencyViewModel> Tours { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using MagicCarpetContracts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.ViewModels;
|
||||
|
||||
public class SuppliesViewModel
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required TourType TourType { get; set; }
|
||||
public DateTime ProductuionDate { get; set; }
|
||||
public int Count { get; set; }
|
||||
public required List<TourSuppliesViewModel> Tours { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.ViewModels;
|
||||
|
||||
public class TourAgencyViewModel
|
||||
{
|
||||
public required string AgencyId { get; set; }
|
||||
public required string TourId { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.ViewModels;
|
||||
|
||||
public class TourSuppliesViewModel
|
||||
{
|
||||
public required string SuppliesId { get; set; }
|
||||
public required string TourId { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Implementations;
|
||||
|
||||
public class AgencyStorageContract : IAgencyStorageContract
|
||||
{
|
||||
private readonly MagicCarpetDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public AgencyStorageContract(MagicCarpetDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.AddMaps(typeof(TourAgency));
|
||||
cfg.AddMaps(typeof(Agency));
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<AgencyDataModel> GetList()
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.Agencies.Select(x => _mapper.Map<AgencyDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public AgencyDataModel GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<AgencyDataModel>(GetAgencyById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(AgencyDataModel agencyDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Agencies.Add(_mapper.Map<Agency>(agencyDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdElement(AgencyDataModel agencyDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetAgencyById(agencyDataModel.Id) ?? throw new ElementNotFoundException(agencyDataModel.Id);
|
||||
_dbContext.Agencies.Update(_mapper.Map(agencyDataModel, 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 = GetAgencyById(id) ?? throw new ElementNotFoundException(id);
|
||||
_dbContext.Agencies.Remove(element);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
//второе требование
|
||||
public bool CheckComponents(SaleDataModel saleDataModel)
|
||||
{
|
||||
using (var transaction = _dbContext.Database.BeginTransaction())
|
||||
{
|
||||
var componentTasks = saleDataModel.Tours
|
||||
.AsParallel()
|
||||
.Select(async saleComponent =>
|
||||
{
|
||||
var component = await _dbContext.Tours.FirstOrDefaultAsync(x => x.Id == saleComponent.TourId);
|
||||
var agency = await _dbContext.Agencies.FirstOrDefaultAsync(x => x.TourType == component.TourType && x.Count >= saleComponent.Count);
|
||||
if (agency == null)
|
||||
{
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
if (agency.Count - saleComponent.Count == 0)
|
||||
{
|
||||
DelElement(agency.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
agency.Count -= saleComponent.Count;
|
||||
_dbContext.Agencies.Update(agency);
|
||||
}
|
||||
|
||||
return true;
|
||||
})
|
||||
.ToList();
|
||||
|
||||
Task.WaitAll(componentTasks.ToArray());
|
||||
transaction.Commit();
|
||||
_dbContext.SaveChanges();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private Agency? GetAgencyById(string id) => _dbContext.Agencies.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Implementations;
|
||||
|
||||
public class SuppliesStorageContract : ISuppliesStorageContract
|
||||
{
|
||||
private readonly MagicCarpetDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public SuppliesStorageContract(MagicCarpetDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Supplies, SuppliesDataModel>()
|
||||
.ConstructUsing(src => new SuppliesDataModel(
|
||||
src.Id,
|
||||
src.Type,
|
||||
src.ProductuionDate,
|
||||
src.Count,
|
||||
_mapper.Map<List<TourSuppliesDataModel>>(src.Tours)
|
||||
));
|
||||
|
||||
cfg.CreateMap<SuppliesDataModel, Supplies>();
|
||||
cfg.CreateMap<TourSuppliesDataModel, TourSupplies>().ReverseMap();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<SuppliesDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Supplieses.AsQueryable();
|
||||
if (startDate is not null && endDate is not null)
|
||||
{
|
||||
query = query.Where(x => x.ProductuionDate >= startDate && x.ProductuionDate < endDate);
|
||||
}
|
||||
return [.. query.Select(x => _mapper.Map<SuppliesDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public SuppliesDataModel GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<SuppliesDataModel>(GetSuppliesById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(SuppliesDataModel suppliesDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Supplieses.Add(_mapper.Map<Supplies>(suppliesDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdElement(SuppliesDataModel suppliesDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetSuppliesById(suppliesDataModel.Id) ?? throw new ElementNotFoundException(suppliesDataModel.Id);
|
||||
_dbContext.Supplieses.Update(_mapper.Map(suppliesDataModel, element));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Supplies? GetSuppliesById(string id) => _dbContext.Supplieses.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -25,7 +26,7 @@ public class TourStorageContract : ITourStorageContract
|
||||
{
|
||||
cfg.CreateMap<Tour, TourDataModel>();
|
||||
cfg.CreateMap<TourDataModel, Tour>();
|
||||
cfg.CreateMap<TourHistory, TourHistoryDataModel>();
|
||||
cfg.CreateMap<TourHistory, TourHistoryDataModel>(); ;
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
@@ -47,6 +47,10 @@ public class MagicCarpetDbContext(IConfigurationDatabase configurationDatabase)
|
||||
|
||||
modelBuilder.Entity<SaleTour>().HasKey(x => new { x.SaleId, x.TourId });
|
||||
|
||||
modelBuilder.Entity<TourSupplies>().HasKey(x => new { x.SuppliesId, x.TourId });
|
||||
|
||||
modelBuilder.Entity<TourAgency>().HasKey(x => new { x.AgencyId, x.TourId });
|
||||
|
||||
modelBuilder.Entity<Post>()
|
||||
.Property(x => x.Configuration)
|
||||
.HasColumnType("jsonb")
|
||||
@@ -70,6 +74,10 @@ public class MagicCarpetDbContext(IConfigurationDatabase configurationDatabase)
|
||||
public DbSet<SaleTour> SaleTours { get; set; }
|
||||
|
||||
public DbSet<Employee> Employees { get; set; }
|
||||
public DbSet<Supplies> Supplieses { get; set; }
|
||||
public DbSet<Agency> Agencies { get; set; }
|
||||
public DbSet<TourSupplies> TourSupplieses { get; set; }
|
||||
public DbSet<TourAgency> TourAgensies { get; set; }
|
||||
|
||||
private static string SerializePostConfiguration(PostConfiguration postConfiguration) => JsonConvert.SerializeObject(postConfiguration);
|
||||
private static PostConfiguration DeserialzePostConfiguration(string jsonString) => JToken.Parse(jsonString).Value<string>("Type") switch
|
||||
|
||||
22
MagicCarpetProject/MagicCarpetDatabase/Models/Agency.cs
Normal file
22
MagicCarpetProject/MagicCarpetDatabase/Models/Agency.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
[AutoMap(typeof(AgencyDataModel), ReverseMap = true)]
|
||||
public class Agency
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required TourType TourType { get; set; }
|
||||
public required int Count { get; set; }
|
||||
|
||||
[ForeignKey("AgencyId")]
|
||||
public List<TourAgency>? Tours { get; set; }
|
||||
}
|
||||
22
MagicCarpetProject/MagicCarpetDatabase/Models/Supplies.cs
Normal file
22
MagicCarpetProject/MagicCarpetDatabase/Models/Supplies.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
public class Supplies
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required TourType Type { get; set; }
|
||||
public DateTime ProductuionDate { get; set; }
|
||||
public required int Count { get; set; }
|
||||
|
||||
[ForeignKey("SuppliesId")]
|
||||
public List<TourSupplies>? Tours { get; set; }
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using MagicCarpetContracts.Enums;
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
@@ -9,6 +11,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
[AutoMap(typeof(TourDataModel), ReverseMap = true)]
|
||||
public class Tour
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
@@ -20,4 +23,8 @@ public class Tour
|
||||
public List<SaleTour>? SaleTours { get; set; }
|
||||
[ForeignKey("TourId")]
|
||||
public List<TourHistory>? TourHistories { get; set; }
|
||||
[ForeignKey("SuppliesesId")]
|
||||
public List<TourSupplies>? TourSupplies { get; set; }
|
||||
[ForeignKey("AgenciesId")]
|
||||
public List<TourAgency>? TourAgency { get; set; }
|
||||
}
|
||||
|
||||
19
MagicCarpetProject/MagicCarpetDatabase/Models/TourAgency.cs
Normal file
19
MagicCarpetProject/MagicCarpetDatabase/Models/TourAgency.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
[AutoMap(typeof(TourAgencyDataModel), ReverseMap = true)]
|
||||
public class TourAgency
|
||||
{
|
||||
public required string AgencyId { get; set; }
|
||||
public required string TourId { get; set; }
|
||||
public int Count { get; set; }
|
||||
public Agency? Agencies { get; set; }
|
||||
public Tour? Tours { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
[AutoMap(typeof(TourSuppliesDataModel), ReverseMap = true)]
|
||||
public class TourSupplies
|
||||
{
|
||||
public required string SuppliesId { get; set; }
|
||||
public required string TourId { get; set; }
|
||||
public int Count { get; set; }
|
||||
public Supplies? Supplies { get; set; }
|
||||
public Tour? Tours { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
using MagicCarpetBusinessLogic.Implementations;
|
||||
using MagicCarpetContracts.BusinessLogicContracts;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.BusinessLogicContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class AgencyBusinessLogicContractTests
|
||||
{
|
||||
private IAgencyBusinessLogicContract _warehouseBusinessLogicContract;
|
||||
private Mock<IAgencyStorageContract> _warehouseStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_warehouseStorageContract = new Mock<IAgencyStorageContract>();
|
||||
_warehouseBusinessLogicContract = new AgencyBusinessLogicContract(_warehouseStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_warehouseStorageContract.Reset();
|
||||
}
|
||||
|
||||
public void GetAllSupplies_ReturnListOfRecords_Test()
|
||||
{
|
||||
// Arrange
|
||||
var listOriginal = new List<AgencyDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), TourType.Beach, 1, []),
|
||||
new(Guid.NewGuid().ToString(), TourType.Beach, 1, []),
|
||||
new(Guid.NewGuid().ToString(), TourType.Beach, 1, []),
|
||||
};
|
||||
_warehouseStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
|
||||
// Act
|
||||
var list = _warehouseBusinessLogicContract.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_ReturnEmptyList_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetList()).Returns([]);
|
||||
// Act
|
||||
var list = _warehouseBusinessLogicContract.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_warehouseStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetList()).Returns((List<AgencyDataModel>)null);
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetAllComponents(), Throws.TypeOf<NullListException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetAllComponents(), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new AgencyDataModel(id, TourType.Beach, 1, []);
|
||||
_warehouseStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
// Act
|
||||
var element = _warehouseBusinessLogicContract.GetComponentByData(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 GetComponentsByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_CorrectRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var record = new AgencyDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_warehouseStorageContract.Setup(x => x.AddElement(It.IsAny<AgencyDataModel>()));
|
||||
// Act
|
||||
_warehouseBusinessLogicContract.InsertComponent(record);
|
||||
// Assert
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.AddElement(It.IsAny<AgencyDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_NullRecord_ThrowException_Test()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponents_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(new AgencyDataModel("id", TourType.Beach, 1, [])), Throws.TypeOf<ValidationException>());
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.AddElement(It.IsAny<AgencyDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_CorrectRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var record = new AgencyDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<AgencyDataModel>()));
|
||||
// Act
|
||||
_warehouseBusinessLogicContract.UpdateComponent(record);
|
||||
// Assert
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<AgencyDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementNotFoundException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<AgencyDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_NullRecord_ThrowException_Test()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponents_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new AgencyDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1, [])), Throws.TypeOf<ValidationException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<AgencyDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_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.DeleteComponent(id);
|
||||
//Assert
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once); Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_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.DeleteComponent(Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent("id"),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_warehouseStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -18,18 +18,22 @@ internal class SaleBusinessLogicContractTests
|
||||
{
|
||||
private SaleBusinessLogicContract _saleBusinessLogicContract;
|
||||
private Mock<ISaleStorageContract> _saleStorageContract;
|
||||
private Mock<IAgencyStorageContract> _agencyStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_saleStorageContract = new Mock<ISaleStorageContract>();
|
||||
_saleBusinessLogicContract = new SaleBusinessLogicContract(_saleStorageContract.Object, new Mock<ILogger>().Object);
|
||||
_agencyStorageContract = new Mock<IAgencyStorageContract>();
|
||||
_saleBusinessLogicContract = new SaleBusinessLogicContract(_saleStorageContract.Object,
|
||||
_agencyStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_saleStorageContract.Reset();
|
||||
_agencyStorageContract.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -396,6 +400,7 @@ internal class SaleBusinessLogicContractTests
|
||||
var flag = false;
|
||||
var record = new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DiscountType.None,
|
||||
false, [new SaleTourDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)]);
|
||||
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<SaleDataModel>())).Returns(true);
|
||||
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>()))
|
||||
.Callback((SaleDataModel x) =>
|
||||
{
|
||||
@@ -409,6 +414,7 @@ internal class SaleBusinessLogicContractTests
|
||||
//Act
|
||||
_saleBusinessLogicContract.InsertSale(record);
|
||||
//Assert
|
||||
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
@@ -417,11 +423,13 @@ internal class SaleBusinessLogicContractTests
|
||||
public void InsertSale_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<SaleDataModel>())).Returns(true);
|
||||
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() => _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), DiscountType.None, false, [new SaleTourDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])), Throws.TypeOf<ElementExistsException>());
|
||||
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -444,13 +452,26 @@ internal class SaleBusinessLogicContractTests
|
||||
public void InsertSale_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<SaleDataModel>())).Returns(true);
|
||||
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), DiscountType.None, false, [new SaleTourDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])), Throws.TypeOf<StorageException>());
|
||||
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSale_InsufficientError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<SaleDataModel>())).Returns(false);
|
||||
Assert.That(() => _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), DiscountType.None, false, [new SaleTourDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])), Throws.TypeOf<InsufficientException>());
|
||||
//Act&Assert
|
||||
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancelSale_CorrectRecord_Test()
|
||||
{
|
||||
@@ -502,4 +523,37 @@ internal class SaleBusinessLogicContractTests
|
||||
Assert.That(() => _saleBusinessLogicContract.CancelSale(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CalculateSaleRevenue_ReturnsCorrectSum_Test()
|
||||
{
|
||||
// Arrange
|
||||
var fromDate = DateTime.Now.AddDays(-10);
|
||||
var toDate = DateTime.Now;
|
||||
var listOriginal = new List<SaleDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DiscountType.None, false,
|
||||
[new SaleTourDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DiscountType.None, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DiscountType.None, false, []),
|
||||
};
|
||||
_saleStorageContract.Setup(x => x.GetList(fromDate, toDate, It.IsAny<string>(), It.IsAny<string>(),It.IsAny<string>())).Returns(listOriginal);
|
||||
// Act
|
||||
var result = _saleBusinessLogicContract.CalculateSaleRevenue(fromDate, toDate);
|
||||
// Assert
|
||||
Assert.That(result, Is.EqualTo(6.0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CalculateSaleRevenue_NoSales_ReturnsZero()
|
||||
{
|
||||
// Arrange
|
||||
var fromDate = DateTime.Now.AddDays(-10);
|
||||
var toDate = DateTime.Now;
|
||||
_saleStorageContract.Setup(x => x.GetList(fromDate, toDate, null, null, null)).Returns([]);
|
||||
// Act
|
||||
double revenue = _saleBusinessLogicContract.CalculateSaleRevenue(fromDate, toDate);
|
||||
// Assert
|
||||
Assert.That(revenue, Is.EqualTo(0));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,286 @@
|
||||
using MagicCarpetBusinessLogic.Implementations;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.BusinessLogicContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SuppliesBusinessLogicContractTests
|
||||
{
|
||||
private SuppliesBusinessLogicContract _suppliesBusinessLogicContract;
|
||||
private Mock<ISuppliesStorageContract> _suppliesStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_suppliesStorageContract = new Mock<ISuppliesStorageContract>();
|
||||
_suppliesBusinessLogicContract = new SuppliesBusinessLogicContract(_suppliesStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_suppliesStorageContract.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_ReturnListOfRecords_Test()
|
||||
{
|
||||
// Arrange
|
||||
var listOriginal = new List<SuppliesDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []),
|
||||
new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []),
|
||||
new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []),
|
||||
};
|
||||
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||
// Act
|
||||
var list = _suppliesBusinessLogicContract.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_ReturnEmptyList_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
|
||||
// Act
|
||||
var list = _suppliesBusinessLogicContract.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_suppliesStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns((List<SuppliesDataModel>)null);
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.GetAllComponents(), Throws.TypeOf<NullListException>());
|
||||
_suppliesStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.GetAllComponents(), Throws.TypeOf<StorageException>());
|
||||
_suppliesStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new SuppliesDataModel(id,TourType.Beach, DateTime.Now, 1, []);
|
||||
_suppliesStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
// Act
|
||||
var element = _suppliesBusinessLogicContract.GetComponentByData(id);
|
||||
// Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetComponentsByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.GetComponentByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _suppliesBusinessLogicContract.GetComponentByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_CorrectRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var record = new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_suppliesStorageContract.Setup(x => x.AddElement(It.IsAny<SuppliesDataModel>()));
|
||||
// Act
|
||||
_suppliesBusinessLogicContract.InsertComponent(record);
|
||||
// Assert
|
||||
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.AddElement(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_NullRecord_ThrowException_Test()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.InsertComponent(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponents_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.InsertComponent(new SuppliesDataModel("id", TourType.Beach, DateTime.UtcNow, 1, [])), Throws.TypeOf<ValidationException>());
|
||||
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.AddElement(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_CorrectRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var record = new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>()));
|
||||
// Act
|
||||
_suppliesBusinessLogicContract.UpdateComponent(record);
|
||||
// Assert
|
||||
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementNotFoundException>());
|
||||
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_NullRecord_ThrowException_Test()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponents_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, [])), Throws.TypeOf<ValidationException>());
|
||||
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CalculateTour_ReturnInt_Test()
|
||||
{
|
||||
// Arrange
|
||||
string id = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<SuppliesDataModel>()
|
||||
{
|
||||
new(id, TourType.Beach, DateTime.UtcNow, 1, new List<TourSuppliesDataModel> { new TourSuppliesDataModel(id, Guid.NewGuid().ToString(), 5), new TourSuppliesDataModel(id, Guid.NewGuid().ToString(), 6) })
|
||||
};
|
||||
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||
// Act
|
||||
var count = _suppliesBusinessLogicContract.CalculateTourSupplies(DateTime.UtcNow.AddDays(-5), DateTime.UtcNow.AddDays(5));
|
||||
// Assert
|
||||
Assert.That(count, Is.EqualTo(11));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CalculateTour_ReturnIntZero_Test()
|
||||
{
|
||||
// Arrange
|
||||
string id1 = Guid.NewGuid().ToString();
|
||||
string id2 = Guid.NewGuid().ToString();
|
||||
string id3 = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<SuppliesDataModel>()
|
||||
{
|
||||
new(id1, TourType.Beach, DateTime.UtcNow, 1, [])
|
||||
};
|
||||
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||
// Act
|
||||
var count = _suppliesBusinessLogicContract.CalculateTourSupplies(DateTime.UtcNow.AddDays(-5), DateTime.UtcNow.AddDays(5));
|
||||
// Assert
|
||||
Assert.That(count, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CalculateTour_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.CalculateTourSupplies(DateTime.UtcNow.AddDays(-5), DateTime.UtcNow.AddDays(5)), Throws.TypeOf<StorageException>());
|
||||
_suppliesStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetTests.DataModelTests;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using System;
|
||||
@@ -154,7 +155,7 @@ internal class TourBusinessLogicContractTests
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new TourDataModel(id, "name","country", 10, TourType.Ski);
|
||||
var record = new TourDataModel(id, "name", "country", 10, TourType.Ski);
|
||||
_tourStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
//Act
|
||||
var element = _tourBusinessLogicContract.GetTourByData(id);
|
||||
@@ -183,7 +184,7 @@ internal class TourBusinessLogicContractTests
|
||||
{
|
||||
//Arrange
|
||||
var country = "country";
|
||||
var record = new TourDataModel(Guid.NewGuid().ToString(), "name", country, 10, TourType.Ski);
|
||||
var record = new TourDataModel(Guid.NewGuid().ToString(), "name", country, 10, TourType.Ski);
|
||||
_tourStorageContract.Setup(x => x.GetElementByName(country)).Returns(record);
|
||||
//Act
|
||||
var element = _tourBusinessLogicContract.GetTourByData(country);
|
||||
@@ -248,7 +249,7 @@ internal class TourBusinessLogicContractTests
|
||||
_tourStorageContract.Setup(x => x.AddElement(It.IsAny<TourDataModel>()))
|
||||
.Callback((TourDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.TourName == record.TourName && x.TourCountry == record.TourCountry
|
||||
flag = x.Id == record.Id && x.TourName == record.TourName && x.TourCountry == record.TourCountry
|
||||
&& x.Price == record.Price && x.TourType == record.TourType;
|
||||
});
|
||||
//Act
|
||||
@@ -288,10 +289,18 @@ internal class TourBusinessLogicContractTests
|
||||
public void InsertTour_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_tourStorageContract.Setup(x => x.AddElement(It.IsAny<TourDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
Assert.That(() => _tourBusinessLogicContract.InsertTour(new TourDataModel(Guid.NewGuid().ToString(), "name","country", 10, TourType.Ski)), Throws.TypeOf<InsufficientException>());
|
||||
//Act&Assert
|
||||
Assert.That(() => _tourBusinessLogicContract.InsertTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski)), Throws.TypeOf<StorageException>());
|
||||
_tourStorageContract.Verify(x => x.AddElement(It.IsAny<TourDataModel>()), Times.Once);
|
||||
_tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertTour_InsufficientError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
Assert.That(() => _tourBusinessLogicContract.InsertTour(new TourDataModel(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski)),Throws.TypeOf<InsufficientException>());
|
||||
//Act&Assert
|
||||
_tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -329,7 +338,7 @@ internal class TourBusinessLogicContractTests
|
||||
//Arrange
|
||||
_tourStorageContract.Setup(x => x.UpdElement(It.IsAny<TourDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() => _tourBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski)), Throws.TypeOf<ElementExistsException>());
|
||||
Assert.That(() => _tourBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski)), Throws.TypeOf <ElementExistsException>());
|
||||
_tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class AgencyDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(null, TourType.Beach, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
model = CreateDataModel(string.Empty, TourType.Beach, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel("id", TourType.Beach, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TypeIsNoneTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), TourType.None, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), TourType.None, 0, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), TourType.None, -1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
[Test]
|
||||
public void ToursIsNullOrEmptyTest()
|
||||
{
|
||||
var sale = CreateDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1, null);
|
||||
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
sale = CreateDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1, []);
|
||||
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var type = TourType.Beach;
|
||||
var count = 1;
|
||||
var tours = CreateSubDataModel();
|
||||
var model = CreateDataModel(id, type, count, tours);
|
||||
Assert.DoesNotThrow(() => model.Validate());
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(model.Id, Is.EqualTo(id));
|
||||
Assert.That(model.TourType, Is.EqualTo(type));
|
||||
Assert.That(model.Count, Is.EqualTo(count));
|
||||
Assert.That(model.Tours, Is.EqualTo(tours));
|
||||
});
|
||||
}
|
||||
|
||||
private static AgencyDataModel CreateDataModel(string? id, TourType type, int count, List<TourAgencyDataModel> tours)
|
||||
=> new AgencyDataModel(id, type, count, tours);
|
||||
private static List<TourAgencyDataModel> CreateSubDataModel()
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SuppliesDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(null, TourType.Beach, DateTime.Now, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
model = CreateDataModel(string.Empty, TourType.Beach, DateTime.Now, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel("id", TourType.Beach, DateTime.Now, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TypeIsNoneTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), TourType.None, DateTime.Now, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), TourType.None, DateTime.Now, 0, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), TourType.None, DateTime.Now, -1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
[Test]
|
||||
public void ToursIsNullOrEmptyTest()
|
||||
{
|
||||
var sale = CreateDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, null);
|
||||
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
|
||||
sale = CreateDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []);
|
||||
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var type = TourType.Beach;
|
||||
var date = DateTime.Now;
|
||||
var count = 1;
|
||||
var tours = CreateSubDataModel();
|
||||
var model = CreateDataModel(id, type, date, count, tours);
|
||||
Assert.DoesNotThrow(() => model.Validate());
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(model.Id, Is.EqualTo(id));
|
||||
Assert.That(model.TourType, Is.EqualTo(type));
|
||||
Assert.That(model.ProductuionDate, Is.EqualTo(date));
|
||||
Assert.That(model.Count, Is.EqualTo(count));
|
||||
Assert.That(model.Tours, Is.EqualTo(tours));
|
||||
});
|
||||
}
|
||||
|
||||
private static SuppliesDataModel CreateDataModel(string? id, TourType type, DateTime date, int count, List<TourSuppliesDataModel> tours)
|
||||
=> new(id, type, date, count, tours);
|
||||
private static List<TourSuppliesDataModel> CreateSubDataModel()
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class TourAgencyDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void AgencyIdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(null, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AgencyIdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel("id", Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TourIdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), null, 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TourIdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), "id", 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var agencyId = Guid.NewGuid().ToString();
|
||||
var tourId = Guid.NewGuid().ToString();
|
||||
var count = 1;
|
||||
var model = CreateDataModel(agencyId, tourId, count);
|
||||
Assert.That(() => model.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(model.AgencyId, Is.EqualTo(agencyId));
|
||||
Assert.That(model.TourId, Is.EqualTo(tourId));
|
||||
Assert.That(model.Count, Is.EqualTo(count));
|
||||
});
|
||||
}
|
||||
|
||||
private static TourAgencyDataModel CreateDataModel(string? agencyId, string? tourId, int count)
|
||||
=> new(agencyId, tourId, count);
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -14,17 +15,17 @@ internal class TourDataModelTests
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var cocktail = CreateDataModel(null, "name", "country", 10.5, TourType.Beach);
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
cocktail = CreateDataModel(string.Empty, "name", "country", 10.5, TourType.Beach);
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
var tour = CreateDataModel(null, "name", "country", 10.5, TourType.Beach);
|
||||
Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
|
||||
tour = CreateDataModel(string.Empty, "name", "country", 10.5, TourType.Beach);
|
||||
Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var cocktail = CreateDataModel("id", "name", "country", 10.5, TourType.Beach);
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
var tour = CreateDataModel("id", "name", "country", 10.5, TourType.Beach);
|
||||
Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -46,17 +47,17 @@ internal class TourDataModelTests
|
||||
[Test]
|
||||
public void PriceIsLessOrZeroTest()
|
||||
{
|
||||
var cocktail = CreateDataModel(Guid.NewGuid().ToString(), null, null, 0, TourType.Beach);
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
cocktail = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, string.Empty, -10.5, TourType.Beach);
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
var tour = CreateDataModel(Guid.NewGuid().ToString(), null, null, 0, TourType.Beach);
|
||||
Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
|
||||
tour = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, string.Empty, -10.5, TourType.Beach);
|
||||
Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TourTypeIsNoneTest()
|
||||
{
|
||||
var cocktail = CreateDataModel(Guid.NewGuid().ToString(), null, null, 0, TourType.Beach);
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
var tour = CreateDataModel(Guid.NewGuid().ToString(), null, null, 0, TourType.Beach);
|
||||
Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -67,6 +68,8 @@ internal class TourDataModelTests
|
||||
var tourCountry = "country";
|
||||
var price = 10.5;
|
||||
var tourType = TourType.Ski;
|
||||
var supplies = CreateSuppliesDataModel();
|
||||
var agency = CreateAgencyDataModel();
|
||||
var tour = CreateDataModel(tourId, tourName, tourCountry, price, tourType);
|
||||
Assert.That(() => tour.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
@@ -79,6 +82,11 @@ internal class TourDataModelTests
|
||||
});
|
||||
}
|
||||
|
||||
private static TourDataModel CreateDataModel(string? id, string? tourName, string? countryName,double price, TourType tourType) =>
|
||||
private static TourDataModel CreateDataModel(string? id, string? tourName, string? countryName, double price, TourType tourType) =>
|
||||
new(id, tourName, countryName,price, tourType);
|
||||
private static List<TourSuppliesDataModel> CreateSuppliesDataModel()
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||
|
||||
private static List<TourAgencyDataModel> CreateAgencyDataModel()
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class TourSuppliesDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void SuppliesIdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(null, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SuppliesIdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel("id", Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TourIdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), null, 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TourIdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), "id", 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var suppliesId = Guid.NewGuid().ToString();
|
||||
var tourId = Guid.NewGuid().ToString();
|
||||
var count = 1;
|
||||
var model = CreateDataModel(suppliesId, tourId, count);
|
||||
Assert.That(() => model.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(model.SuppliesId, Is.EqualTo(suppliesId));
|
||||
Assert.That(model.TourId, Is.EqualTo(tourId));
|
||||
Assert.That(model.Count, Is.EqualTo(count));
|
||||
});
|
||||
}
|
||||
|
||||
private static TourSuppliesDataModel CreateDataModel(string? suppliesId, string? tourId, int count)
|
||||
=> new(suppliesId, tourId, count);
|
||||
}
|
||||
@@ -76,6 +76,36 @@ internal static class MagicCarpetDbContextExtensions
|
||||
return employee;
|
||||
}
|
||||
|
||||
public static Agency InsertAgencyToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string? id = null, TourType tourType = TourType.Beach, int count = 20, List<(string, int)>? tours = null)
|
||||
{
|
||||
var agency = new Agency() { Id = id ?? Guid.NewGuid().ToString(), TourType = tourType, Count = count, Tours = [] };
|
||||
if (tours is not null)
|
||||
{
|
||||
foreach (var elem in tours)
|
||||
{
|
||||
agency.Tours.Add(new TourAgency { AgencyId = agency.Id, TourId = elem.Item1, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
dbContext.Agencies.Add(agency);
|
||||
dbContext.SaveChanges();
|
||||
return agency;
|
||||
}
|
||||
|
||||
public static Supplies InsertSuppliesToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string? id = null, TourType type = TourType.Beach, int count = 20, DateTime? date = null, List<(string, int)>? tours = null)
|
||||
{
|
||||
var supply = new Supplies() { Id = id ?? Guid.NewGuid().ToString(), Type = type, Count = count, ProductuionDate = date ?? DateTime.UtcNow, Tours = [] };
|
||||
if (tours is not null)
|
||||
{
|
||||
foreach (var elem in tours)
|
||||
{
|
||||
supply.Tours.Add(new TourSupplies { SuppliesId = supply.Id, TourId = elem.Item1, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
dbContext.Supplieses.Add(supply);
|
||||
dbContext.SaveChanges();
|
||||
return supply;
|
||||
}
|
||||
|
||||
public static Client? GetClientFromDatabase(this MagicCarpetDbContext dbContext, string id) => dbContext.Clients.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
public static Post? GetPostFromDatabaseByPostId(this MagicCarpetDbContext dbContext, string id) => dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual);
|
||||
@@ -91,6 +121,8 @@ internal static class MagicCarpetDbContextExtensions
|
||||
public static Sale[] GetSalesByClientId(this MagicCarpetDbContext dbContext, string? clientId) => [.. dbContext.Sales.Include(x => x.SaleTours).Where(x => x.ClientId == clientId)];
|
||||
|
||||
public static Employee? GetEmployeeFromDatabaseById(this MagicCarpetDbContext dbContext, string id) => dbContext.Employees.FirstOrDefault(x => x.Id == id);
|
||||
public static Agency? GetAgencyFromDatabaseById(this MagicCarpetDbContext dbContext, string id) => dbContext.Agencies.Include(x => x.Tours).FirstOrDefault(x => x.Id == id);
|
||||
public static Supplies? GetSuppliesFromDatabaseById(this MagicCarpetDbContext dbContext, string id) => dbContext.Supplieses.Include(x => x.Tours).FirstOrDefault(x => x.Id == id);
|
||||
|
||||
public static void RemoveClientsFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Clients\" CASCADE;");
|
||||
|
||||
@@ -104,5 +136,8 @@ internal static class MagicCarpetDbContextExtensions
|
||||
|
||||
public static void RemoveEmployeesFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
|
||||
|
||||
public static void RemoveAgenciesFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Agencies\" CASCADE;");
|
||||
public static void RemoveSuppliesFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Supplieses\" CASCADE;");
|
||||
|
||||
private static void ExecuteSqlRaw(this MagicCarpetDbContext dbContext, string command) => dbContext.Database.ExecuteSqlRaw(command);
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetDatabase.Implementations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class AgencyStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private AgencyStorageContract _agencyStorageContract;
|
||||
private Tour _tour;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_agencyStorageContract = new AgencyStorageContract(MagicCarpetDbContext);
|
||||
_tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name", TourType.Sightseeing);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Agencies\" CASCADE;");
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Tours\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var agency = InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
||||
InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
||||
InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
||||
var list = _agencyStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(), agency);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _agencyStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var agency = InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
||||
var result = _agencyStorageContract.GetElementById(agency.Id);
|
||||
AssertElement(result, agency);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() => _agencyStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenValidData_Test()
|
||||
{
|
||||
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 5, [_tour.Id]);
|
||||
_agencyStorageContract.AddElement(agency);
|
||||
var result = GetAgencyFromDatabaseById(agency.Id);
|
||||
AssertElement(result, agency);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 5, [_tour.Id]);
|
||||
InsertAgencyToDatabaseAndReturn(agency.Id, TourType.Sightseeing, 5, [(_tour.Id, 3)]);
|
||||
Assert.That(() => _agencyStorageContract.AddElement(agency), Throws.TypeOf<StorageException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenNoHaveTour_Test()
|
||||
{
|
||||
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 0, [_tour.Id]);
|
||||
InsertAgencyToDatabaseAndReturn(agency.Id, TourType.Sightseeing, 5, [(_tour.Id, 3)]);
|
||||
Assert.That(() => _agencyStorageContract.AddElement(agency), Throws.TypeOf<StorageException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenValidData_Test()
|
||||
{
|
||||
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 5, [_tour.Id]);
|
||||
InsertAgencyToDatabaseAndReturn(agency.Id, TourType.Ski, 10, null);
|
||||
_agencyStorageContract.UpdElement(agency);
|
||||
var result = GetAgencyFromDatabaseById(agency.Id);
|
||||
AssertElement(result, agency);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 5, [_tour.Id]);
|
||||
Assert.That(() => _agencyStorageContract.UpdElement(agency), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordExists_Test()
|
||||
{
|
||||
var agency = InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
||||
_agencyStorageContract.DelElement(agency.Id);
|
||||
var result = GetAgencyFromDatabaseById(agency.Id);
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordDoesNotExist_ThrowsElementNotFoundException()
|
||||
{
|
||||
Assert.That(() => _agencyStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
private Tour InsertTourToDatabaseAndReturn(string id, string name, TourType type)
|
||||
{
|
||||
var tour = new Tour { Id = id, TourName = name, TourType = type };
|
||||
MagicCarpetDbContext.Tours.Add(tour);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return tour;
|
||||
}
|
||||
|
||||
private Agency InsertAgencyToDatabaseAndReturn(string id, TourType type, int count, List<(string, int)>? tours = null)
|
||||
{
|
||||
var agency = new Agency { Id = id, TourType = type, Count = count, Tours = [] };
|
||||
if (tours is not null)
|
||||
{
|
||||
foreach (var elem in tours)
|
||||
{
|
||||
agency.Tours.Add(new TourAgency { AgencyId = agency.Id, TourId = elem.Item1, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
MagicCarpetDbContext.Agencies.Add(agency);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return agency;
|
||||
}
|
||||
|
||||
private static void AssertElement(AgencyDataModel? actual, Agency expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.TourType, Is.EqualTo(expected.TourType));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Tours is not null)
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Not.Null);
|
||||
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertElement(Agency? actual, AgencyDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.TourType, Is.EqualTo(expected.TourType));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Tours is not null)
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Not.Null);
|
||||
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static AgencyDataModel CreateModel(string id, TourType type, int count, List<string> toursIds)
|
||||
{
|
||||
var tours = toursIds.Select(x => new TourAgencyDataModel(id, x, 1)).ToList();
|
||||
return new(id, type, count, tours);
|
||||
}
|
||||
|
||||
private Agency? GetAgencyFromDatabaseById(string id)
|
||||
{
|
||||
return MagicCarpetDbContext.Agencies.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContracts;
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
internal abstract class BaseStorageContractTest
|
||||
{
|
||||
|
||||
@@ -11,7 +11,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContracts;
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class ClientStorageContractTests : BaseStorageContractTest
|
||||
|
||||
@@ -10,7 +10,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContracts;
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
class EmployeeStorageContractTests : BaseStorageContractTest
|
||||
|
||||
@@ -6,6 +6,7 @@ using MagicCarpetDatabase.Implementations;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using MagicCarpetTests.Infrastructure;
|
||||
using MagicCarpetTests.StoragesContracts;
|
||||
using MagicCarpetTests.StoragesContractsTests;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
@@ -9,7 +9,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContracts;
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SalaryStorageContractTests : BaseStorageContractTest
|
||||
|
||||
@@ -12,12 +12,12 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static NUnit.Framework.Internal.OSPlatform;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContracts;
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private SaleStorageContract _saletStorageContract;
|
||||
private SaleStorageContract _saleStorageContract;
|
||||
private Client _client;
|
||||
private Employee _employee;
|
||||
private Tour _tour;
|
||||
@@ -25,7 +25,7 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_saletStorageContract = new SaleStorageContract(MagicCarpetDbContext);
|
||||
_saleStorageContract = new SaleStorageContract(MagicCarpetDbContext);
|
||||
_client = MagicCarpetDbContext.InsertClientToDatabaseAndReturn();
|
||||
_employee = MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn();
|
||||
_tour = MagicCarpetDbContext.InsertTourToDatabaseAndReturn();
|
||||
@@ -46,7 +46,7 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
var sale = MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1, 1.2)]);
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 5, 1.2)]);
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(_tour.Id, 10, 1.2)]);
|
||||
var list = _saletStorageContract.GetList();
|
||||
var list = _saleStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == sale.Id), sale);
|
||||
@@ -55,7 +55,7 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _saletStorageContract.GetList();
|
||||
var list = _saleStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
@@ -67,7 +67,7 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(_tour.Id, 1, 1.2)]);
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(_tour.Id, 1, 1.2)]);
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(3), tours: [(_tour.Id, 1, 1.2)]);
|
||||
var list = _saletStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1));
|
||||
var list = _saleStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
}
|
||||
@@ -79,7 +79,7 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1, 1.2)]);
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1, 1.2)]);
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(employee.Id, null, tours: [(_tour.Id, 1, 1.2)]);
|
||||
var list = _saletStorageContract.GetList(employeeId: _employee.Id);
|
||||
var list = _saleStorageContract.GetList(employeeId: _employee.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.EmployeeId == _employee.Id));
|
||||
@@ -93,7 +93,7 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1, 1.2)]);
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, client.Id, tours: [(_tour.Id, 1, 1.2)]);
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(_tour.Id, 1, 1.2)]);
|
||||
var list = _saletStorageContract.GetList(clientId: _client.Id);
|
||||
var list = _saleStorageContract.GetList(clientId: _client.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.ClientId == _client.Id));
|
||||
@@ -107,7 +107,7 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1, 1.2), (tour.Id, 4, 1.2)]);
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(tour.Id, 1, 1.2)]);
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(tour.Id, 1, 1.2), (_tour.Id, 1, 1.2)]);
|
||||
var list = _saletStorageContract.GetList(tourId: _tour.Id);
|
||||
var list = _saleStorageContract.GetList(tourId: _tour.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
Assert.That(list.All(x => x.Tours!.Any(y => y.TourId == _tour.Id)));
|
||||
@@ -126,7 +126,7 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, client.Id, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(_tour.Id, 1, 1.2)]);
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(tour.Id, 1, 1.2)]);
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(employee.Id, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(_tour.Id, 1, 1.2)]);
|
||||
var list = _saletStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1), employeeId: _employee.Id, clientId: _client.Id, tourId: tour.Id);
|
||||
var list = _saleStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1), employeeId: _employee.Id, clientId: _client.Id, tourId: tour.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(1));
|
||||
}
|
||||
@@ -135,28 +135,28 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var sale = MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1, 1.2)]);
|
||||
AssertElement(_saletStorageContract.GetElementById(sale.Id), sale);
|
||||
AssertElement(_saleStorageContract.GetElementById(sale.Id), sale);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1, 1.2)]);
|
||||
Assert.That(() => _saletStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
Assert.That(() => _saleStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenRecordHasCanceled_Test()
|
||||
{
|
||||
var sale = MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1, 1.2)], isCancel: true);
|
||||
AssertElement(_saletStorageContract.GetElementById(sale.Id), sale);
|
||||
AssertElement(_saleStorageContract.GetElementById(sale.Id), sale);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var sale = CreateModel(Guid.NewGuid().ToString(), _employee.Id, _client.Id, DiscountType.RegularCustomer, false, [_tour.Id]);
|
||||
_saletStorageContract.AddElement(sale);
|
||||
_saleStorageContract.AddElement(sale);
|
||||
AssertElement(MagicCarpetDbContext.GetSaleFromDatabaseById(sale.Id), sale);
|
||||
}
|
||||
|
||||
@@ -164,7 +164,7 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
public void Try_AddElement_WhenIsDeletedIsTrue_Test()
|
||||
{
|
||||
var sale = CreateModel(Guid.NewGuid().ToString(), _employee.Id, _client.Id, DiscountType.RegularCustomer, true, [_tour.Id]);
|
||||
Assert.That(() => _saletStorageContract.AddElement(sale), Throws.Nothing);
|
||||
Assert.That(() => _saleStorageContract.AddElement(sale), Throws.Nothing);
|
||||
AssertElement(MagicCarpetDbContext.GetSaleFromDatabaseById(sale.Id), CreateModel(sale.Id, _employee.Id, _client.Id, DiscountType.RegularCustomer, false, [_tour.Id]));
|
||||
}
|
||||
|
||||
@@ -172,7 +172,7 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var sale = MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1, 1.2)], isCancel: false);
|
||||
_saletStorageContract.DelElement(sale.Id);
|
||||
_saleStorageContract.DelElement(sale.Id);
|
||||
var element = MagicCarpetDbContext.GetSaleFromDatabaseById(sale.Id);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
@@ -184,14 +184,53 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _saletStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
Assert.That(() => _saleStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordWasCanceled_Test()
|
||||
{
|
||||
var sale = MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1, 1.2)], isCancel: true);
|
||||
Assert.That(() => _saletStorageContract.DelElement(sale.Id), Throws.TypeOf<ElementDeletedException>());
|
||||
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)], isCancel: true);
|
||||
Assert.That(() => _saleStorageContract.DelElement(sale.Id), Throws.TypeOf<ElementDeletedException>());
|
||||
}
|
||||
|
||||
private Client InsertClientToDatabaseAndReturn(string fio = "test", string phoneNumber = "+7-777-777-77-77")
|
||||
{
|
||||
var client = new Client() { Id = Guid.NewGuid().ToString(), FIO = fio, PhoneNumber = phoneNumber, DiscountSize = 10 };
|
||||
MagicCarpetDbContext.Clients.Add(client);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return client;
|
||||
}
|
||||
|
||||
private Employee InsertEmployeeToDatabaseAndReturn(string fio = "test", string employeeEmail = "abc@gmail.com")
|
||||
{
|
||||
var employee = new Employee() { Id = Guid.NewGuid().ToString(), FIO = fio, Email = employeeEmail, PostId = Guid.NewGuid().ToString() };
|
||||
MagicCarpetDbContext.Employees.Add(employee);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return employee;
|
||||
}
|
||||
|
||||
private Tour InsertTourToDatabaseAndReturn(string tourName = "test", TourType tourType = TourType.Sightseeing, double price = 1)
|
||||
{
|
||||
var tour = new Tour() { Id = Guid.NewGuid().ToString(), TourName = tourName, TourType = tourType, Price = price };
|
||||
MagicCarpetDbContext.Tours.Add(tour);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return tour;
|
||||
}
|
||||
|
||||
private Sale InsertSaleToDatabaseAndReturn(string employeeId, string? clientId, DateTime? saleDate = null, double sum = 1, DiscountType discountType = DiscountType.OnSale, double discount = 0, bool isCancel = false, List<(string, int)>? tours = null)
|
||||
{
|
||||
var sale = new Sale() { EmployeeId = employeeId, ClientId = clientId, SaleDate = saleDate ?? DateTime.UtcNow, Sum = sum, DiscountType = discountType, Discount = discount, IsCancel = isCancel, SaleTours = [] };
|
||||
if (tours is not null)
|
||||
{
|
||||
foreach (var elem in tours)
|
||||
{
|
||||
sale.SaleTours.Add(new SaleTour { TourId = elem.Item1, SaleId = sale.Id, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
MagicCarpetDbContext.Sales.Add(sale);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return sale;
|
||||
}
|
||||
|
||||
private static void AssertElement(SaleDataModel? actual, Sale expected)
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetDatabase.Implementations;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SuppliesStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private SuppliesStorageContract _suppliesStorageContract;
|
||||
private Tour _tour;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_suppliesStorageContract = new SuppliesStorageContract(MagicCarpetDbContext);
|
||||
_tour = InsertTourToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Supplieses\" CASCADE;");
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Tours\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var supply = InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Ski, 5);
|
||||
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Ski, 5);
|
||||
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Ski, 5);
|
||||
var list = _suppliesStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(), supply);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _suppliesStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var supply = InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Ski, 5);
|
||||
AssertElement(_suppliesStorageContract.GetElementById(supply.Id), supply);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() => _suppliesStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var supply = CreateModel(Guid.NewGuid().ToString(), TourType.Ski, 5, [_tour.Id]);
|
||||
_suppliesStorageContract.AddElement(supply);
|
||||
AssertElement(GetSupplyFromDatabaseById(supply.Id), supply);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var supply = CreateModel(Guid.NewGuid().ToString(), TourType.Ski, 5, [_tour.Id]);
|
||||
InsertSupplyToDatabaseAndReturn(supply.Id, TourType.Ski, 5, [(_tour.Id, 3)]);
|
||||
Assert.That(() => _suppliesStorageContract.AddElement(supply), Throws.TypeOf<StorageException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var supply = CreateModel(Guid.NewGuid().ToString(), TourType.Ski, 5, [_tour.Id]);
|
||||
InsertSupplyToDatabaseAndReturn(supply.Id, TourType.Ski, 5, null);
|
||||
_suppliesStorageContract.UpdElement(supply);
|
||||
AssertElement(GetSupplyFromDatabaseById(supply.Id), supply);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _suppliesStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString(), TourType.Ski, 5, [_tour.Id])), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
private Tour InsertTourToDatabaseAndReturn()
|
||||
{
|
||||
var tour = new Tour { Id = Guid.NewGuid().ToString(), TourName = "Test Tour", TourType = TourType.Ski };
|
||||
MagicCarpetDbContext.Tours.Add(tour);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return tour;
|
||||
}
|
||||
|
||||
private Supplies InsertSupplyToDatabaseAndReturn(string id, TourType type, int count, List<(string, int)>? tours = null)
|
||||
{
|
||||
var supply = new Supplies { Id = id, Type = type, ProductuionDate = DateTime.UtcNow, Count = count, Tours = [] };
|
||||
if (tours is not null)
|
||||
{
|
||||
foreach (var elem in tours)
|
||||
{
|
||||
supply.Tours.Add(new TourSupplies { SuppliesId = supply.Id, TourId = elem.Item1, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
MagicCarpetDbContext.Supplieses.Add(supply);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return supply;
|
||||
}
|
||||
|
||||
private static void AssertElement(SuppliesDataModel? actual, Supplies expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.TourType, Is.EqualTo(expected.Type));
|
||||
Assert.That(actual.ProductuionDate, Is.EqualTo(expected.ProductuionDate));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Tours is not null)
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Not.Null);
|
||||
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertElement(Supplies? actual, SuppliesDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Type, Is.EqualTo(expected.TourType));
|
||||
Assert.That(actual.ProductuionDate, Is.EqualTo(expected.ProductuionDate));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Tours is not null)
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Not.Null);
|
||||
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static SuppliesDataModel CreateModel(string id, TourType type, int count, List<string> toursIds)
|
||||
{
|
||||
var tours = toursIds.Select(x => new TourSuppliesDataModel(id, x, 1)).ToList();
|
||||
return new(id, type, DateTime.UtcNow, count, tours);
|
||||
}
|
||||
|
||||
|
||||
private Supplies? GetSupplyFromDatabaseById(string id)
|
||||
{
|
||||
return MagicCarpetDbContext.Supplieses.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static NUnit.Framework.Internal.OSPlatform;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContracts;
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class TourStorageContractTests : BaseStorageContractTest
|
||||
@@ -166,6 +166,23 @@ internal class TourStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
Assert.That(() => _tourStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
private Tour InsertTourToDatabaseAndReturn(string id, string tourName = "test", string tourCountry = "country", TourType tourType = TourType.Beach, double price = 1)
|
||||
{
|
||||
var tour = new Tour() { Id = id, TourName = tourName, TourCountry = tourCountry, TourType = tourType, Price = price };
|
||||
MagicCarpetDbContext.Tours.Add(tour);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return tour;
|
||||
}
|
||||
|
||||
private TourHistory InsertTourHistoryToDatabaseAndReturn(string tourId, double price, DateTime changeDate)
|
||||
{
|
||||
var tourHistory = new TourHistory() { Id = Guid.NewGuid().ToString(), TourId = tourId, OldPrice = price, ChangeDate = changeDate };
|
||||
MagicCarpetDbContext.TourHistories.Add(tourHistory);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return tourHistory;
|
||||
}
|
||||
|
||||
private static void AssertElement(TourDataModel? actual, Tour expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
|
||||
@@ -0,0 +1,296 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.BindingModels;
|
||||
using MagicCarpetContracts.BusinessLogicContracts;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.ViewModels;
|
||||
using MagicCarpetWebApi.Adapters;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.WebApiAdapterTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SuppliesAdapterTests
|
||||
{
|
||||
private Mock<ISuppliesBusinessLogicContract> _suppliesBusinessLogicContract;
|
||||
private SuppliesAdapter _adapter;
|
||||
private Mock<ILogger<SuppliesAdapter>> _logger;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_suppliesBusinessLogicContract = new Mock<ISuppliesBusinessLogicContract>();
|
||||
_logger = new Mock<ILogger<SuppliesAdapter>>();
|
||||
_adapter = new SuppliesAdapter(_suppliesBusinessLogicContract.Object, _logger.Object);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllComponents_WhenSuppliesExist_ReturnOk()
|
||||
{
|
||||
// Arrange
|
||||
var supplies = new List<SuppliesDataModel>
|
||||
{
|
||||
new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Ski, DateTime.Now, 5, []),
|
||||
new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Ski, DateTime.Now.AddDays(1), 10, [])
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.GetAllComponents()).Returns(supplies);
|
||||
// Act
|
||||
var result = _adapter.GetAllComponents();
|
||||
var list = (List<SuppliesViewModel>)result.Result!;
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
Assert.That(list.Count, Is.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllComponents_WhenListNull_ReturnNotFound()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesBusinessLogicContract.Setup(x => x.GetAllComponents()).Throws(new NullListException());
|
||||
// Act
|
||||
var result = _adapter.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllComponents_WhenStorageException_ReturnsInternalServerError()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesBusinessLogicContract.Setup(x => x.GetAllComponents()).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act
|
||||
var result = _adapter.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.InternalServerError));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetComponentByData_WhenSupplies_ReturnOK()
|
||||
{
|
||||
var supply = new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Ski, DateTime.Now, 5, []);
|
||||
// Arrange
|
||||
_suppliesBusinessLogicContract.Setup(x => x.GetComponentByData(supply.Id)).Returns(supply);
|
||||
// Act
|
||||
var result = _adapter.GetComponentByData(supply.Id);
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var resultData = (SuppliesViewModel)result.Result!;
|
||||
Assert.That(resultData.Id!, Is.EqualTo(supply.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetComponentByData_WhenComponentNotFound_ReturnsNotFound()
|
||||
{
|
||||
// Arrange
|
||||
var data = "test";
|
||||
_suppliesBusinessLogicContract.Setup(x => x.GetComponentByData(data)).Throws(new ElementNotFoundException(""));
|
||||
// Act
|
||||
var result = _adapter.GetComponentByData(data);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetComponentByData_WhenStorageException_ReturnInternalServerError()
|
||||
{
|
||||
// Arrange
|
||||
var data = "test";
|
||||
_suppliesBusinessLogicContract.Setup(x => x.GetComponentByData(data)).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act
|
||||
var result = _adapter.GetComponentByData(data);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.InternalServerError));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponent_WhenValidData_ReturnsNoContent()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
TourType = TourType.Ski,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Tours = [new TourSuppliesBindingModel { TourId = Guid.NewGuid().ToString(), SuppliesId = Guid.NewGuid().ToString(), Count = 5 }]
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.InsertComponent(It.IsAny<SuppliesDataModel>()));
|
||||
// Act
|
||||
var result = _adapter.InsertComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponent_WhenDataIsNull_ReturnsBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesBusinessLogicContract.Setup(x => x.InsertComponent(null)).Throws(new ArgumentNullException());
|
||||
// Act
|
||||
var result = _adapter.InsertComponent(null);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponent_WhenValidationError_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
TourType = TourType.None,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Tours = []
|
||||
};
|
||||
// Act
|
||||
var result = _adapter.InsertComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponent_WhenElementExistException_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply1 = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
TourType = TourType.None,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Tours = []
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.InsertComponent(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act
|
||||
var result = _adapter.InsertComponent(supply1);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponent_WhenStorageException_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
TourType = TourType.Ski,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Tours = new List<TourSuppliesBindingModel>()
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.InsertComponent(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act
|
||||
var result = _adapter.InsertComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponent_WhenValidData_ReturnsNoContent()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel { Id = Guid.NewGuid().ToString() };
|
||||
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>()));
|
||||
// Act
|
||||
var result = _adapter.UpdateComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponent_WhenDataIsNull_ReturnsBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var component = new SuppliesBindingModel { Id = Guid.NewGuid().ToString() };
|
||||
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
// Act
|
||||
var result = _adapter.UpdateComponent(component);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponent_WhenValidationError_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
TourType = TourType.Ski,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Tours = []
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new ValidationException(""));
|
||||
// Act
|
||||
var result = _adapter.UpdateComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponent_WhenElementNotFoundException_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
TourType = TourType.None,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Tours = []
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
// Act
|
||||
var result = _adapter.UpdateComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponent_WhenElementExistException_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply1 = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
TourType = TourType.None,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Tours = []
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act
|
||||
var result = _adapter.UpdateComponent(supply1);
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponent_WhenStorageException_ReturnBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
var supply = new SuppliesBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
TourType = TourType.Ski,
|
||||
ProductuionDate = DateTime.Now,
|
||||
Count = 5,
|
||||
Tours = new List<TourSuppliesBindingModel>()
|
||||
};
|
||||
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act
|
||||
var result = _adapter.UpdateComponent(supply);
|
||||
// Assert
|
||||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
using MagicCarpetContracts.BindingModels;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.ViewModels;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using MagicCarpetTests.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.WebApiControllersTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class AgencyControllerTests : BaseWebApiControllerTest
|
||||
{
|
||||
private string _tourId;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_tourId = MagicCarpetDbContext.InsertTourToDatabaseAndReturn().Id;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MagicCarpetDbContext.RemoveToursFromDatabase();
|
||||
MagicCarpetDbContext.RemoveAgenciesFromDatabase();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAllAgencies_WhenHaveRecords_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var agency = MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
||||
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
||||
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync("/api/agency");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<AgencyViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(3));
|
||||
});
|
||||
AssertElement(data.First(x => x.Id == agency.Id), agency);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_WhenNoRecords_ShouldSuccess_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync("/api/agency");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<AgencyViewModel>>(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 agency = MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/agency/{agency.Id}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
AssertElement(await GetModelFromResponseAsync<AgencyViewModel>(response), agency);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
|
||||
{
|
||||
//Arrange
|
||||
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/agency/{Guid.NewGuid()}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn(tours: [(_tourId, 5)]);
|
||||
var agency = CreateModel(tours: [(_tourId, 5)]);
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/agency", MakeContent(agency));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
AssertElement(MagicCarpetDbContext.GetAgencyFromDatabaseById(agency.Id!), agency);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/agency", 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/agency", 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 furnitureIdIncorrect = new AgencyBindingModel { Id = "Id" };
|
||||
var furnitureWithNameIncorrect = new AgencyBindingModel { Id = Guid.NewGuid().ToString(), TourType = TourType.None };
|
||||
//Act
|
||||
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/agency", MakeContent(furnitureIdIncorrect));
|
||||
var responseWithNameIncorrect = await HttpClient.PostAsync($"/api/agency", MakeContent(furnitureWithNameIncorrect));
|
||||
//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
|
||||
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
||||
var agency = CreateModel(tours: [(_tourId, 5)]);
|
||||
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn(agency.Id);
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/agency", MakeContent(agency));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
MagicCarpetDbContext.ChangeTracker.Clear();
|
||||
AssertElement(MagicCarpetDbContext.GetAgencyFromDatabaseById(agency.Id!), agency);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var furniture = CreateModel();
|
||||
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/agency", MakeContent(furniture));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/agency", 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/agency", MakeContent(new { Data = "test", Position = 10 }));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var component = Guid.NewGuid().ToString();
|
||||
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn(component);
|
||||
//Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/agency/{component}");
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
Assert.That(MagicCarpetDbContext.GetSaleFromDatabaseById(component), Is.Null);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/agency/{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/agency/id");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
private static AgencyBindingModel CreateModel(string? id = null, TourType tourType = TourType.Ski, int count = 21, List<(string, int)>? tours = null)
|
||||
{
|
||||
var agency = new AgencyBindingModel() { Id = id ?? Guid.NewGuid().ToString(), TourType = tourType, Count = count, Tours = [] };
|
||||
if (tours is not null)
|
||||
{
|
||||
foreach (var elem in tours)
|
||||
{
|
||||
agency.Tours.Add(new TourAgencyBindingModel { AgencyId = agency.Id, TourId = elem.Item1, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
return agency;
|
||||
}
|
||||
|
||||
private static void AssertElement(AgencyViewModel? actual, Agency expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Tours is not null)
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Not.Null);
|
||||
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertElement(Agency? actual, AgencyBindingModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Tours is not null)
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Not.Null);
|
||||
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@ internal class SaleControllerTests : BaseWebApiControllerTest
|
||||
MagicCarpetDbContext.RemoveEmployeesFromDatabase();
|
||||
MagicCarpetDbContext.RemoveClientsFromDatabase();
|
||||
MagicCarpetDbContext.RemoveToursFromDatabase();
|
||||
MagicCarpetDbContext.RemoveAgenciesFromDatabase();
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -310,6 +311,7 @@ internal class SaleControllerTests : BaseWebApiControllerTest
|
||||
public async Task Post_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
||||
MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, null, tours: [(_tourId, 5, 1.1)]);
|
||||
var saleModel = CreateModel(_employeeId, _clientId, _tourId);
|
||||
//Act
|
||||
|
||||
@@ -0,0 +1,254 @@
|
||||
using MagicCarpetContracts.BindingModels;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.ViewModels;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using MagicCarpetTests.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.WebApiControllersTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SuppliesControllerTests : BaseWebApiControllerTest
|
||||
{
|
||||
private string _componentId;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_componentId = MagicCarpetDbContext.InsertTourToDatabaseAndReturn().Id;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MagicCarpetDbContext.RemoveToursFromDatabase();
|
||||
MagicCarpetDbContext.RemoveSuppliesFromDatabase();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAllWarehouses_WhenHaveRecords_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var supply = MagicCarpetDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
MagicCarpetDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
MagicCarpetDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync("/api/supplies");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<SuppliesViewModel>>(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");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<AgencyViewModel>>(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 supplies = MagicCarpetDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/supplies/{supplies.Id}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
AssertElement(await GetModelFromResponseAsync<SuppliesViewModel>(response), supplies);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
|
||||
{
|
||||
//Arrange
|
||||
MagicCarpetDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
//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
|
||||
MagicCarpetDbContext.InsertSuppliesToDatabaseAndReturn(tours: [(_componentId, 5)]);
|
||||
var supplies = CreateModel(components: [(_componentId, 5)]);
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/supplies", MakeContent(supplies));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
AssertElement(MagicCarpetDbContext.GetSuppliesFromDatabaseById(supplies.Id!), supplies);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/supplies", 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", 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 SuppliesViewModel { Id = "Id", TourType = TourType.None, Tours = [] };
|
||||
var suppliesWithTypeIncorrect = new SuppliesViewModel { Id = Guid.NewGuid().ToString(), TourType = TourType.None, Tours = [] };
|
||||
//Act
|
||||
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/supplies", MakeContent(suppliesIdIncorrect));
|
||||
var responseWithTypeIncorrect = await HttpClient.PostAsync($"/api/supplies", 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
|
||||
MagicCarpetDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
var supplies = CreateModel(components: [(_componentId, 5)]);
|
||||
MagicCarpetDbContext.InsertSuppliesToDatabaseAndReturn(supplies.Id);
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/supplies", MakeContent(supplies));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
MagicCarpetDbContext.ChangeTracker.Clear();
|
||||
AssertElement(MagicCarpetDbContext.GetSuppliesFromDatabaseById(supplies.Id!), supplies);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var furniture = CreateModel();
|
||||
MagicCarpetDbContext.InsertSuppliesToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/supplies", MakeContent(furniture));
|
||||
//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", 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", MakeContent(new { Data = "test", Position = 10 }));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
|
||||
private static SuppliesBindingModel CreateModel(string? id = null, TourType type = TourType.Ski, int count = 21, DateTime? date = null, List<(string, int)>? components = null)
|
||||
{
|
||||
var Supply = new SuppliesBindingModel() { Id = id ?? Guid.NewGuid().ToString(), TourType = type, Count = count, ProductuionDate = date ?? DateTime.UtcNow,Tours = [] };
|
||||
if (components is not null)
|
||||
{
|
||||
foreach (var elem in components)
|
||||
{
|
||||
Supply.Tours.Add(new TourSuppliesBindingModel { SuppliesId = Supply.Id, TourId = elem.Item1, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
return Supply;
|
||||
}
|
||||
|
||||
private static void AssertElement(SuppliesViewModel? actual, Supplies expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
Assert.That(actual.ProductuionDate.Date, Is.EqualTo(expected.ProductuionDate.Date));
|
||||
});
|
||||
if (expected.Tours is not null)
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Not.Null);
|
||||
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertElement(Supplies? actual, SuppliesBindingModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
Assert.That(actual.ProductuionDate.Date, Is.EqualTo(expected.ProductuionDate.Date));
|
||||
});
|
||||
if (expected.Tours is not null)
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Not.Null);
|
||||
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Null);
|
||||
}
|
||||
}
|
||||
}
|
||||
190
MagicCarpetProject/MagicCarpetWebApi/Adapters/AgencyAdapter.cs
Normal file
190
MagicCarpetProject/MagicCarpetWebApi/Adapters/AgencyAdapter.cs
Normal file
@@ -0,0 +1,190 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.AdapterContracts;
|
||||
using MagicCarpetContracts.AdapterContracts.OperationResponses;
|
||||
using MagicCarpetContracts.BindingModels;
|
||||
using MagicCarpetContracts.BusinessLogicContracts;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.ViewModels;
|
||||
|
||||
namespace MagicCarpetWebApi.Adapters;
|
||||
|
||||
public class AgencyAdapter : IAgencyAdapter
|
||||
{
|
||||
private readonly IAgencyBusinessLogicContract _warehouseBusinessLogicContract;
|
||||
private readonly ILogger<AgencyAdapter> _logger;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public AgencyAdapter(IAgencyBusinessLogicContract warehouseBusinessLogicContract, ILogger<AgencyAdapter> logger)
|
||||
{
|
||||
_warehouseBusinessLogicContract = warehouseBusinessLogicContract;
|
||||
_logger = logger;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<AgencyBindingModel, AgencyDataModel>();
|
||||
cfg.CreateMap<AgencyDataModel, AgencyViewModel>();
|
||||
cfg.CreateMap<TourAgencyBindingModel, TourAgencyDataModel>();
|
||||
cfg.CreateMap<TourAgencyDataModel, TourAgencyViewModel>();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public AgencyOperationResponse GetAllComponents()
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine(_warehouseBusinessLogicContract.GetAllComponents());
|
||||
return AgencyOperationResponse.OK([.. _warehouseBusinessLogicContract.GetAllComponents().Select(x => _mapper.Map<AgencyViewModel>(x))]);
|
||||
}
|
||||
catch (NullListException)
|
||||
{
|
||||
_logger.LogError("NullListException: The list of warehouse is null");
|
||||
return AgencyOperationResponse.NotFound("The list of warehouse is not initialized");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return AgencyOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return AgencyOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public AgencyOperationResponse GetComponentByData(string data)
|
||||
{
|
||||
try
|
||||
{
|
||||
return AgencyOperationResponse.OK(_mapper.Map<AgencyViewModel>(_warehouseBusinessLogicContract.GetComponentByData(data)));
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: Data is null or empty");
|
||||
return AgencyOperationResponse.BadRequest("Data is empty");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException: warehouse not found");
|
||||
return AgencyOperationResponse.NotFound($"warehouse with data '{data}' not found");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return AgencyOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return AgencyOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public AgencyOperationResponse InsertComponent(AgencyBindingModel warehouseDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_warehouseBusinessLogicContract.InsertComponent(_mapper.Map<AgencyDataModel>(warehouseDataModel));
|
||||
return AgencyOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: warehouse data is null");
|
||||
return AgencyOperationResponse.BadRequest("furniture data is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException: Invalid warehouse data");
|
||||
return AgencyOperationResponse.BadRequest($"Invalid warehouse data: {ex.Message}");
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementExistsException: warehouse already exists");
|
||||
return AgencyOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return AgencyOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return AgencyOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public AgencyOperationResponse UpdateComponent(AgencyBindingModel warehouseDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_warehouseBusinessLogicContract.UpdateComponent(_mapper.Map<AgencyDataModel>(warehouseDataModel));
|
||||
return AgencyOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: warehouse data is null");
|
||||
return AgencyOperationResponse.BadRequest("warehouse data is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException: Invalid warehouse data");
|
||||
return AgencyOperationResponse.BadRequest($"Invalid warehouse data: {ex.Message}");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException: warehouse not found");
|
||||
return AgencyOperationResponse.BadRequest($"warehouse with id '{warehouseDataModel.Id}' not found");
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementExistsException: warehouse already exists");
|
||||
return AgencyOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return AgencyOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return AgencyOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public AgencyOperationResponse DeleteComponent(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
_warehouseBusinessLogicContract.DeleteComponent(id);
|
||||
return AgencyOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: Id is null or empty");
|
||||
return AgencyOperationResponse.BadRequest("Id is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException: Invalid id");
|
||||
return AgencyOperationResponse.BadRequest($"Invalid id: {ex.Message}");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException: warehouse not found");
|
||||
return AgencyOperationResponse.BadRequest($"furniture with id '{id}' not found");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return AgencyOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return AgencyOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
160
MagicCarpetProject/MagicCarpetWebApi/Adapters/SuppliesAdapter.cs
Normal file
160
MagicCarpetProject/MagicCarpetWebApi/Adapters/SuppliesAdapter.cs
Normal file
@@ -0,0 +1,160 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.AdapterContracts;
|
||||
using MagicCarpetContracts.AdapterContracts.OperationResponses;
|
||||
using MagicCarpetContracts.BindingModels;
|
||||
using MagicCarpetContracts.BusinessLogicContracts;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.ViewModels;
|
||||
|
||||
namespace MagicCarpetWebApi.Adapters;
|
||||
|
||||
public class SuppliesAdapter : ISuppliesAdapter
|
||||
{
|
||||
private readonly ISuppliesBusinessLogicContract _suppliesBusinessLogicContract;
|
||||
private readonly ILogger<SuppliesAdapter> _logger;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public SuppliesAdapter(ISuppliesBusinessLogicContract suppliesBusinessLogicContract,
|
||||
ILogger<SuppliesAdapter> logger)
|
||||
{
|
||||
_suppliesBusinessLogicContract = suppliesBusinessLogicContract;
|
||||
_logger = logger;
|
||||
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<SuppliesBindingModel, SuppliesDataModel>();
|
||||
cfg.CreateMap<SuppliesDataModel, SuppliesViewModel>();
|
||||
cfg.CreateMap<TourSuppliesBindingModel, TourSuppliesDataModel>();
|
||||
cfg.CreateMap<TourSuppliesDataModel, TourSuppliesViewModel>();
|
||||
});
|
||||
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public SuppliesOperationResponse GetAllComponents()
|
||||
{
|
||||
try
|
||||
{
|
||||
return SuppliesOperationResponse.OK([.. _suppliesBusinessLogicContract.GetAllComponents().Select(x => _mapper.Map<SuppliesViewModel>(x))]);
|
||||
}
|
||||
catch (NullListException)
|
||||
{
|
||||
_logger.LogError("NullListException: The list of supplies is null");
|
||||
return SuppliesOperationResponse.NotFound("The list of supplies is not initialized");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return SuppliesOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return SuppliesOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public SuppliesOperationResponse GetComponentByData(string data)
|
||||
{
|
||||
try
|
||||
{
|
||||
return SuppliesOperationResponse.OK(_mapper.Map<SuppliesViewModel>(_suppliesBusinessLogicContract.GetComponentByData(data)));
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: Data is null or empty");
|
||||
return SuppliesOperationResponse.BadRequest("Data is empty");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException: Component not found");
|
||||
return SuppliesOperationResponse.NotFound($"Component with data '{data}' not found");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return SuppliesOperationResponse.InternalServerError(
|
||||
$"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return SuppliesOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public SuppliesOperationResponse InsertComponent(SuppliesBindingModel componentDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_suppliesBusinessLogicContract.InsertComponent(_mapper.Map<SuppliesDataModel>(componentDataModel));
|
||||
return SuppliesOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: Component data is null");
|
||||
return SuppliesOperationResponse.BadRequest("Component data is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException: Invalid component data");
|
||||
return SuppliesOperationResponse.BadRequest($"Invalid component data: {ex.Message}");
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementExistsException: Component already exists");
|
||||
return SuppliesOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return SuppliesOperationResponse.BadRequest(
|
||||
$"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return SuppliesOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public SuppliesOperationResponse UpdateComponent(SuppliesBindingModel componentModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_suppliesBusinessLogicContract.UpdateComponent(_mapper.Map<SuppliesDataModel>(componentModel));
|
||||
return SuppliesOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException: Component data is null");
|
||||
return SuppliesOperationResponse.BadRequest("Component data is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException: Invalid component data");
|
||||
return SuppliesOperationResponse.BadRequest($"Invalid component data: {ex.Message}");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException: Component not found");
|
||||
return SuppliesOperationResponse.BadRequest($"Component with id '{componentModel.Id}' not found");
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementExistsException: Component already exists");
|
||||
return SuppliesOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException: Error while working with data storage");
|
||||
return SuppliesOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception: An unexpected error occurred");
|
||||
return SuppliesOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using MagicCarpetContracts.AdapterContracts;
|
||||
using MagicCarpetContracts.BindingModels;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MagicCarpetWebApi.Controllers;
|
||||
|
||||
[Authorize]
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[Produces("application/json")]
|
||||
public class AgencyController(IAgencyAdapter adapter) : ControllerBase
|
||||
{
|
||||
private readonly IAgencyAdapter _adapter = adapter;
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult GetAll()
|
||||
{
|
||||
return _adapter.GetAllComponents().GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpGet("{data}")]
|
||||
public IActionResult GetComponentByData(string data)
|
||||
{
|
||||
return _adapter.GetComponentByData(data).GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Register([FromBody] AgencyBindingModel agencyBindingModel)
|
||||
{
|
||||
return _adapter.InsertComponent(agencyBindingModel).GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public IActionResult ChangeInfo([FromBody] AgencyBindingModel agencyBindingModel)
|
||||
{
|
||||
return _adapter.UpdateComponent(agencyBindingModel).GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public IActionResult DeleteComponent(string id)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
return BadRequest("Id is null or empty");
|
||||
|
||||
return _adapter.DeleteComponent(id).GetResponse(Request, Response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using MagicCarpetContracts.AdapterContracts;
|
||||
using MagicCarpetContracts.BindingModels;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MagicCarpetWebApi.Controllers;
|
||||
|
||||
[Authorize]
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[Produces("application/json")]
|
||||
public class SuppliesController(ISuppliesAdapter adapter) : ControllerBase
|
||||
{
|
||||
private readonly ISuppliesAdapter _adapter = adapter;
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult GetAll()
|
||||
{
|
||||
return _adapter.GetAllComponents().GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpGet("{data}")]
|
||||
public IActionResult GetComponentByData(string data)
|
||||
{
|
||||
return _adapter.GetComponentByData(data).GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Register([FromBody] SuppliesBindingModel suppliesBindingModel)
|
||||
{
|
||||
return _adapter.InsertComponent(suppliesBindingModel).GetResponse(Request, Response);
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public IActionResult ChangeInfo([FromBody] SuppliesBindingModel suppliesBindingModel)
|
||||
{
|
||||
return _adapter.UpdateComponent(suppliesBindingModel).GetResponse(Request, Response);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using MagicCarpetBusinessLogic.Implementations;
|
||||
using MagicCarpetContracts.AdapterContracts;
|
||||
using MagicCarpetContracts.BuisnessLogicContracts;
|
||||
using MagicCarpetContracts.BusinessLogicContracts;
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetDatabase;
|
||||
@@ -57,6 +58,8 @@ builder.Services.AddTransient<ITourBusinessLogicContract, TourBusinessLogicContr
|
||||
builder.Services.AddTransient<ISalaryBusinessLogicContract, SalaryBusinessLogicContract>();
|
||||
builder.Services.AddTransient<ISaleBusinessLogicContract, SaleBusinessLogicContract>();
|
||||
builder.Services.AddTransient<IEmployeeBusinessLogicContract, EmployeeBusinessLogicContract>();
|
||||
builder.Services.AddTransient<IAgencyBusinessLogicContract, AgencyBusinessLogicContract>();
|
||||
builder.Services.AddTransient<ISuppliesBusinessLogicContract, SuppliesBusinessLogicContract>();
|
||||
|
||||
builder.Services.AddTransient<MagicCarpetDbContext>();
|
||||
builder.Services.AddTransient<IClientStorageContract, ClientStorageContract>();
|
||||
@@ -65,6 +68,8 @@ builder.Services.AddTransient<ITourStorageContract, TourStorageContract>();
|
||||
builder.Services.AddTransient<ISalaryStorageContract, SalaryStorageContract>();
|
||||
builder.Services.AddTransient<ISaleStorageContract, SaleStorageContract>();
|
||||
builder.Services.AddTransient<IEmployeeStorageContract, EmployeeStorageContract>();
|
||||
builder.Services.AddTransient<IAgencyStorageContract, AgencyStorageContract>();
|
||||
builder.Services.AddTransient<ISuppliesStorageContract, SuppliesStorageContract>();
|
||||
|
||||
builder.Services.AddTransient<IClientAdapter, ClientAdapter>();
|
||||
builder.Services.AddTransient<IPostAdapter, PostAdapter>();
|
||||
@@ -72,6 +77,8 @@ builder.Services.AddTransient<ITourAdapter, TourAdapter>();
|
||||
builder.Services.AddTransient<ISaleAdapter, SaleAdapter>();
|
||||
builder.Services.AddTransient<IEmployeeAdapter, EmployeeAdapter>();
|
||||
builder.Services.AddTransient<ISalaryAdapter, SalaryAdapter>();
|
||||
builder.Services.AddTransient<IAgencyAdapter, AgencyAdapter>();
|
||||
builder.Services.AddTransient<ISuppliesAdapter, SuppliesAdapter>();
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
Reference in New Issue
Block a user