feat: добавил дата модели роли кладовщик и контракт валидации, который и реализовал, добавил ошибку валидации

This commit is contained in:
2025-04-18 14:20:49 +04:00
parent 5944a60cd5
commit 998c1e5d33
10 changed files with 357 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,37 @@
using BankContracts.Exceptions;
using BankContracts.Extensions;
using BankContracts.Infrastructure;
namespace BankContracts.DataModels;
/// <summary>
/// Дата модель кредитная программа - валюта (многие ко многим)
/// </summary>
/// <param name="creditProgramId">уникальный Guid индентификатор кредитной программы</param>
/// <param name="currencyId">уникальный Guid индентификатор валюты</param>
public class CreditProgramCurrencyDataModel(string creditProgramId, string currencyId) : IValidation
{
public string CreditProgramId { get; private set; } = creditProgramId;
public string CurrencyId { get; private set; } = currencyId;
public void Validate()
{
if (CreditProgramId.IsEmpty())
{
throw new ValidationException("Field CreditProgramId is null or empty");
}
if (!CreditProgramId.IsGuid())
{
throw new ValidationException("The value in the field CreditProgramId is not a unique identifier");
}
if (CurrencyId.IsEmpty())
{
throw new ValidationException("Field CurrencyId is null or empty");
}
if (!CurrencyId.IsGuid())
{
throw new ValidationException("The value in the field CurrencyId is not a unique identifier");
}
}
}

View File

@@ -0,0 +1,69 @@
using BankContracts.Exceptions;
using BankContracts.Extensions;
using BankContracts.Infrastructure;
namespace BankContracts.DataModels;
/// <summary>
/// Дата модель кредитной программы
/// </summary>
/// <param name="id">уникальный Guid индентификатор</param>
/// <param name="name">название</param>
/// <param name="cost">сумма</param>
/// <param name="maxCost">максимальная сумма</param>
/// <param name="storekeeperId">уникальный Guid Индентификатор кладовщика</param>
/// <param name="periodId">уникальный Guid Индентификатор срока</param>
public class CreditProgramDataModel(string id, string name, decimal cost, decimal maxCost, string storekeeperId, string periodId) : IValidation
{
public string Id { get; private set; } = id;
public string Name { get; private set; } = name;
public decimal Cost { get; private set; } = cost;
public decimal MaxCost { get; private set; } = maxCost;
public string StorekeeperId { get; private set; } = storekeeperId;
public string PeriodId { get; private set; } = periodId;
public void Validate()
{
if (Id.IsEmpty())
{
throw new ValidationException("Field Id is null or empty");
}
if (!Id.IsGuid())
{
throw new ValidationException("The value in the field Id is not a unique identifier");
}
if (Name.IsEmpty())
{
throw new ValidationException("Field Name is null or empty");
}
if (Cost <= 0)
{
throw new ValidationException("Field Cost is less or equal to zero");
}
if (MaxCost <= 0)
{
throw new ValidationException("Field MaxCost is less or equal to zero");
}
if (StorekeeperId.IsEmpty())
{
throw new ValidationException("Field StorekeeperId is null or empty");
}
if (!StorekeeperId.IsGuid())
{
throw new ValidationException("The value in the field StorekeeperId is not a unique identifier");
}
if (PeriodId.IsEmpty())
{
throw new ValidationException("Field PeriodId is null or empty");
}
if (!PeriodId.IsGuid())
{
throw new ValidationException("The value in the field PeriodId is not a unique identifier");
}
}
}

View File

@@ -0,0 +1,58 @@
using BankContracts.Exceptions;
using BankContracts.Extensions;
using BankContracts.Infrastructure;
namespace BankContracts.DataModels;
/// <summary>
/// Дата модель валюты
/// </summary>
/// <param name="id">уникальный Guid индентификатор</param>
/// <param name="name">название</param>
/// <param name="abbreviation">аббревиатура</param>
/// <param name="cost">стоимость валюты</param>
/// <param name="storekeeperId">уникальный Guid Индентификатор кладовщика</param>
public class CurrencyDataModel(string id, string name, string abbreviation, decimal cost, string storekeeperId) : IValidation
{
public string Id { get; private set; } = id;
public string Name { get; private set; } = name;
public string Abbreviation { get; private set; } = abbreviation;
public decimal Cost { get; private set; } = cost;
public string StorekeeperId { get; private set; } = storekeeperId;
public void Validate()
{
if (Id.IsEmpty())
{
throw new ValidationException("Field Id is null or empty");
}
if (!Id.IsGuid())
{
throw new ValidationException("The value in the field Id is not a unique identifier");
}
if (Name.IsEmpty())
{
throw new ValidationException("Field Name is null or empty");
}
if (Abbreviation.IsEmpty())
{
throw new ValidationException("Field Abbreviation is null or empty");
}
if (Cost <= 0)
{
throw new ValidationException("Field Cost is less or equal to zero");
}
if (StorekeeperId.IsEmpty())
{
throw new ValidationException("Field StorekeeperId is null or empty");
}
if (!StorekeeperId.IsGuid())
{
throw new ValidationException("The value in the field StorekeeperId is not a unique identifier");
}
}
}

View File

@@ -0,0 +1,47 @@
using BankContracts.Exceptions;
using BankContracts.Extensions;
using BankContracts.Infrastructure;
namespace BankContracts.DataModels;
/// <summary>
/// Дата модель срока
/// </summary>
/// <param name="id">уникальный Guid индентификатор</param>
/// <param name="startDate"></param>
/// <param name="endDate"></param>
/// <param name="storekeeperId">уникальный Guid индентификатор кладовщика</param>
public class PeriodDataModel(string id, DateTime startDate, DateTime endDate, string storekeeperId) : IValidation
{
public string Id { get; private set; } = id;
public DateTime StartTime { get; private set; } = startDate;
public DateTime EndTime { get; private set; } = endDate;
public string StorekeeperId { get; private set; } = storekeeperId;
public void Validate()
{
if (Id.IsEmpty())
{
throw new ValidationException("Field Id is null or empty");
}
if (!Id.IsGuid())
{
throw new ValidationException("The value in the field Id is not a unique identifier");
}
if (StartTime.Date > EndTime.Date)
{
throw new ValidationException($"he date of period start cannot be larger than the date of period end");
}
if (StorekeeperId.IsEmpty())
{
throw new ValidationException("Field StorekeeperId is null or empty");
}
if (!StorekeeperId.IsGuid())
{
throw new ValidationException("The value in the field StorekeeperId is not a unique identifier");
}
}
}

View File

@@ -0,0 +1,86 @@
using BankContracts.Exceptions;
using BankContracts.Extensions;
using BankContracts.Infrastructure;
using System.Text.RegularExpressions;
namespace BankContracts.DataModels;
/// <summary>
/// Дата модель кладовщика
/// </summary>
/// <param name="id">уникальный Guid индентификатор</param>
/// <param name="name">имя</param>
/// <param name="surname">фамилия</param>
/// <param name="middleName">отчество (лучше не придумал)</param>
/// <param name="login">логин</param>
/// <param name="password">пароль</param>
/// <param name="email">адрес электронной почты</param>
/// <param name="phoneNumber">номер телефона</param>
public class StorekeeperDataModel(
string id, string name, string surname, string middleName,
string login, string password, string email, string phoneNumber) : IValidation
{
public string Id { get; private set; } = id;
public string Name { get; private set; } = name;
public string Surname { get; private set; } = surname;
public string MiddleName { get; private set; } = middleName;
public string Login { get; private set; } = login;
public string Password { get; private set; } = password;
public string Email { get; private set; } = email;
public string PhoneNumber { get; private set; } = phoneNumber;
public void Validate()
{
if (Id.IsEmpty())
{
throw new ValidationException("Field Id is null or empty");
}
if (!Id.IsGuid())
{
throw new ValidationException("The value in the field Id is not a unique identifier");
}
if (Name.IsEmpty())
{
throw new ValidationException("Field Name is null or empty");
}
if (Surname.IsEmpty())
{
throw new ValidationException("Field Surname is null or empty");
}
if (MiddleName.IsEmpty())
{
throw new ValidationException("Field MiddleName is null or empty");
}
if (Login.IsEmpty())
{
throw new ValidationException("Field Login is null or empty");
}
if (Password.IsEmpty())
{
throw new ValidationException("Field Password is null or empty");
}
if (Email.IsEmpty())
{
throw new ValidationException("Field Email is null or empty");
}
if (!Regex.IsMatch(Email, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"))
{
throw new ValidationException("Field Email is not a valid email address");
}
if (PhoneNumber.IsEmpty())
{
throw new ValidationException("Field PhoneNumber is null or empty");
}
if (!Regex.IsMatch(PhoneNumber, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$"))
{
throw new ValidationException("Field PhoneNumber is not a phone number");
}
}
}

View File

@@ -0,0 +1,6 @@
namespace BankContracts.Exceptions;
/// <summary>
/// Класс-исключение при валидации данных в дата моделях
/// </summary>
public class ValidationException(string message) : Exception(message) { }

View File

@@ -0,0 +1,14 @@
namespace BankContracts.Extensions;
public static class StringExtension
{
public static bool IsEmpty(this string str)
{
return string.IsNullOrWhiteSpace(str);
}
public static bool IsGuid(this string str)
{
return Guid.TryParse(str, out _);
}
}

View File

@@ -0,0 +1,9 @@
namespace BankContracts.Infrastructure;
/// <summary>
/// Валидация данных в дата моделях
/// </summary>
internal interface IValidation
{
void Validate();
}

22
TheBank/TheBank.sln Normal file
View File

@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BankContracts", "BankContracts\BankContracts.csproj", "{7AF1B83C-2E92-482F-8189-6FE7313134FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7AF1B83C-2E92-482F-8189-6FE7313134FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7AF1B83C-2E92-482F-8189-6FE7313134FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7AF1B83C-2E92-482F-8189-6FE7313134FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7AF1B83C-2E92-482F-8189-6FE7313134FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal