From 86596a6c3007aa4c14018a65aa6e33dd5e97fac1 Mon Sep 17 00:00:00 2001 From: Glliza Date: Mon, 10 Feb 2025 01:13:25 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5=D1=81?= =?UTF-8?q?=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataModels/BuyerDataModel.cs | 38 +++++++++++++++++++ .../DataModels/PostDataModel.cs | 12 ++++++ .../PuferFishContracts/Enums/PostType.cs | 16 ++++++++ .../Exceptions/ValidationException.cs | 11 ++++++ .../Extensions/StringExtensions.cs | 21 ++++++++++ .../Infrastructure/IValidation.cs | 13 +++++++ 6 files changed, 111 insertions(+) create mode 100644 PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs create mode 100644 PuferFishContracts/PuferFishContracts/DataModels/PostDataModel.cs create mode 100644 PuferFishContracts/PuferFishContracts/Enums/PostType.cs create mode 100644 PuferFishContracts/PuferFishContracts/Exceptions/ValidationException.cs create mode 100644 PuferFishContracts/PuferFishContracts/Extensions/StringExtensions.cs create mode 100644 PuferFishContracts/PuferFishContracts/Infrastructure/IValidation.cs diff --git a/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs new file mode 100644 index 0000000..0d8eb78 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using PuferFishContracts.Extensions; +using PuferFishContracts.Exceptions; +using PuferFishContracts.Infrastructure; + +namespace PuferFishContracts.DataModels +{ + public class BuyerDataModel(string id, string fio, string phoneNumber, double points) : IValidation + { + public string Id { get; private set; } = id; + public string FIO { get; private set; } = fio; + public string PhoneNumber { get; private set; } = phoneNumber; + public double Points { get; private set; } = points; + + 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"); + } + } +} \ No newline at end of file diff --git a/PuferFishContracts/PuferFishContracts/DataModels/PostDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/PostDataModel.cs new file mode 100644 index 0000000..12de3f4 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/DataModels/PostDataModel.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.DataModels +{ + internal class PostDataModel + { + } +} diff --git a/PuferFishContracts/PuferFishContracts/Enums/PostType.cs b/PuferFishContracts/PuferFishContracts/Enums/PostType.cs new file mode 100644 index 0000000..3f63881 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Enums/PostType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.Enums +{ + public enum PostType + { + None = 0, + Cashier = 1, + chef = 2, + Packer = 3 + } +} diff --git a/PuferFishContracts/PuferFishContracts/Exceptions/ValidationException.cs b/PuferFishContracts/PuferFishContracts/Exceptions/ValidationException.cs new file mode 100644 index 0000000..0bf2b23 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Exceptions/ValidationException.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.Exceptions; + +public class ValidationException(string message) : Exception(message) +{ +} diff --git a/PuferFishContracts/PuferFishContracts/Extensions/StringExtensions.cs b/PuferFishContracts/PuferFishContracts/Extensions/StringExtensions.cs new file mode 100644 index 0000000..9756569 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Extensions/StringExtensions.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.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 var _); + } + } +} diff --git a/PuferFishContracts/PuferFishContracts/Infrastructure/IValidation.cs b/PuferFishContracts/PuferFishContracts/Infrastructure/IValidation.cs new file mode 100644 index 0000000..75037b5 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Infrastructure/IValidation.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.Infrastructure +{ + public interface IValidation + { + void Validate(); + } +}