forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
до тестов
This commit is contained in:
@@ -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,25 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
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.Enums;
|
||||
using MagicCarpetContracts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.DataModels;
|
||||
|
||||
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> Components { get; set; }
|
||||
}
|
||||
@@ -14,7 +14,7 @@ namespace MagicCarpetContracts.DataModels;
|
||||
public class SuppliesDataModel(string id, TourType tourType, DateTime date, int count, List<TourSuppliesDataModel> tours) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public TourType Type { get; private set; } = tourType;
|
||||
public TourType TourType { get; private set; } = tourType;
|
||||
public DateTime ProductuionDate { get; private set; } = date;
|
||||
public int Count { get; private set; } = count;
|
||||
public List<TourSuppliesDataModel> Tours { get; private set; } = tours;
|
||||
@@ -25,7 +25,7 @@ public class SuppliesDataModel(string id, TourType tourType, DateTime date, int
|
||||
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 (Type == TourType.None)
|
||||
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");
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
@@ -21,16 +21,8 @@ public class AgencyStorageContract : IAgencyStorageContract
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Agency, AgencyDataModel>()
|
||||
.ConstructUsing(src => new AgencyDataModel(
|
||||
src.Id,
|
||||
src.Type,
|
||||
src.Count,
|
||||
_mapper.Map<List<TourAgencyDataModel>>(src.Tours)
|
||||
));
|
||||
|
||||
cfg.CreateMap<AgencyDataModel, Agency>();
|
||||
cfg.CreateMap<TourAgencyDataModel, TourAgency>().ReverseMap();
|
||||
cfg.AddMaps(typeof(TourAgency));
|
||||
cfg.AddMaps(typeof(Agency));
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ internal class SuppliesDataModelTests
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(model.Id, Is.EqualTo(id));
|
||||
Assert.That(model.Type, Is.EqualTo(type));
|
||||
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));
|
||||
|
||||
@@ -75,6 +75,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 warehouse = new Agency() { Id = id ?? Guid.NewGuid().ToString(), Type = tourType, Count = count, Tours = [] };
|
||||
if (tours is not null)
|
||||
{
|
||||
foreach (var elem in tours)
|
||||
{
|
||||
warehouse.Tours.Add(new TourAgency { AgencyId = warehouse.Id, TourId = elem.Item1, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
dbContext.Agencies.Add(warehouse);
|
||||
dbContext.SaveChanges();
|
||||
return warehouse;
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -90,6 +120,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;");
|
||||
|
||||
@@ -103,5 +135,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);
|
||||
}
|
||||
@@ -17,7 +17,7 @@ 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,7 +184,7 @@ 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]
|
||||
|
||||
@@ -0,0 +1,287 @@
|
||||
using MagicCarpetContracts.BindingModels;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
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 _componentId;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_componentId = MagicCarpetDbContext.InsertTourToDatabaseAndReturn().Id;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MagicCarpetDbContext.RemoveToursFromDatabase();
|
||||
MagicCarpetDbContext.RemoveAgenciesFromDatabase();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAllAgencys_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: [(_componentId, 5)]);
|
||||
var agency = CreateModel(components: [(_componentId, 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(components: [(_componentId, 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.Beach, int count = 21, List<(string, int)>? components = null)
|
||||
{
|
||||
var agency = new AgencyBindingModel() { Id = id ?? Guid.NewGuid().ToString(), TourType = tourType, Count = count, Tours = [] };
|
||||
if (components is not null)
|
||||
{
|
||||
foreach (var elem in components)
|
||||
{
|
||||
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.Components, Is.Not.Null);
|
||||
Assert.That(actual.Components, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.Components.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Components[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.Components[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Components, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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,46 @@
|
||||
using MagicCarpetContracts.AdapterContracts;
|
||||
using MagicCarpetContracts.BindingModels;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MagicCarpetWebApi.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
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,37 @@
|
||||
using MagicCarpetContracts.AdapterContracts;
|
||||
using MagicCarpetContracts.BindingModels;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MagicCarpetWebApi.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
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;
|
||||
@@ -56,6 +57,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>();
|
||||
@@ -64,6 +67,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>();
|
||||
@@ -71,6 +76,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