Производитель

This commit is contained in:
2025-03-09 16:18:45 +04:00
parent 72547eeb41
commit 1b3e780fb7
20 changed files with 829 additions and 152 deletions

View File

@@ -13,10 +13,10 @@ internal class PostBusinessLogicContract(IPostStorageContract postStorageContrac
private readonly ILogger _logger = logger;
private readonly IPostStorageContract _postStorageContract = postStorageContract;
public List<PostDataModel> GetAllPosts(bool onlyActive = true)
public List<PostDataModel> GetAllPosts()
{
_logger.LogInformation("GetAllPosts params: {onlyActive}", onlyActive);
return _postStorageContract.GetList(onlyActive) ?? throw new NullListException();
_logger.LogInformation("GetAllPosts");
return _postStorageContract.GetList() ?? throw new NullListException();
}
public List<PostDataModel> GetAllDataOfPost(string postId)

View File

@@ -0,0 +1,17 @@
using CatHasPawsContratcs.AdapterContracts.OperationResponses;
using CatHasPawsContratcs.BindingModels;
namespace CatHasPawsContratcs.AdapterContracts;
public interface IManufacturerAdapter
{
ManufacturerOperationResponse GetList();
ManufacturerOperationResponse GetElement(string data);
ManufacturerOperationResponse RegisterManufacturer(ManufacturerBindingModel manufacturerModel);
ManufacturerOperationResponse ChangeManufacturerInfo(ManufacturerBindingModel manufacturerModel);
ManufacturerOperationResponse RemoveManufacturer(string id);
}

View File

@@ -0,0 +1,19 @@
using CatHasPawsContratcs.Infrastructure;
using CatHasPawsContratcs.ViewModels;
namespace CatHasPawsContratcs.AdapterContracts.OperationResponses;
public class ManufacturerOperationResponse : OperationResponse
{
public static ManufacturerOperationResponse OK(List<ManufacturerViewModel> data) => OK<ManufacturerOperationResponse, List<ManufacturerViewModel>>(data);
public static ManufacturerOperationResponse OK(ManufacturerViewModel data) => OK<ManufacturerOperationResponse, ManufacturerViewModel>(data);
public static ManufacturerOperationResponse NoContent() => NoContent<ManufacturerOperationResponse>();
public static ManufacturerOperationResponse NotFound(string message) => NotFound<ManufacturerOperationResponse>(message);
public static ManufacturerOperationResponse BadRequest(string message) => BadRequest<ManufacturerOperationResponse>(message);
public static ManufacturerOperationResponse InternalServerError(string message) => InternalServerError<ManufacturerOperationResponse>(message);
}

View File

@@ -0,0 +1,8 @@
namespace CatHasPawsContratcs.BindingModels;
public class ManufacturerBindingModel
{
public string? Id { get; set; }
public string? ManufacturerName { get; set; }
}

View File

@@ -4,7 +4,7 @@ namespace CatHasPawsContratcs.BusinessLogicsContracts;
public interface IPostBusinessLogicContract
{
List<PostDataModel> GetAllPosts(bool onlyActive);
List<PostDataModel> GetAllPosts();
List<PostDataModel> GetAllDataOfPost(string postId);

View File

@@ -14,6 +14,8 @@ public class ManufacturerDataModel(string id, string manufacturerName, string? p
public string? PrevPrevManufacturerName { get; private set; } = prevPrevManufacturerName;
public ManufacturerDataModel(string id, string manufacturerName) : this(id, manufacturerName, null, null) { }
public void Validate()
{
if (Id.IsEmpty())

View File

@@ -30,5 +30,8 @@ public class SaleProductDataModel(string saleId, string productId, int count, do
if (Count <= 0)
throw new ValidationException("Field Count is less than or equal to 0");
if (Price <= 0)
throw new ValidationException("Field Price is less than or equal to 0");
}
}

View File

@@ -4,7 +4,7 @@ namespace CatHasPawsContratcs.StoragesContracts;
public interface IPostStorageContract
{
List<PostDataModel> GetList(bool onlyActual = true);
List<PostDataModel> GetList();
List<PostDataModel> GetPostWithHistory(string postId);

View File

@@ -0,0 +1,12 @@
namespace CatHasPawsContratcs.ViewModels;
public class ManufacturerViewModel
{
public required string Id { get; set; }
public required string ManufacturerName { get; set; }
public string? PrevManufacturerName { get; set; }
public string? PrevPrevManufacturerName { get; set; }
}

View File

@@ -29,16 +29,11 @@ internal class PostStorageContract : IPostStorageContract
_mapper = new Mapper(config);
}
public List<PostDataModel> GetList(bool onlyActual = true)
public List<PostDataModel> GetList()
{
try
{
var query = _dbContext.Posts.AsQueryable();
if (onlyActual)
{
query = query.Where(x => x.IsActual);
}
return [.. query.Select(x => _mapper.Map<PostDataModel>(x))];
return [.. _dbContext.Posts.Select(x => _mapper.Map<PostDataModel>(x))];
}
catch (Exception ex)
{

View File

@@ -33,61 +33,53 @@ internal class PostBusinessLogicContractTests
//Arrange
var listOriginal = new List<PostDataModel>()
{
new(Guid.NewGuid().ToString(),"name 1", PostType.Assistant, 10, true, DateTime.UtcNow),
new(Guid.NewGuid().ToString(), "name 2", PostType.Assistant, 10, false, DateTime.UtcNow),
new(Guid.NewGuid().ToString(), "name 3", PostType.Assistant, 10, true, DateTime.UtcNow),
new(Guid.NewGuid().ToString(),"name 1", PostType.Assistant, 10),
new(Guid.NewGuid().ToString(), "name 2", PostType.Assistant, 10),
new(Guid.NewGuid().ToString(), "name 3", PostType.Assistant, 10),
};
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
_postStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
//Act
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
var listAll = _postBusinessLogicContract.GetAllPosts(false);
var list = _postBusinessLogicContract.GetAllPosts();
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(listAll, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(listAll, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_postStorageContract.Verify(x => x.GetList(true), Times.Once);
_postStorageContract.Verify(x => x.GetList(false), Times.Once);
_postStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllPosts_ReturnEmptyList_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns([]);
_postStorageContract.Setup(x => x.GetList()).Returns([]);
//Act
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
var listAll = _postBusinessLogicContract.GetAllPosts(false);
var list = _postBusinessLogicContract.GetAllPosts();
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(listAll, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(listAll, Has.Count.EqualTo(0));
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
});
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
}
[Test]
public void GetAllPosts_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
Assert.That(() => _postBusinessLogicContract.GetAllPosts(), Throws.TypeOf<NullListException>());
_postStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllPosts_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
Assert.That(() => _postBusinessLogicContract.GetAllPosts(), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
@@ -97,8 +89,8 @@ internal class PostBusinessLogicContractTests
var postId = Guid.NewGuid().ToString();
var listOriginal = new List<PostDataModel>()
{
new(postId, "name 1", PostType.Assistant, 10, true, DateTime.UtcNow),
new(postId, "name 2", PostType.Assistant, 10, false, DateTime.UtcNow)
new(postId, "name 1", PostType.Assistant, 10),
new(postId, "name 2", PostType.Assistant, 10)
};
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns(listOriginal);
//Act
@@ -162,7 +154,7 @@ internal class PostBusinessLogicContractTests
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new PostDataModel(id, "name", PostType.Assistant, 10, true, DateTime.UtcNow);
var record = new PostDataModel(id, "name", PostType.Assistant, 10);
_postStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _postBusinessLogicContract.GetPostByData(id);
@@ -177,7 +169,7 @@ internal class PostBusinessLogicContractTests
{
//Arrange
var postName = "name";
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Assistant, 10, true, DateTime.UtcNow);
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Assistant, 10);
_postStorageContract.Setup(x => x.GetElementByName(postName)).Returns(record);
//Act
var element = _postBusinessLogicContract.GetPostByData(postName);
@@ -233,12 +225,11 @@ internal class PostBusinessLogicContractTests
{
//Arrange
var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Supervisor, 10, true, DateTime.UtcNow.AddDays(-1));
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Supervisor, 10);
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) =>
{
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary &&
x.ChangeDate == record.ChangeDate;
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary;
});
//Act
_postBusinessLogicContract.InsertPost(record);
@@ -253,7 +244,7 @@ internal class PostBusinessLogicContractTests
//Arrange
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Supervisor, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Supervisor, 10)), Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
}
@@ -269,7 +260,7 @@ internal class PostBusinessLogicContractTests
public void InsertPost_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.Supervisor, 10, true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
Assert.That(() => _postBusinessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.Supervisor, 10)), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
}
@@ -279,7 +270,7 @@ internal class PostBusinessLogicContractTests
//Arrange
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Supervisor, 10, true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Supervisor, 10)), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
}
@@ -288,12 +279,11 @@ internal class PostBusinessLogicContractTests
{
//Arrange
var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Supervisor, 10, true, DateTime.UtcNow.AddDays(-1));
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Supervisor, 10);
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) =>
{
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary &&
x.ChangeDate == record.ChangeDate;
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary;
});
//Act
_postBusinessLogicContract.UpdatePost(record);
@@ -308,7 +298,7 @@ internal class PostBusinessLogicContractTests
//Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Supervisor, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementNotFoundException>());
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Supervisor, 10)), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
@@ -318,7 +308,7 @@ internal class PostBusinessLogicContractTests
//Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.Supervisor, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.Supervisor, 10)), Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
@@ -334,7 +324,7 @@ internal class PostBusinessLogicContractTests
public void UpdatePost_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.Supervisor, 10, true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
Assert.That(() => _postBusinessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.Supervisor, 10)), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
}
@@ -344,7 +334,7 @@ internal class PostBusinessLogicContractTests
//Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Supervisor, 10, true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Supervisor, 10)), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}

View File

@@ -186,12 +186,12 @@ internal class SalaryBusinessLogicContractTests
{
//Arrange
var workerId = Guid.NewGuid().ToString();
var saleSum = 200.0;
var saleSum = 1.2 * 5;
var postSalary = 2000.0;
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, saleSum, DiscountType.None, 0, false, [])]);
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, DiscountType.None, false, [new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, postSalary, true, DateTime.UtcNow));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, postSalary));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
var sum = 0.0;
@@ -220,13 +220,13 @@ internal class SalaryBusinessLogicContractTests
new(worker3Id, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)
};
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), worker1Id, null, 1, DiscountType.None, 0, false, []),
new SaleDataModel(Guid.NewGuid().ToString(), worker1Id, null, 1, DiscountType.None, 0, false, []),
new SaleDataModel(Guid.NewGuid().ToString(), worker2Id, null, 1, DiscountType.None, 0, false, []),
new SaleDataModel(Guid.NewGuid().ToString(), worker3Id, null, 1, DiscountType.None, 0, false, []),
new SaleDataModel(Guid.NewGuid().ToString(), worker3Id, null, 1, DiscountType.None, 0, false, [])]);
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), worker1Id, null, DiscountType.None, false, []),
new SaleDataModel(Guid.NewGuid().ToString(), worker1Id, null, DiscountType.None, false, []),
new SaleDataModel(Guid.NewGuid().ToString(), worker2Id, null, DiscountType.None, false, []),
new SaleDataModel(Guid.NewGuid().ToString(), worker3Id, null, DiscountType.None, false, []),
new SaleDataModel(Guid.NewGuid().ToString(), worker3Id, null, DiscountType.None, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, 2000, true, DateTime.UtcNow));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, 2000));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns(list);
//Act
@@ -236,7 +236,7 @@ internal class SalaryBusinessLogicContractTests
}
[Test]
public void CalculateSalaryByMounth_WithoitSalesByWorker_Test()
public void CalculateSalaryByMounth_WithoutSalesByWorker_Test()
{
//Arrange
var postSalary = 2000.0;
@@ -244,7 +244,7 @@ internal class SalaryBusinessLogicContractTests
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, postSalary, true, DateTime.UtcNow));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, postSalary));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
var sum = 0.0;
@@ -266,7 +266,7 @@ internal class SalaryBusinessLogicContractTests
//Arrange
var workerId = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, 2000, true, DateTime.UtcNow));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, 2000));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
@@ -279,7 +279,7 @@ internal class SalaryBusinessLogicContractTests
//Arrange
var workerId = Guid.NewGuid().ToString();
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, 200, DiscountType.None, 0, false, [])]);
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, DiscountType.None, false, [])]);
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
@@ -292,9 +292,9 @@ internal class SalaryBusinessLogicContractTests
//Arrange
var workerId = Guid.NewGuid().ToString();
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, 200, DiscountType.None, 0, false, [])]);
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, DiscountType.None, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, 2000, true, DateTime.UtcNow));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, 2000));
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
}
@@ -307,7 +307,7 @@ internal class SalaryBusinessLogicContractTests
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, 2000, true, DateTime.UtcNow));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, 2000));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
@@ -320,7 +320,7 @@ internal class SalaryBusinessLogicContractTests
//Arrange
var workerId = Guid.NewGuid().ToString();
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, 200, DiscountType.None, 0, false, [])]);
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, DiscountType.None, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
@@ -335,9 +335,9 @@ internal class SalaryBusinessLogicContractTests
//Arrange
var workerId = Guid.NewGuid().ToString();
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, 200, DiscountType.None, 0, false, [])]);
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, DiscountType.None, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, 2000, true, DateTime.UtcNow));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, 2000));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Throws(new StorageException(new InvalidOperationException()));
//Act&Assert

View File

@@ -34,10 +34,10 @@ internal class SaleBusinessLogicContractTests
var date = DateTime.UtcNow;
var listOriginal = new List<SaleDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false,
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DiscountType.None, false,
[new SaleProductDataModel(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(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
@@ -98,10 +98,10 @@ internal class SaleBusinessLogicContractTests
var workerId = Guid.NewGuid().ToString();
var listOriginal = new List<SaleDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false,
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DiscountType.None, false,
[new SaleProductDataModel(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(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
@@ -179,10 +179,10 @@ internal class SaleBusinessLogicContractTests
var buyerId = Guid.NewGuid().ToString();
var listOriginal = new List<SaleDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false,
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DiscountType.None, false,
[new SaleProductDataModel(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(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
@@ -260,10 +260,10 @@ internal class SaleBusinessLogicContractTests
var productId = Guid.NewGuid().ToString();
var listOriginal = new List<SaleDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false,
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DiscountType.None, false,
[new SaleProductDataModel(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(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
@@ -338,8 +338,8 @@ internal class SaleBusinessLogicContractTests
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new SaleDataModel(id, Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false,
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
var record = new SaleDataModel(id, Guid.NewGuid().ToString(), null, DiscountType.None, false,
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)]);
_saleStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _saleBusinessLogicContract.GetSaleByData(id);
@@ -389,17 +389,17 @@ internal class SaleBusinessLogicContractTests
{
//Arrange
var flag = false;
var record = new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.None, 10,
false, [new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
var record = new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DiscountType.None,
false, [new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)]);
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>()))
.Callback((SaleDataModel x) =>
{
flag = x.Id == record.Id && x.WorkerId == record.WorkerId && x.BuyerId == record.BuyerId &&
x.SaleDate == record.SaleDate && x.Sum == record.Sum && x.DiscountType == record.DiscountType &&
x.Discount == record.Discount && x.IsCancel == record.IsCancel && x.Products.Count == record.Products.Count &&
x.Products.First().ProductId == record.Products.First().ProductId &&
x.Products.First().SaleId == record.Products.First().SaleId &&
x.Products.First().Count == record.Products.First().Count;
x.Discount == record.Discount && x.IsCancel == record.IsCancel && x.Products?.Count == record.Products?.Count &&
x.Products?.First().ProductId == record.Products?.First().ProductId &&
x.Products?.First().SaleId == record.Products?.First().SaleId &&
x.Products?.First().Count == record.Products?.First().Count;
});
//Act
_saleBusinessLogicContract.InsertSale(record);
@@ -415,7 +415,7 @@ internal class SaleBusinessLogicContractTests
_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(), 10, DiscountType.None, 10, false, [new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
Guid.NewGuid().ToString(), DiscountType.None, false, [new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])), Throws.TypeOf<ElementExistsException>());
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
}
@@ -431,7 +431,7 @@ internal class SaleBusinessLogicContractTests
public void InsertSale_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.InsertSale(new SaleDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.None, 10, false, [])), Throws.TypeOf<ValidationException>());
Assert.That(() => _saleBusinessLogicContract.InsertSale(new SaleDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DiscountType.None, false, [])), Throws.TypeOf<ValidationException>());
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Never);
}
@@ -442,7 +442,7 @@ internal class SaleBusinessLogicContractTests
_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(), 10, DiscountType.None, 10, false, [new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
Guid.NewGuid().ToString(), DiscountType.None, false, [new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])), Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
}

View File

@@ -10,41 +10,41 @@ internal class PostDataModelTests
[Test]
public void IdIsNullOrEmptyTest()
{
var post = CreateDataModel(null, "name", PostType.Assistant, 10, true, DateTime.UtcNow);
var post = CreateDataModel(null, "name", PostType.Assistant, 10);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(string.Empty, "name", PostType.Assistant, 10, true, DateTime.UtcNow);
post = CreateDataModel(string.Empty, "name", PostType.Assistant, 10);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var post = CreateDataModel("id", "name", PostType.Assistant, 10, true, DateTime.UtcNow);
var post = CreateDataModel("id", "name", PostType.Assistant, 10);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostNameIsEmptyTest()
{
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Assistant, 10, true, DateTime.UtcNow);
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Assistant, 10);
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Assistant, 10, true, DateTime.UtcNow);
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Assistant, 10);
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostTypeIsNoneTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.None, 10, true, DateTime.UtcNow);
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.None, 10);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SalaryIsLessOrZeroTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, 0, true, DateTime.UtcNow);
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, 0);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, -10, true, DateTime.UtcNow);
post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Assistant, -10);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
@@ -55,9 +55,7 @@ internal class PostDataModelTests
var postName = "name";
var postType = PostType.Assistant;
var salary = 10;
var isActual = false;
var changeDate = DateTime.UtcNow.AddDays(-1);
var post = CreateDataModel(postId, postName, postType, salary, isActual, changeDate);
var post = CreateDataModel(postId, postName, postType, salary);
Assert.That(() => post.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
@@ -65,11 +63,9 @@ internal class PostDataModelTests
Assert.That(post.PostName, Is.EqualTo(postName));
Assert.That(post.PostType, Is.EqualTo(postType));
Assert.That(post.Salary, Is.EqualTo(salary));
Assert.That(post.IsActual, Is.EqualTo(isActual));
Assert.That(post.ChangeDate, Is.EqualTo(changeDate));
});
}
private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, double salary, bool isActual, DateTime changeDate) =>
new(id, postName, postType, salary, isActual, changeDate);
private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, double salary) =>
new(id, postName, postType, salary);
}

View File

@@ -10,88 +10,120 @@ internal class SaleDataModelTests
[Test]
public void IdIsNullOrEmptyTest()
{
var sale = CreateDataModel(null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
var sale = CreateDataModel(null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DiscountType.OnSale, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
sale = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DiscountType.OnSale, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var sale = CreateDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
var sale = CreateDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DiscountType.OnSale, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkerIdIsNullOrEmptyTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), null, Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
var sale = CreateDataModel(Guid.NewGuid().ToString(), null, Guid.NewGuid().ToString(), DiscountType.OnSale, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
sale = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), DiscountType.OnSale, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkerIdIsNotGuidTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), "workerId", Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
var sale = CreateDataModel(Guid.NewGuid().ToString(), "workerId", Guid.NewGuid().ToString(), DiscountType.OnSale, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void BuyerIdIsNotGuidTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "buyerId", 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SumIsLessOrZeroTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, DiscountType.OnSale, 10, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10, DiscountType.OnSale, 10, false, CreateSubDataModel());
var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "buyerId", DiscountType.OnSale, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductsIsNullOrEmptyTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, null);
var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DiscountType.OnSale, false, null);
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, []);
sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DiscountType.OnSale, false, []);
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CalcSumAndDiscountTest()
{
var saleId = Guid.NewGuid().ToString();
var workerId = Guid.NewGuid().ToString();
var buyerId = Guid.NewGuid().ToString();
var products = new List<SaleProductDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 2, 1.1),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1, 1.3)
};
var isCancel = false;
var totalSum = products.Sum(x => x.Price * x.Count);
var saleNone = CreateDataModel(saleId, workerId, buyerId, DiscountType.None, isCancel, products);
Assert.Multiple(() =>
{
Assert.That(saleNone.Sum, Is.EqualTo(totalSum));
Assert.That(saleNone.Discount, Is.EqualTo(0));
});
var saleOnSale = CreateDataModel(saleId, workerId, buyerId, DiscountType.OnSale, isCancel, products);
Assert.Multiple(() =>
{
Assert.That(saleOnSale.Sum, Is.EqualTo(totalSum));
Assert.That(saleOnSale.Discount, Is.EqualTo(totalSum * 0.1));
});
var saleRegularCustomer = CreateDataModel(saleId, workerId, buyerId, DiscountType.RegularCustomer, isCancel, products);
Assert.Multiple(() =>
{
Assert.That(saleRegularCustomer.Sum, Is.EqualTo(totalSum));
Assert.That(saleRegularCustomer.Discount, Is.EqualTo(totalSum * 0.5));
});
var saleCertificate = CreateDataModel(saleId, workerId, buyerId, DiscountType.Certificate, isCancel, products);
Assert.Multiple(() =>
{
Assert.That(saleCertificate.Sum, Is.EqualTo(totalSum));
Assert.That(saleCertificate.Discount, Is.EqualTo(totalSum * 0.3));
});
var saleMulty = CreateDataModel(saleId, workerId, buyerId, DiscountType.Certificate | DiscountType.RegularCustomer, isCancel, products);
Assert.Multiple(() =>
{
Assert.That(saleMulty.Sum, Is.EqualTo(totalSum));
Assert.That(saleMulty.Discount, Is.EqualTo(totalSum * 0.8));
});
}
[Test]
public void AllFieldsIsCorrectTest()
{
var saleId = Guid.NewGuid().ToString();
var workerId = Guid.NewGuid().ToString();
var buyerId = Guid.NewGuid().ToString();
var sum = 10;
var discountType = DiscountType.Certificate;
var discount = 1;
var isCancel = true;
var products = CreateSubDataModel();
var sale = CreateDataModel(saleId, workerId, buyerId, sum, discountType, discount, isCancel, products);
var sale = CreateDataModel(saleId, workerId, buyerId, discountType, isCancel, products);
Assert.That(() => sale.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(sale.Id, Is.EqualTo(saleId));
Assert.That(sale.WorkerId, Is.EqualTo(workerId));
Assert.That(sale.BuyerId, Is.EqualTo(buyerId));
Assert.That(sale.Sum, Is.EqualTo(sum));
Assert.That(sale.DiscountType, Is.EqualTo(discountType));
Assert.That(sale.Discount, Is.EqualTo(discount));
Assert.That(sale.IsCancel, Is.EqualTo(isCancel));
Assert.That(sale.Products, Is.EquivalentTo(products));
});
}
private static SaleDataModel CreateDataModel(string? id, string? workerId, string? buyerId, double sum, DiscountType discountType, double discount, bool isCancel, List<SaleProductDataModel>? products) =>
new(id, workerId, buyerId, sum, discountType, discount, isCancel, products);
private static SaleDataModel CreateDataModel(string? id, string? workerId, string? buyerId, DiscountType discountType, bool isCancel, List<SaleProductDataModel>? products) =>
new(id, workerId, buyerId, discountType, isCancel, products);
private static List<SaleProductDataModel> CreateSubDataModel()
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1, 1.1)];
}

View File

@@ -9,41 +9,50 @@ internal class SaleProductDataModelTests
[Test]
public void SaleIdIsNullOrEmptyTest()
{
var saleProduct = CreateDataModel(null, Guid.NewGuid().ToString(), 10);
var saleProduct = CreateDataModel(null, Guid.NewGuid().ToString(), 10, 1.1);
Assert.That(() => saleProduct.Validate(), Throws.TypeOf<ValidationException>());
saleProduct = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
saleProduct = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10, 1.1);
Assert.That(() => saleProduct.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SaleIdIsNotGuidTest()
{
var saleProduct = CreateDataModel("saleId", Guid.NewGuid().ToString(), 10);
var saleProduct = CreateDataModel("saleId", Guid.NewGuid().ToString(), 10, 1.1);
Assert.That(() => saleProduct.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductIdIsNullOrEmptyTest()
{
var saleProduct = CreateDataModel(Guid.NewGuid().ToString(), null, 10);
var saleProduct = CreateDataModel(Guid.NewGuid().ToString(), null, 10, 1.1);
Assert.That(() => saleProduct.Validate(), Throws.TypeOf<ValidationException>());
saleProduct = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
saleProduct = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10, 1.1);
Assert.That(() => saleProduct.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductIdIsNotGuidTest()
{
var saleProduct = CreateDataModel(Guid.NewGuid().ToString(), "productId", 10);
var saleProduct = CreateDataModel(Guid.NewGuid().ToString(), "productId", 10, 1.1);
Assert.That(() => saleProduct.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CountIsLessOrZeroTest()
{
var saleProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
var saleProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, 1.1);
Assert.That(() => saleProduct.Validate(), Throws.TypeOf<ValidationException>());
saleProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
saleProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10, 1.1);
Assert.That(() => saleProduct.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PriceIsLessOrZeroTest()
{
var saleProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1, 0);
Assert.That(() => saleProduct.Validate(), Throws.TypeOf<ValidationException>());
saleProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1, -10);
Assert.That(() => saleProduct.Validate(), Throws.TypeOf<ValidationException>());
}
@@ -53,16 +62,18 @@ internal class SaleProductDataModelTests
var saleId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var count = 10;
var saleProduct = CreateDataModel(saleId, productId, count);
var price = 1.2;
var saleProduct = CreateDataModel(saleId, productId, count, price);
Assert.That(() => saleProduct.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(saleProduct.SaleId, Is.EqualTo(saleId));
Assert.That(saleProduct.ProductId, Is.EqualTo(productId));
Assert.That(saleProduct.Count, Is.EqualTo(count));
Assert.That(saleProduct.Price, Is.EqualTo(price));
});
}
private static SaleProductDataModel CreateDataModel(string? saleId, string? productId, int count) =>
new(saleId, productId, count);
private static SaleProductDataModel CreateDataModel(string? saleId, string? productId, int count, double price) =>
new(saleId, productId, count, price);
}

View File

@@ -0,0 +1,357 @@
using CatHasPawsContratcs.BindingModels;
using CatHasPawsContratcs.ViewModels;
using CatHasPawsDatabase.Models;
using CatHasPawsTests.Infrastructure;
using System.Net;
namespace CatHasPawsTests.WebApiControllersTests;
[TestFixture]
internal class ManufacturerControllerTests : BaseWebApiControllerTest
{
[TearDown]
public void TearDown()
{
CatHasPawsDbContext.RemoveProductsFromDatabase();
CatHasPawsDbContext.RemoveManufacturersFromDatabase();
}
[Test]
public async Task GetList_WhenHaveRecords_ShouldSuccess_Test()
{
//Arrange
var manufacturer = CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn(manufacturerName: "name 1");
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn(manufacturerName: "name 2");
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn(manufacturerName: "name 3");
//Act
var response = await HttpClient.GetAsync("/api/manufacturers");
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
var data = await GetModelFromResponseAsync<List<ManufacturerViewModel>>(response);
Assert.Multiple(() =>
{
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(3));
});
AssertElement(data.First(x => x.Id == manufacturer.Id), manufacturer);
}
[Test]
public async Task GetList_WhenNoRecords_ShouldSuccess_Test()
{
//Act
var response = await HttpClient.GetAsync("/api/manufacturers");
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
var data = await GetModelFromResponseAsync<List<ManufacturerViewModel>>(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 manufacturer = CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn();
//Act
var response = await HttpClient.GetAsync($"/api/manufacturers/{manufacturer.Id}");
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
AssertElement(await GetModelFromResponseAsync<ManufacturerViewModel>(response), manufacturer);
}
[Test]
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
{
//Arrange
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn();
//Act
var response = await HttpClient.GetAsync($"/api/manufacturers/{Guid.NewGuid()}");
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
}
[Test]
public async Task GetElement_ByName_WhenHaveRecord_ShouldSuccess_Test()
{
//Arrange
var manufacturer = CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn();
//Act
var response = await HttpClient.GetAsync($"/api/manufacturers/{manufacturer.ManufacturerName}");
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
AssertElement(await GetModelFromResponseAsync<ManufacturerViewModel>(response), manufacturer);
}
[Test]
public async Task GetElement_ByName_WhenNoRecord_ShouldNotFound_Test()
{
//Arrange
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn();
//Act
var response = await HttpClient.GetAsync($"/api/manufacturers/New%20Name");
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
}
[Test]
public async Task GetElement_ByOldName_WhenHaveRecord_ShouldSuccess_Test()
{
//Arrange
var oldName = "old name";
var manufacturer = CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn(prevManufacturerName: oldName);
//Act
var response = await HttpClient.GetAsync($"/api/manufacturers/{oldName}");
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
AssertElement(await GetModelFromResponseAsync<ManufacturerViewModel>(response), manufacturer);
}
[Test]
public async Task Post_ShouldSuccess_Test()
{
//Arrange
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn();
var manufacturerModel = CreateModel();
//Act
var response = await HttpClient.PostAsync($"/api/manufacturers", MakeContent(manufacturerModel));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
AssertElement(CatHasPawsDbContext.GetManufacturerFromDatabase(manufacturerModel.Id!), manufacturerModel);
}
[Test]
public async Task Post_WhenHaveRecordWithSameId_ShouldBadRequest_Test()
{
//Arrange
var manufacturerModel = CreateModel();
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn(manufacturerModel.Id);
//Act
var response = await HttpClient.PostAsync($"/api/manufacturers", MakeContent(manufacturerModel));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
[Test]
public async Task Post_WhenHaveRecordWithSameName_ShouldBadRequest_Test()
{
//Arrange
var manufacturerModel = CreateModel(manufacturerName: "unique name");
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn(manufacturerName: manufacturerModel.ManufacturerName!);
//Act
var response = await HttpClient.PostAsync($"/api/manufacturers", MakeContent(manufacturerModel));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
[Test]
public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test()
{
//Arrange
var manufacturerModelWithIdIncorrect = new ManufacturerBindingModel { Id = "Id", ManufacturerName = "name" };
var manufacturerModelWithNameIncorrect = new ManufacturerBindingModel { Id = Guid.NewGuid().ToString(), ManufacturerName = string.Empty };
//Act
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/manufacturers", MakeContent(manufacturerModelWithIdIncorrect));
var responseWithNameIncorrect = await HttpClient.PostAsync($"/api/manufacturers", MakeContent(manufacturerModelWithNameIncorrect));
//Assert
Assert.Multiple(() =>
{
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Name is incorrect");
});
}
[Test]
public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test()
{
//Act
var response = await HttpClient.PostAsync($"/api/manufacturers", 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/manufacturers", MakeContent(new { Data = "test", Position = 10 }));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
[Test]
public async Task Put_ShouldSuccess_Test()
{
//Arrange
var manufacturerModel = CreateModel();
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn(manufacturerModel.Id);
//Act
var response = await HttpClient.PutAsync($"/api/manufacturers", MakeContent(manufacturerModel));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
CatHasPawsDbContext.ChangeTracker.Clear();
AssertElement(CatHasPawsDbContext.GetManufacturerFromDatabase(manufacturerModel.Id!), manufacturerModel);
}
[Test]
public async Task Put_CheckPrevNames_ShouldSuccess_Test()
{
//Arrange
var manufacturerModel1 = CreateModel(manufacturerName: "name 1");
var manufacturerModel2 = CreateModel(id: manufacturerModel1.Id, manufacturerName: "name 2");
var manufacturerModel3 = CreateModel(id: manufacturerModel1.Id, manufacturerName: "name 3");
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn(manufacturerModel1.Id);
//Act
await HttpClient.PutAsync($"/api/manufacturers", MakeContent(manufacturerModel1));
await HttpClient.PutAsync($"/api/manufacturers", MakeContent(manufacturerModel2));
await HttpClient.PutAsync($"/api/manufacturers", MakeContent(manufacturerModel3));
//Assert
CatHasPawsDbContext.ChangeTracker.Clear();
var entityModel = CatHasPawsDbContext.GetManufacturerFromDatabase(manufacturerModel1.Id!);
Assert.That(entityModel, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(entityModel.ManufacturerName, Is.EqualTo(manufacturerModel3.ManufacturerName));
Assert.That(entityModel.PrevManufacturerName, Is.EqualTo(manufacturerModel2.ManufacturerName));
Assert.That(entityModel.PrevPrevManufacturerName, Is.EqualTo(manufacturerModel1.ManufacturerName));
});
}
[Test]
public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test()
{
//Arrange
var manufacturerModel = CreateModel();
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn();
//Act
var response = await HttpClient.PutAsync($"/api/manufacturers", MakeContent(manufacturerModel));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
[Test]
public async Task Put_WhenHaveRecordWithSameName_ShouldBadRequest_Test()
{
//Arrange
var manufacturerModel = CreateModel(manufacturerName: "unique name");
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn(manufacturerModel.Id);
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn(manufacturerName: manufacturerModel.ManufacturerName!);
//Act
var response = await HttpClient.PostAsync($"/api/manufacturers", MakeContent(manufacturerModel));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
[Test]
public async Task Put_WhenDataIsIncorrect_ShouldBadRequest_Test()
{
//Arrange
var manufacturerModelWithIdIncorrect = new ManufacturerBindingModel { Id = "Id", ManufacturerName = "name" };
var manufacturerModelWithNameIncorrect = new ManufacturerBindingModel { Id = Guid.NewGuid().ToString(), ManufacturerName = string.Empty };
//Act
var responseWithIdIncorrect = await HttpClient.PutAsync($"/api/manufacturers", MakeContent(manufacturerModelWithIdIncorrect));
var responseWithNameIncorrect = await HttpClient.PutAsync($"/api/manufacturers", MakeContent(manufacturerModelWithNameIncorrect));
//Assert
Assert.Multiple(() =>
{
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Name is incorrect");
});
}
[Test]
public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test()
{
//Act
var response = await HttpClient.PutAsync($"/api/manufacturers", 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/manufacturers", MakeContent(new { Data = "test", Position = 10 }));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
[Test]
public async Task Delete_ShouldSuccess_Test()
{
//Arrange
var manufacturerId = Guid.NewGuid().ToString();
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn(manufacturerId);
//Act
var response = await HttpClient.DeleteAsync($"/api/manufacturers/{manufacturerId}");
//Assert
Assert.Multiple(() =>
{
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
Assert.That(CatHasPawsDbContext.GetManufacturerFromDatabase(manufacturerId), Is.Null);
});
}
[Test]
public async Task Delete_WhenHaveProducts_ShouldSuccess_Test()
{
//Arrange
var manufacturerId = CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn().Id;
CatHasPawsDbContext.InsertProductToDatabaseAndReturn(manufacturerId);
//Act
var response = await HttpClient.DeleteAsync($"/api/manufacturers/{manufacturerId}");
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
[Test]
public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test()
{
//Arrange
CatHasPawsDbContext.InsertManufacturerToDatabaseAndReturn();
//Act
var response = await HttpClient.DeleteAsync($"/api/manufacturers/{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/manufacturers/id");
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
private static void AssertElement(ManufacturerViewModel? actual, Manufacturer expected)
{
Assert.That(actual, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(actual.Id, Is.EqualTo(expected.Id));
Assert.That(actual.ManufacturerName, Is.EqualTo(expected.ManufacturerName));
Assert.That(actual.PrevManufacturerName, Is.EqualTo(expected.PrevManufacturerName));
Assert.That(actual.PrevPrevManufacturerName, Is.EqualTo(expected.PrevPrevManufacturerName));
});
}
private static ManufacturerBindingModel CreateModel(string? id = null, string manufacturerName = "name")
=> new()
{
Id = id ?? Guid.NewGuid().ToString(),
ManufacturerName = manufacturerName
};
private static void AssertElement(Manufacturer? actual, ManufacturerBindingModel expected)
{
Assert.That(actual, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(actual.Id, Is.EqualTo(expected.Id));
Assert.That(actual.ManufacturerName, Is.EqualTo(expected.ManufacturerName));
});
}
}

View File

@@ -0,0 +1,189 @@
using AutoMapper;
using CatHasPawsContratcs.AdapterContracts;
using CatHasPawsContratcs.AdapterContracts.OperationResponses;
using CatHasPawsContratcs.BindingModels;
using CatHasPawsContratcs.BusinessLogicsContracts;
using CatHasPawsContratcs.DataModels;
using CatHasPawsContratcs.Exceptions;
using CatHasPawsContratcs.ViewModels;
namespace CatHasPawsWebApi.Adapters;
public class ManufacturerAdapter : IManufacturerAdapter
{
private readonly IManufacturerBusinessLogicContract _manufacturerBusinessLogicContract;
private readonly ILogger _logger;
private readonly Mapper _mapper;
public ManufacturerAdapter(IManufacturerBusinessLogicContract manufacturerBusinessLogicContract, ILogger<ManufacturerAdapter> logger)
{
_manufacturerBusinessLogicContract = manufacturerBusinessLogicContract;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ManufacturerBindingModel, ManufacturerDataModel>();
cfg.CreateMap<ManufacturerDataModel, ManufacturerViewModel>();
});
_mapper = new Mapper(config);
}
public ManufacturerOperationResponse GetList()
{
try
{
return ManufacturerOperationResponse.OK([.. _manufacturerBusinessLogicContract.GetAllManufacturers().Select(x => _mapper.Map<ManufacturerViewModel>(x))]);
}
catch (NullListException)
{
_logger.LogError("NullListException");
return ManufacturerOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ManufacturerOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ManufacturerOperationResponse.InternalServerError(ex.Message);
}
}
public ManufacturerOperationResponse GetElement(string data)
{
try
{
return ManufacturerOperationResponse.OK(_mapper.Map<ManufacturerViewModel>(_manufacturerBusinessLogicContract.GetManufacturerByData(data)));
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return ManufacturerOperationResponse.BadRequest("Data is empty");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return ManufacturerOperationResponse.NotFound($"Not found element by data {data}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ManufacturerOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ManufacturerOperationResponse.InternalServerError(ex.Message);
}
}
public ManufacturerOperationResponse RegisterManufacturer(ManufacturerBindingModel manufacturerModel)
{
try
{
_manufacturerBusinessLogicContract.InsertManufacturer(_mapper.Map<ManufacturerDataModel>(manufacturerModel));
return ManufacturerOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return ManufacturerOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return ManufacturerOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return ManufacturerOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ManufacturerOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ManufacturerOperationResponse.InternalServerError(ex.Message);
}
}
public ManufacturerOperationResponse ChangeManufacturerInfo(ManufacturerBindingModel manufacturerModel)
{
try
{
_manufacturerBusinessLogicContract.UpdateManufacturer(_mapper.Map<ManufacturerDataModel>(manufacturerModel));
return ManufacturerOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return ManufacturerOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return ManufacturerOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return ManufacturerOperationResponse.BadRequest($"Not found element by Id {manufacturerModel.Id}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return ManufacturerOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ManufacturerOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ManufacturerOperationResponse.InternalServerError(ex.Message);
}
}
public ManufacturerOperationResponse RemoveManufacturer(string id)
{
try
{
_manufacturerBusinessLogicContract.DeleteManufacturer(id);
return ManufacturerOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return ManufacturerOperationResponse.BadRequest("Id is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return ManufacturerOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return ManufacturerOperationResponse.BadRequest($"Not found element by id: {id}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ManufacturerOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ManufacturerOperationResponse.InternalServerError(ex.Message);
}
}
}

View File

@@ -0,0 +1,45 @@
using CatHasPawsContratcs.AdapterContracts;
using CatHasPawsContratcs.BindingModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace CatHasPawsWebApi.Controllers;
[Authorize]
[Route("api/[controller]")]
[ApiController]
[Produces("application/json")]
public class ManufacturersController(IManufacturerAdapter adapter) : ControllerBase
{
private readonly IManufacturerAdapter _adapter = adapter;
[HttpGet]
public IActionResult GetAllRecords()
{
return _adapter.GetList().GetResponse(Request, Response);
}
[HttpGet("{data}")]
public IActionResult GetRecord(string data)
{
return _adapter.GetElement(data).GetResponse(Request, Response);
}
[HttpPost]
public IActionResult Register([FromBody] ManufacturerBindingModel model)
{
return _adapter.RegisterManufacturer(model).GetResponse(Request, Response);
}
[HttpPut]
public IActionResult ChangeInfo([FromBody] ManufacturerBindingModel model)
{
return _adapter.ChangeManufacturerInfo(model).GetResponse(Request, Response);
}
[HttpDelete("{id}")]
public IActionResult Delete(string id)
{
return _adapter.RemoveManufacturer(id).GetResponse(Request, Response);
}
}

View File

@@ -68,6 +68,7 @@ builder.Services.AddTransient<ISaleStorageContract, SaleStorageContract>();
builder.Services.AddTransient<IWorkerStorageContract, WorkerStorageContract>();
builder.Services.AddTransient<IBuyerAdapter, BuyerAdapter>();
builder.Services.AddTransient<IManufacturerAdapter, ManufacturerAdapter>();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();