add data models

This commit is contained in:
Ivan Gutorov 2025-02-03 01:12:40 +04:00
parent bbfbb5689f
commit 5790aecef2
12 changed files with 324 additions and 0 deletions

View File

@ -0,0 +1,33 @@
using System.Text.RegularExpressions;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.Infrastructure;
namespace TheCyclopsContracts.DataModels;
public class ClientDataModel(string id, string fio, string email) : IValidation
{
public string Id { get; private set; } = id;
public string FIO { get; private set; } = fio;
public string Email { get; private set; } = email;
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 (Email.IsEmpty())
throw new ValidationException("Field Email is empty");
if (!Regex.IsMatch(Email, @"^\S+@\S+\.\S+$"))
throw new ValidationException("Field Email is not an email address");
}
}

View File

@ -0,0 +1,38 @@
using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.Infrastructure;
namespace TheCyclopsContracts.DataModels;
public class ComponentDataModel(string id, string componentName,
ComponentType componentType, double price, bool isDeleted) : IValidation
{
public string Id { get; private set; } = id;
public string ComponentName { get; private set; } = componentName;
public ComponentType ComponentType { get; private set; } = componentType;
public double Price { get; private set; } = price;
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 (ComponentName.IsEmpty())
throw new ValidationException("Field ComponentName is empty");
if (ComponentType == ComponentType.None)
throw new ValidationException("Field ComponentType is empty");
if (Price <= 0)
throw new ValidationException("Field Price is less than or equal to 0");
}
}

View File

@ -0,0 +1,47 @@
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.Infrastructure;
namespace TheCyclopsContracts.DataModels;
public class EmployeeDataModel(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)
throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})");
}
}

View File

@ -0,0 +1,33 @@
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.Infrastructure;
namespace TheCyclopsContracts.DataModels;
public class InstallationComponentDataModel(string installationId,
string componentId, int count) : IValidation
{
public string InstallationId { get; private set; } = installationId;
public string ComponentId { get; private set; } = componentId;
public int Count { get; private set; } = count;
public void Validate()
{
if (InstallationId.IsEmpty())
throw new ValidationException("Field InstallationId is empty");
if (!InstallationId.IsGuid())
throw new ValidationException("The value in the field InstallationId is not a unique identifier");
if (ComponentId.IsEmpty())
throw new ValidationException("Field ComponentId is empty");
if (!ComponentId.IsGuid())
throw new ValidationException("The value in the field ComponentId is not a unique identifier");
if (Count <= 0)
throw new ValidationException("Field Count is less than or equal to 0");
}
}

View File

@ -0,0 +1,55 @@
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.Infrastructure;
namespace TheCyclopsContracts.DataModels;
public class InstallationDataModel(string id, string employeeId, string? clientId,
DateTime installationDate, double installationPrice, double sum, bool isCanceled,
List<InstallationComponentDataModel> components) : IValidation
{
public string Id { get; private set; } = id;
public string EmployeeId { get; private set; } = employeeId;
public string? ClientId { get; private set; } = clientId;
public DateTime SaleDate { get; private set; } = DateTime.UtcNow;
public DateTime InstallationDate { get; private set; } = installationDate;
public double InstallationPrice { get; private set; } = installationPrice;
public double Sum { get; private set; } = sum;
public bool IsCanceled { get; private set; } = isCanceled;
public List<InstallationComponentDataModel> Components { get; private set; } = components;
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 (EmployeeId.IsEmpty())
throw new ValidationException("Field EmployeeId is empty");
if (!EmployeeId.IsGuid())
throw new ValidationException("The value in the field EmployeeId is not a unique identifier");
if (!ClientId?.IsGuid() ?? !ClientId?.IsEmpty() ?? false)
throw new ValidationException("The value in the field ClientId is not a unique identifier");
if (InstallationPrice <= 0)
throw new ValidationException("Field InstallationPrice is less than or equal to 0");
if (Sum <= 0)
throw new ValidationException("Field Sum is less than or equal to 0");
if ((Components?.Count ?? 0) == 0)
throw new ValidationException("The sale must include components");
}
}

View File

@ -0,0 +1,46 @@
using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
namespace TheCyclopsContracts.DataModels;
public class PostDataModel(string id, string PostId, string postName, PostType postType, double salary, bool isActual, DateTime changeDate)
{
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,26 @@
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.Infrastructure;
namespace TheCyclopsContracts.DataModels;
public class SalaryDataModel(string employeeId, DateTime salaryDate, double employeeSalary) : IValidation
{
public string EmployeeId { get; private set; } = employeeId;
public DateTime SalaryDate { get; private set; } = salaryDate;
public double Salary { get; private set; } = employeeSalary;
public void Validate()
{
if (EmployeeId.IsEmpty())
throw new ValidationException("Field EmployeeId is empty");
if (!EmployeeId.IsGuid())
throw new ValidationException("The value in the field EmployeeId 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,11 @@
namespace TheCyclopsContracts.Enums;
public enum ComponentType
{
None = 0,
Camera = 1,
Sensor = 2,
ContropPanel = 3,
Cable = 4,
Other = 5
}

View File

@ -0,0 +1,10 @@
namespace TheCyclopsContracts.Enums;
public enum PostType
{
None = 0,
Designer = 1,
Installer = 2,
Seller = 3,
Administrator = 4
}

View File

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

View File

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

View File

@ -0,0 +1,6 @@
namespace TheCyclopsContracts.Infrastructure;
public interface IValidation
{
void Validate();
}