diff --git a/WildPlum/WildPlum/DataModels/BuyerDataModel.cs b/WildPlum/WildPlum/DataModels/BuyerDataModel.cs new file mode 100644 index 0000000..9b42112 --- /dev/null +++ b/WildPlum/WildPlum/DataModels/BuyerDataModel.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Xml; +using WildPlum.Extensions; +using WildPlum.Infrastructure; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace WildPlum.DataModels; + +public class BuyerDataModel(string id, string fio, string phoneNumber) : IValidation +{ + public string Id { get; private set; } = id; + public string FIO { get; private set; } = fio; + 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 (FIO.IsEmpty()) + throw new ValidationException("Field FIO is empty"); + + if (PhoneNumber.IsEmpty()) + throw new ValidationException("Field PhoneNumber is empty"); + + if (!Regex.IsMatch(PhoneNumber, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\-]?)?[\d\- ]{7,10}$")) + throw new ValidationException("Field PhoneNumber is not a phone number"); + } +} diff --git a/WildPlum/WildPlum/DataModels/OrderDataModel.cs b/WildPlum/WildPlum/DataModels/OrderDataModel.cs new file mode 100644 index 0000000..aa4c375 --- /dev/null +++ b/WildPlum/WildPlum/DataModels/OrderDataModel.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WildPlum.Extensions; +using WildPlum.Infrastructure; + +namespace WildPlum.DataModels; + +public class OrderDataModel (string id, int price, DateTime date, string postId) : IValidation +{ + public string Id { get; private set; } = id; + public int Price { get; private set; } = price; + public DateTime Date { get; private set; } = date; + public string BuyerId { get; private set; } = postId; + + 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 (BuyerId.IsEmpty()) + throw new ValidationException("Field BuyerId is empty"); + if (!BuyerId.IsGuid()) + throw new ValidationException("The value in the field BuyerId is not a unique identifier"); + } +} diff --git a/WildPlum/WildPlum/DataModels/OrderProductDataModel.cs b/WildPlum/WildPlum/DataModels/OrderProductDataModel.cs new file mode 100644 index 0000000..888c67c --- /dev/null +++ b/WildPlum/WildPlum/DataModels/OrderProductDataModel.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WildPlum.Extensions; +using WildPlum.Infrastructure; + +namespace WildPlum.DataModels; + +public class OrderProductDataModel (string orderId, string productId, int count) : IValidation +{ + public string OrderId { get; private set; } = orderId; + public string ProductId { get; private set; } = productId; + public int Count { get; private set; } = count; + + public void Validate() + { + if (OrderId.IsEmpty()) + throw new ValidationException("Field OrderId is empty"); + if (!OrderId.IsGuid()) + throw new ValidationException("The value in the field OrderId is not a unique identifier"); + if (ProductId.IsEmpty()) + throw new ValidationException("Field ProductId is empty"); + if (!ProductId.IsGuid()) + throw new ValidationException("The value in the field Id is not a unique identifier"); + if (Count <= 0) + throw new ValidationException("Field Count is less than or equal to 0"); + } +} diff --git a/WildPlum/WildPlum/DataModels/PostDateModel.cs b/WildPlum/WildPlum/DataModels/PostDateModel.cs new file mode 100644 index 0000000..992ee67 --- /dev/null +++ b/WildPlum/WildPlum/DataModels/PostDateModel.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using WildPlum.Enums; +using WildPlum.Extensions; +using WildPlum.Infrastructure; + +namespace WildPlum.DataModels; + +public class PostDateModel(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"); + } +} diff --git a/WildPlum/WildPlum/DataModels/ProductDataModel.cs b/WildPlum/WildPlum/DataModels/ProductDataModel.cs new file mode 100644 index 0000000..8707ab0 --- /dev/null +++ b/WildPlum/WildPlum/DataModels/ProductDataModel.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using WildPlum.Enums; +using WildPlum.Extensions; +using WildPlum.Infrastructure; + +namespace WildPlum.DataModels; + +public class ProductDataModel (string id, string productName, ProductType productType, string manufacturerId, double price, bool isDeleted) : IValidation +{ + public string Id { get; private set; } = id; + public string ProductName { get; private set; } = productName; + public ProductType ProductType { get; private set; } = productType; + 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 (ProductName.IsEmpty()) + throw new ValidationException("Field ProductName is empty"); + if (ProductType == ProductType.None) + throw new ValidationException("Field ProductType is empty"); + if (Price <= 0) + throw new ValidationException("Field Price is less than or equal to 0"); + } +} diff --git a/WildPlum/WildPlum/DataModels/ProductHistoryDataModel.cs b/WildPlum/WildPlum/DataModels/ProductHistoryDataModel.cs new file mode 100644 index 0000000..907b441 --- /dev/null +++ b/WildPlum/WildPlum/DataModels/ProductHistoryDataModel.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WildPlum.Extensions; +using WildPlum.Infrastructure; + +namespace WildPlum.DataModels; + +public class ProductHistoryDataModel(string productId, double oldPrice) : IValidation +{ + public string ProductId { get; private set; } = productId; + public double OldPrice { get; private set; } = oldPrice; + public DateTime ChangeDate { get; private set; } = DateTime.UtcNow; + public void Validate() + { + if (ProductId.IsEmpty()) + throw new ValidationException("Field ProductId is empty"); + if (!ProductId.IsGuid()) + throw new ValidationException("The value in the field ProductId is not a unique identifier"); + if (OldPrice <= 0) + throw new ValidationException("Field OldPrice is less than or equal to 0"); + } +} diff --git a/WildPlum/WildPlum/DataModels/ProductWarehouseDataModel.cs b/WildPlum/WildPlum/DataModels/ProductWarehouseDataModel.cs new file mode 100644 index 0000000..68f2d7c --- /dev/null +++ b/WildPlum/WildPlum/DataModels/ProductWarehouseDataModel.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WildPlum.Extensions; +using WildPlum.Infrastructure; + +namespace WildPlum.DataModels; + +public class ProductWarehouseDataModel (string productId, string warehouseId, int count) : IValidation +{ + public string ProductId { get; private set; } = productId; + public string WarehouseId { get; private set; } = warehouseId; + public int Count { get; private set; } = count; + + public void Validate() + { + if (ProductId.IsEmpty()) + throw new ValidationException("Field ProductId is empty"); + if (WarehouseId.IsEmpty()) + throw new ValidationException("Field WarehouseId is empty"); + if (!WarehouseId.IsGuid()) + throw new ValidationException("The value in the field WarehouseId is not a unique identifier"); + if (!ProductId.IsGuid()) + throw new ValidationException("The value in the field Id is not a unique identifier"); + if (Count <= 0) + throw new ValidationException("Field Count is less than or equal to 0"); + } +} diff --git a/WildPlum/WildPlum/DataModels/SalaryDataModel.cs b/WildPlum/WildPlum/DataModels/SalaryDataModel.cs new file mode 100644 index 0000000..062c8e3 --- /dev/null +++ b/WildPlum/WildPlum/DataModels/SalaryDataModel.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WildPlum.Extensions; +using WildPlum.Infrastructure; + +namespace WildPlum.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"); + } +} diff --git a/WildPlum/WildPlum/DataModels/WarehouseDataModel.cs b/WildPlum/WildPlum/DataModels/WarehouseDataModel.cs new file mode 100644 index 0000000..ef887c2 --- /dev/null +++ b/WildPlum/WildPlum/DataModels/WarehouseDataModel.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WildPlum.Extensions; +using WildPlum.Infrastructure; + +namespace WildPlum.DataModels; + +public class WarehouseDataModel (string id, string name, string adress) : IValidation +{ + public string Id { get; private set; } = id; + public string Name { get; private set; } = name; + public string Adress { get; private set; } = adress; + + 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 (Adress.IsEmpty()) + throw new ValidationException("Field Adress is empty"); + } +} diff --git a/WildPlum/WildPlum/DataModels/WorkerDataModel.cs b/WildPlum/WildPlum/DataModels/WorkerDataModel.cs new file mode 100644 index 0000000..5d047c5 --- /dev/null +++ b/WildPlum/WildPlum/DataModels/WorkerDataModel.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using WildPlum.Extensions; +using WildPlum.Infrastructure; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace WildPlum.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()})"); + } +} diff --git a/WildPlum/WildPlum/Enums/PostType.cs b/WildPlum/WildPlum/Enums/PostType.cs new file mode 100644 index 0000000..a5b547e --- /dev/null +++ b/WildPlum/WildPlum/Enums/PostType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WildPlum.Enums; + +public enum PostType +{ + None = 0, + Operator = 1, + Delivaryman = 2, + Loader = 3 +} diff --git a/WildPlum/WildPlum/Enums/ProductType.cs b/WildPlum/WildPlum/Enums/ProductType.cs new file mode 100644 index 0000000..fac6595 --- /dev/null +++ b/WildPlum/WildPlum/Enums/ProductType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WildPlum.Enums; + +public enum ProductType +{ + None = 0, + Cloth = 1, + Shoes = 2, + Toy = 3, + Office = 4 +} diff --git a/WildPlum/WildPlum/Exceptions/ValidationException.cs b/WildPlum/WildPlum/Exceptions/ValidationException.cs new file mode 100644 index 0000000..dee38de --- /dev/null +++ b/WildPlum/WildPlum/Exceptions/ValidationException.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WildPlum.Exceptions; + +public class ValidationException(string message) : Exception(message) +{ +} diff --git a/WildPlum/WildPlum/Extensions/StringExtensions.cs b/WildPlum/WildPlum/Extensions/StringExtensions.cs new file mode 100644 index 0000000..5f9c8c1 --- /dev/null +++ b/WildPlum/WildPlum/Extensions/StringExtensions.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WildPlum.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 _); + } +} diff --git a/WildPlum/WildPlum/Infrastructure/IValidation.cs b/WildPlum/WildPlum/Infrastructure/IValidation.cs new file mode 100644 index 0000000..d14f809 --- /dev/null +++ b/WildPlum/WildPlum/Infrastructure/IValidation.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WildPlum.Infrastructure; + +public interface IValidation +{ + void Validate(); +}