From d14fa74e384d38f65d9ea6fcc973407056e6871a Mon Sep 17 00:00:00 2001 From: Adelina888 Date: Thu, 20 Feb 2025 20:07:10 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=D1=8B=20=D0=B8=20=D0=B8=D1=81=D0=BA?= =?UTF-8?q?=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PipingHot/PipingHot.sln | 6 +++ .../PipingHot/DataModels/PostDataModel.cs | 11 ++--- .../Exceptions/DateTimeExtensions.cs | 15 +++++++ .../Exceptions/ElementExistsException.cs | 18 +++++++++ .../Exceptions/ElementNotFoundException.cs | 16 ++++++++ .../Exceptions/IncorrectDatesException.cs | 14 +++++++ .../PipingHot/Exceptions/NullListException.cs | 12 ++++++ .../PipingHot/Exceptions/StorageException.cs | 13 ++++++ .../OrderBusinessLogicContract.cs | 6 ++- .../PostBusinessLogicContract.cs | 8 ++-- .../ProductBusinessLogicContract.cs | 6 ++- .../RestaurantBusinessLogicContract.cs | 4 +- .../SalaryBusinessLogicContract.cs | 7 +++- .../WorkerBusinessLogicContract.cs | 6 ++- .../PipingHotBusinessLogic.csproj | 13 ++++++ .../RestaurantBusinessLogicsContractsTests.cs | 11 +++++ .../DataModelsTests/PostDataModelTests.cs | 40 +++++-------------- 17 files changed, 157 insertions(+), 49 deletions(-) create mode 100644 PipingHot/PipingHot/Exceptions/DateTimeExtensions.cs create mode 100644 PipingHot/PipingHot/Exceptions/ElementExistsException.cs create mode 100644 PipingHot/PipingHot/Exceptions/ElementNotFoundException.cs create mode 100644 PipingHot/PipingHot/Exceptions/IncorrectDatesException.cs create mode 100644 PipingHot/PipingHot/Exceptions/NullListException.cs create mode 100644 PipingHot/PipingHot/Exceptions/StorageException.cs create mode 100644 PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs diff --git a/PipingHot/PipingHot.sln b/PipingHot/PipingHot.sln index 7a54cdd..cfb03a4 100644 --- a/PipingHot/PipingHot.sln +++ b/PipingHot/PipingHot.sln @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PipingHotContrast", "Piping EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PipingHotTests", "PipingHotTests\PipingHotTests.csproj", "{2269D181-1994-4BB3-A024-611BB25239AE}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PipingHotBusinessLogic", "PipingHotBusinessLogic\PipingHotBusinessLogic.csproj", "{808448EA-2CE2-474C-9F95-F4ACBF170592}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {2269D181-1994-4BB3-A024-611BB25239AE}.Debug|Any CPU.Build.0 = Debug|Any CPU {2269D181-1994-4BB3-A024-611BB25239AE}.Release|Any CPU.ActiveCfg = Release|Any CPU {2269D181-1994-4BB3-A024-611BB25239AE}.Release|Any CPU.Build.0 = Release|Any CPU + {808448EA-2CE2-474C-9F95-F4ACBF170592}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {808448EA-2CE2-474C-9F95-F4ACBF170592}.Debug|Any CPU.Build.0 = Debug|Any CPU + {808448EA-2CE2-474C-9F95-F4ACBF170592}.Release|Any CPU.ActiveCfg = Release|Any CPU + {808448EA-2CE2-474C-9F95-F4ACBF170592}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PipingHot/PipingHot/DataModels/PostDataModel.cs b/PipingHot/PipingHot/DataModels/PostDataModel.cs index bf8e295..da223e1 100644 --- a/PipingHot/PipingHot/DataModels/PostDataModel.cs +++ b/PipingHot/PipingHot/DataModels/PostDataModel.cs @@ -11,10 +11,9 @@ using System.Threading.Tasks; namespace PipingHotContrast.DataModels; -public class PostDataModel(string id, string postId, string postName, PostType postType, double salary, bool isActual, DateTime changeDate): IValidation +public class PostDataModel(string id, string postName, PostType postType, double salary, bool isActual, DateTime changeDate): IValidation { - public string Id { get; private set; } = id; - public string PostId { get; private set; } = postId; + public string Id { get; private set; } = id; public string PostName { get; private set; } = postName; public PostType PostType { get; private set; } = postType; public double Salary { get; private set; } = salary; @@ -26,11 +25,7 @@ public class PostDataModel(string id, string postId, string postName, PostType p if (Id.IsEmpty()) throw new ValidationException("Field Id is empty"); if (!Id.IsGuid()) - throw new ValidationException("The value in the field Id is not a unique identifier"); - if (PostId.IsEmpty()) - throw new ValidationException("Field PostId is empty"); - if (!PostId.IsGuid()) - throw new ValidationException("The value in the field PostId is not a unique identifier"); + throw new ValidationException("The value in the field Id is not a unique identifier"); if (PostName.IsEmpty()) throw new ValidationException("Field PostName is empty"); if (PostType==PostType.None) diff --git a/PipingHot/PipingHot/Exceptions/DateTimeExtensions.cs b/PipingHot/PipingHot/Exceptions/DateTimeExtensions.cs new file mode 100644 index 0000000..4067cfb --- /dev/null +++ b/PipingHot/PipingHot/Exceptions/DateTimeExtensions.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.Exceptions; + +public static class DateTimeExtensions +{ + public static bool IsDateNotOlder(this DateTime date, DateTime olderDate) + { + return date >= olderDate; + } +} diff --git a/PipingHot/PipingHot/Exceptions/ElementExistsException.cs b/PipingHot/PipingHot/Exceptions/ElementExistsException.cs new file mode 100644 index 0000000..6d3a301 --- /dev/null +++ b/PipingHot/PipingHot/Exceptions/ElementExistsException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.Exceptions; + +public class ElementExistsException:Exception +{ + public string ParamName { get; private set; } + public string ParamValue { get; private set; } + public ElementExistsException(string paramName, string paramValue) :base($"There is already an element with value{paramValue} of parameter { paramName}") + { + ParamName = paramName; + ParamValue = paramValue; + } +} diff --git a/PipingHot/PipingHot/Exceptions/ElementNotFoundException.cs b/PipingHot/PipingHot/Exceptions/ElementNotFoundException.cs new file mode 100644 index 0000000..888dda5 --- /dev/null +++ b/PipingHot/PipingHot/Exceptions/ElementNotFoundException.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.Exceptions; + +public class ElementNotFoundException: Exception +{ + public string Value { get; private set; } + public ElementNotFoundException(string value) : base($"Element not found at value = { value}") + { + Value = value; + } +} diff --git a/PipingHot/PipingHot/Exceptions/IncorrectDatesException.cs b/PipingHot/PipingHot/Exceptions/IncorrectDatesException.cs new file mode 100644 index 0000000..0543e20 --- /dev/null +++ b/PipingHot/PipingHot/Exceptions/IncorrectDatesException.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace PipingHotContrast.Exceptions; + +public class IncorrectDatesException:Exception +{ + public IncorrectDatesException(DateTime start, DateTime end) : base($"The end date must be later than the start date..StartDate: { start: dd.MM.YYYY}. EndDate: {end:dd.MM.YYYY}") { } + +} diff --git a/PipingHot/PipingHot/Exceptions/NullListException.cs b/PipingHot/PipingHot/Exceptions/NullListException.cs new file mode 100644 index 0000000..c62528b --- /dev/null +++ b/PipingHot/PipingHot/Exceptions/NullListException.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.Exceptions; + +public class NullListException:Exception +{ + public NullListException() : base("The returned list is null") { } +} diff --git a/PipingHot/PipingHot/Exceptions/StorageException.cs b/PipingHot/PipingHot/Exceptions/StorageException.cs new file mode 100644 index 0000000..dc5bbb1 --- /dev/null +++ b/PipingHot/PipingHot/Exceptions/StorageException.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.Exceptions; + +public class StorageException:Exception +{ + public StorageException(Exception ex) : base($"Error while working instorage: { ex.Message}", ex) { } + +} diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs index b21d83e..375e30b 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs @@ -1,5 +1,7 @@ -using PipingHotContrast.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; +using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; @@ -8,7 +10,7 @@ using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; -internal class OrderBusinessLogicContract : IOrderBusinessLogicContract +internal class OrderBusinessLogicContract(IOrderStorageContract orderStorageContract, ILogger logger) : IOrderBusinessLogicContract { public List GetAllOrdersByPeriod(DateTime fromDate, DateTime toDate) { diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs index 1a1cf58..7207cb4 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -1,6 +1,8 @@ -using PipingHotContrast.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; using PipingHotContrast.Enums; +using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; @@ -9,7 +11,7 @@ using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; -internal class PostBusinessLogicContract : IPostBusinessLogicContract +internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract { public List GetAllPosts(bool onlyActive) { @@ -22,7 +24,7 @@ internal class PostBusinessLogicContract : IPostBusinessLogicContract } public PostDataModel GetPostByData(string data) { - return new("", "","", PostType.None, 0, true, DateTime.UtcNow); + return new("", "", PostType.None, 0, true, DateTime.UtcNow); } public void InsertPost(PostDataModel postDataModel) diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs index 5bc0d5a..6ef78e7 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs @@ -1,6 +1,8 @@ -using PipingHotContrast.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; using PipingHotContrast.Enums; +using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; @@ -9,7 +11,7 @@ using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; -internal class ProductBusinessLogicContract : IProductBusinessLogicContract +internal class ProductBusinessLogicContract(IProductStorageContract productStorageContract, ILogger logger) : IProductBusinessLogicContract { public List GetAllProducts(bool onlyActive = true) { diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs index 71313ef..098ee70 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs @@ -1,6 +1,8 @@  +using Microsoft.Extensions.Logging; using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; +using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; @@ -9,7 +11,7 @@ using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; -internal class RestaurantBusinessLogicContract : IRestaurantBusinessLogicContract +internal class RestaurantBusinessLogicContract(IRestaurantStorageContract restaurantStorageContract, ILogger logger) : IRestaurantBusinessLogicContract { public List GetAllRestaurants() { diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs index bdd6b01..3c85957 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs @@ -1,5 +1,7 @@ -using PipingHotContrast.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; +using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; @@ -8,7 +10,8 @@ using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; -internal class SalaryBusinessLogicContract : ISalaryBusinessLogicContract +internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, + IOrderStorageContract orderStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract { public List GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate) { diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs index 3aab982..f1632ef 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs @@ -1,5 +1,7 @@ -using PipingHotContrast.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; +using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; @@ -8,7 +10,7 @@ using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; -internal class WorkerBusinessLogicContract : IWorkerBusinessLogicContract +internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageContract, ILogger logger) : IWorkerBusinessLogicContract { public List GetAllWorkers(bool onlyActive = true) { diff --git a/PipingHot/PipingHotBusinessLogic/PipingHotBusinessLogic.csproj b/PipingHot/PipingHotBusinessLogic/PipingHotBusinessLogic.csproj index ac07c5b..80e2698 100644 --- a/PipingHot/PipingHotBusinessLogic/PipingHotBusinessLogic.csproj +++ b/PipingHot/PipingHotBusinessLogic/PipingHotBusinessLogic.csproj @@ -6,5 +6,18 @@ true enable + + + + + + + + + + + + + diff --git a/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs new file mode 100644 index 0000000..0e7d344 --- /dev/null +++ b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotTests.BusinessLogicsContractsTests; + +internal class RestaurantBusinessLogicsContractsTests +{ +} diff --git a/PipingHot/PipingHotTests/DataModelsTests/PostDataModelTests.cs b/PipingHot/PipingHotTests/DataModelsTests/PostDataModelTests.cs index 7a26814..c71b5b6 100644 --- a/PipingHot/PipingHotTests/DataModelsTests/PostDataModelTests.cs +++ b/PipingHot/PipingHotTests/DataModelsTests/PostDataModelTests.cs @@ -20,57 +20,41 @@ internal class PostDataModelTests [Test] public void IdIsNullOrEmptyTest() { - var post = CreateDataModel(null, Guid.NewGuid().ToString(), "name", PostType.Operator, 10, true, DateTime.UtcNow); + var post = CreateDataModel(null, "name", PostType.Operator, 10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); - post = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "name", PostType.Operator, 10, true, DateTime.UtcNow); + post = CreateDataModel(string.Empty, "name", PostType.Operator, 10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); } [Test] public void IdIsNotGuidTest() { - var post = CreateDataModel("id", Guid.NewGuid().ToString(), "name", PostType.Operator, 10, true, DateTime.UtcNow); - Assert.That(() => post.Validate(), Throws.TypeOf()); - } - - [Test] - public void PostIdIsNullEmptyTest() - { - var post = CreateDataModel(Guid.NewGuid().ToString(), null, "name", PostType.Operator, 10, true, DateTime.UtcNow); - Assert.That(() => post.Validate(), Throws.TypeOf()); - post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "name", PostType.Operator, 10, true, DateTime.UtcNow); - Assert.That(() => post.Validate(), Throws.TypeOf()); - } - - [Test] - public void PostIdIsNotGuidTest() - { - var post = CreateDataModel(Guid.NewGuid().ToString(), "postId", "name", PostType.Operator, 10, true, DateTime.UtcNow); + var post = CreateDataModel("id", "name", PostType.Operator, 10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); } [Test] public void PostNameIsEmptyTest()//? { - var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, PostType.Operator, 10, true, DateTime.UtcNow); + var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Operator, 10, true, DateTime.UtcNow); Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); - manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), string.Empty, PostType.Operator, 10, true, DateTime.UtcNow); + manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Operator, 10, true, DateTime.UtcNow); Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); } [Test] public void PostTypeIsNoneTest() { - var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.None, 10, true, DateTime.UtcNow); + var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.None, 10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); } [Test] public void SalaryIsLessOrZeroTest() { - var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.Operator, 0, true, DateTime.UtcNow); + var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Operator, 0, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); - post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.Operator, -10, true, DateTime.UtcNow); + post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Operator, -10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); } @@ -78,18 +62,16 @@ internal class PostDataModelTests public void AllFieldsIsCorrectTest() { var postId = Guid.NewGuid().ToString(); - var postPostId = Guid.NewGuid().ToString(); var postName = "name"; var postType = PostType.Operator; var salary = 10; var isActual = false; var changeDate = DateTime.UtcNow.AddDays(-1); - var post = CreateDataModel(postId, postPostId, postName, postType, salary, isActual, changeDate); + var post = CreateDataModel(postId, postName, postType, salary, isActual, changeDate); Assert.That(() => post.Validate(), Throws.Nothing); Assert.Multiple(() => { Assert.That(post.Id, Is.EqualTo(postId)); - Assert.That(post.PostId, Is.EqualTo(postPostId)); Assert.That(post.PostName, Is.EqualTo(postName)); Assert.That(post.PostType, Is.EqualTo(postType)); Assert.That(post.Salary, Is.EqualTo(salary)); @@ -97,6 +79,6 @@ internal class PostDataModelTests Assert.That(post.ChangeDate, Is.EqualTo(changeDate)); }); } - private static PostDataModel CreateDataModel(string? id, string? postId, string? postName, PostType postType, double salary, bool isActual, DateTime changeDate) => - new(id, postId, postName, postType, salary, isActual, changeDate); + private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, double salary, bool isActual, DateTime changeDate) => + new(id, postName, postType, salary, isActual, changeDate); }