Модели

This commit is contained in:
Pyro 2025-02-13 12:42:06 +04:00
parent 644b2fdd73
commit 2a9f55b410
13 changed files with 331 additions and 0 deletions

View File

@ -0,0 +1,33 @@
using System.Text.RegularExpressions;
using TwoFromTheCaseContracts.Exceptions;
using TwoFromTheCaseContracts.Extensions;
using TwoFromTheCaseContracts.Infrastructure;
namespace TwoFromTheCaseContracts.DataModels;
public class CustomerDataModel(string id, string name, string phoneNumber) : IValidation
{
public string Id { get; private set; } = id;
public string Name { get; private set; } = name;
public string PhoneNumber { get; private set; } = phoneNumber;
public void Validate()
{
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 (Name.IsEmpty())
throw new ValidationException("Field Name is empty");
if (PhoneNumber.IsEmpty())
throw new ValidationException("Field PhoneNumber is empty");
if (!Regex.IsMatch(PhoneNumber, @"^((\+7|7|8)+([0-9]){10})$"))
throw new ValidationException("Field PhoneNumber is not a phone number");
}
}

View File

@ -0,0 +1,47 @@
using TwoFromTheCaseContracts.Enums;
using TwoFromTheCaseContracts.Exceptions;
using TwoFromTheCaseContracts.Extensions;
using TwoFromTheCaseContracts.Infrastructure;
namespace TwoFromTheCaseContracts.DataModels;
public class PostDataModel(string id, string postId, 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;
public bool IsActual { get; private set; } = isActual;
public DateTime ChangeDate { get; private set; } = changeDate;
public void Validate()
{
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");
if (PostName.IsEmpty())
throw new ValidationException("Field PostName is empty");
if (PostType == PostType.None)
throw new ValidationException("Field PostType is empty");
if (Salary <= 0)
throw new ValidationException("Field Salary is empty");
}
}

View File

@ -0,0 +1,40 @@
using TwoFromTheCaseContracts.Enums;
using TwoFromTheCaseContracts.Exceptions;
using TwoFromTheCaseContracts.Extensions;
using TwoFromTheCaseContracts.Infrastructure;
namespace TwoFromTheCaseContracts.DataModels;
public class PremisesDataModel(string id, string premisesName, PremisesType premisesType, string customerId, bool isDeleted) : IValidation
{
public string Id { get; private set; } = id;
public string PremisesName { get; private set; } = premisesName;
public PremisesType PremisesType { get; private set; } = premisesType;
public string CustomerId { get; private set; } = customerId;
public bool IsDeleted { get; private set; } = isDeleted;
public void Validate()
{
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 (PremisesName.IsEmpty())
throw new ValidationException("Field PremisesName is empty");
if (PremisesType == PremisesType.None)
throw new ValidationException("Field PremisesType is empty");
if (CustomerId.IsEmpty())
throw new ValidationException("Field CustomerId is empty");
if (!CustomerId.IsGuid())
throw new ValidationException("The value in the field CustomerId is not a unique identifier");
}
}

View File

@ -0,0 +1,26 @@
using TwoFromTheCaseContracts.Exceptions;
using TwoFromTheCaseContracts.Extensions;
using TwoFromTheCaseContracts.Infrastructure;
namespace TwoFromTheCaseContracts.DataModels;
public class SalaryDataModel(string workerId, DateTime salaryDate, double workerSalary) : IValidation
{
public string WorkerId { get; private set; } = workerId;
public DateTime SalaryDate { get; private set; } = salaryDate;
public double Salary { get; private set; } = workerSalary;
public void Validate()
{
if (WorkerId.IsEmpty())
throw new ValidationException("Field WorkerId is empty");
if (!WorkerId.IsGuid())
throw new ValidationException("The value in the field WorkerId is not a unique identifier");
if (Salary <= 0)
throw new ValidationException("Field Salary is less than or equal to 0");
}
}

View File

@ -0,0 +1,41 @@
using TwoFromTheCaseContracts.Exceptions;
using TwoFromTheCaseContracts.Extensions;
using TwoFromTheCaseContracts.Infrastructure;
namespace TwoFromTheCaseContracts.DataModels;
public class WorkDataModel(string id, string premisesId, double price, bool isCancel, List<WorkerWorkDataModel> workers) : IValidation
{
public string Id { get; private set; } = id;
public string PremisesId { get; private set; } = premisesId;
public double Price { get; private set; } = price;
public DateTime SaleDate { get; private set; } = DateTime.UtcNow;
public bool IsCancel { get; private set; } = isCancel;
public List<WorkerWorkDataModel> Workers { get; private set; } = workers;
public void Validate()
{
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 (PremisesId.IsEmpty())
throw new ValidationException("Field PremisesId is empty");
if (!PremisesId.IsGuid())
throw new ValidationException("The value in the field PremisesId is not a unique identifier");
if (Price <= 0)
throw new ValidationException("Field Price is less than or equal to 0");
if ((Workers?.Count ?? 0) == 0)
throw new ValidationException("The sale must include workers");
}
}

View File

@ -0,0 +1,26 @@
using TwoFromTheCaseContracts.Exceptions;
using TwoFromTheCaseContracts.Extensions;
using TwoFromTheCaseContracts.Infrastructure;
namespace TwoFromTheCaseContracts.DataModels;
public class WorkHistoryDataModel(string WorkId, double oldPrice) : IValidation
{
public string WorkId { get; private set; } = WorkId;
public double OldPrice { get; private set; } = oldPrice;
public DateTime ChangeDate { get; private set; } = DateTime.UtcNow;
public void Validate()
{
if (WorkId.IsEmpty())
throw new ValidationException("Field WorkId is empty");
if (!WorkId.IsGuid())
throw new ValidationException("The value in the field WorkId is not a unique identifier");
if (OldPrice <= 0)
throw new ValidationException("Field OldPrice is less than or equal to 0");
}
}

View File

@ -0,0 +1,47 @@
using TwoFromTheCaseContracts.Exceptions;
using TwoFromTheCaseContracts.Extensions;
using TwoFromTheCaseContracts.Infrastructure;
namespace TwoFromTheCaseContracts.DataModels;
public class WorkerDataModel(string id, string fio, string postId, DateTime birthDate, DateTime employmentDate, bool isDeleted) : IValidation
{
public string Id { get; private set; } = id;
public string FIO { get; private set; } = fio;
public string PostId { get; private set; } = postId;
public DateTime BirthDate { get; private set; } = birthDate;
public DateTime EmploymentDate { get; private set; } = employmentDate;
public bool IsDeleted { get; private set; } = isDeleted;
public void Validate()
{
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 (FIO.IsEmpty())
throw new ValidationException("Field FIO is empty");
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 (BirthDate.Date > DateTime.Now.AddYears(-16).Date)
throw new ValidationException($"Minors cannot be hired (BirthDate = {BirthDate.ToShortDateString()})");
if (EmploymentDate.Date < BirthDate.Date)
throw new ValidationException("The date of employment cannot be less than the date of birth");
if ((EmploymentDate - BirthDate).TotalDays / 365 < 16) // EmploymentDate.Year - BirthDate.Year
throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})");
}
}

View File

@ -0,0 +1,27 @@
using TwoFromTheCaseContracts.Exceptions;
using TwoFromTheCaseContracts.Extensions;
using TwoFromTheCaseContracts.Infrastructure;
namespace TwoFromTheCaseContracts.DataModels;
public class WorkerWorkDataModel(string workerId, string workId) : IValidation
{
public string WorkerId { get; private set; } = workerId;
public string WorkId { get; private set; } = workId;
public void Validate()
{
if (WorkerId.IsEmpty())
throw new ValidationException("Field WorkerId is empty");
if (!WorkerId.IsGuid())
throw new ValidationException("The value in the field WorkerId is not a unique identifier");
if (WorkId.IsEmpty())
throw new ValidationException("Field WorkId is empty");
if (!WorkId.IsGuid())
throw new ValidationException("The value in the field WorkId is not a unique identifier");
}
}

View File

@ -0,0 +1,11 @@
namespace TwoFromTheCaseContracts.Enums;
public enum PostType
{
None = 0,
Plasterer = 1,
Painter = 2,
Carpenter = 3,
Plumber = 4,
Electrician = 5
}

View File

@ -0,0 +1,8 @@
namespace TwoFromTheCaseContracts.Enums;
public enum PremisesType
{
None = 0,
Residential = 1,
Commercial = 2
}

View File

@ -0,0 +1,5 @@
namespace TwoFromTheCaseContracts.Exceptions;
public class ValidationException(string message) : Exception(message)
{
}

View File

@ -0,0 +1,14 @@
namespace TwoFromTheCaseContracts.Extensions;
public static class StringExtensions
{
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,6 @@
namespace TwoFromTheCaseContracts.Infrastructure;
public interface IValidation
{
void Validate();
}