конструкторы и исключения

This commit is contained in:
Adelina888 2025-02-20 20:07:10 +04:00
parent a7bfe8177a
commit d14fa74e38
17 changed files with 157 additions and 49 deletions

View File

@ -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

View File

@ -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 PostName { get; private set; } = postName;
public PostType PostType { get; private set; } = postType;
public double Salary { get; private set; } = salary;
@ -27,10 +26,6 @@ public class PostDataModel(string id, string postId, string postName, PostType p
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");
if (PostName.IsEmpty())
throw new ValidationException("Field PostName is empty");
if (PostType==PostType.None)

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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}") { }
}

View File

@ -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") { }
}

View File

@ -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) { }
}

View File

@ -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<OrderDataModel> GetAllOrdersByPeriod(DateTime fromDate, DateTime toDate)
{

View File

@ -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<PostDataModel> 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)

View File

@ -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<ProductDataModel> GetAllProducts(bool onlyActive = true)
{

View File

@ -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<RestaurantDataModel> GetAllRestaurants()
{

View File

@ -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<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
{

View File

@ -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<WorkerDataModel> GetAllWorkers(bool onlyActive = true)
{

View File

@ -7,4 +7,17 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="PipingHotTests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PipingHot\PipingHotContrast.csproj" />
</ItemGroup>
</Project>

View File

@ -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
{
}

View File

@ -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<ValidationException>());
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<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var post = CreateDataModel("id", Guid.NewGuid().ToString(), "name", PostType.Operator, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostIdIsNullEmptyTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), null, "name", PostType.Operator, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "name", PostType.Operator, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[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<ValidationException>());
}
[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<ValidationException>());
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<ValidationException>());
}
[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<ValidationException>());
}
[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<ValidationException>());
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<ValidationException>());
}
@ -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);
}