From c2dc672bfb7b1af8b6b3aef074bae7adb9023ffb Mon Sep 17 00:00:00 2001 From: Nikoo Asadnejad Date: Sat, 28 Oct 2023 22:49:20 +0330 Subject: [PATCH] feat : add ip valueObject --- .../Constants/Messages/ExceptionMessage.cs | 2 + Src/Product.Domain/Entities/BaseModel.cs | 41 ++++++++++-------- Src/Product.Domain/Entities/CategoryModel.cs | 2 - Src/Product.Domain/Entities/ProductModel.cs | 1 - .../Exceptions/DomainException.cs | 9 ---- .../Exceptions/DomainValidationException.cs | 9 ++++ .../Exceptions/Genrals/InvalidIpException.cs | 2 +- Src/Product.Domain/ValueObjects/IPAddress.cs | 6 --- Src/Product.Domain/ValueObjects/Ip.cs | 42 +++++++++++++++++++ 9 files changed, 78 insertions(+), 36 deletions(-) delete mode 100644 Src/Product.Domain/Exceptions/DomainException.cs create mode 100644 Src/Product.Domain/Exceptions/DomainValidationException.cs delete mode 100644 Src/Product.Domain/ValueObjects/IPAddress.cs create mode 100644 Src/Product.Domain/ValueObjects/Ip.cs diff --git a/Src/Product.Domain/Constants/Messages/ExceptionMessage.cs b/Src/Product.Domain/Constants/Messages/ExceptionMessage.cs index 5ef4e9b..cdb03ca 100644 --- a/Src/Product.Domain/Constants/Messages/ExceptionMessage.cs +++ b/Src/Product.Domain/Constants/Messages/ExceptionMessage.cs @@ -3,4 +3,6 @@ namespace Product.Domain.Constants.Messages; public struct ExceptionMessage { public static readonly string InvalidIp = "Ip is invalid"; + public static readonly string IpIsMandatry = "The IpAddress cannot be null or empty."; + public static string IpLenghtShouldBe(int lenght) => $"The IpAddress length must be {lenght} characters."; } \ No newline at end of file diff --git a/Src/Product.Domain/Entities/BaseModel.cs b/Src/Product.Domain/Entities/BaseModel.cs index 996b3ff..64d87b2 100644 --- a/Src/Product.Domain/Entities/BaseModel.cs +++ b/Src/Product.Domain/Entities/BaseModel.cs @@ -1,5 +1,6 @@ -using System.Net; + using Product.Domain.Exceptions; +using Product.Domain.ValueObjects; namespace Product.Domain.Entities; @@ -12,39 +13,45 @@ public abstract class BaseModel public long? UpdatedBy { get; private set; } public long? DeleteDate { get; private set; } public long? DeletedBy { get; private set; } - public string? CreateIp { get; private set; } - public string? UpdateIp { get; private set; } - public string? DeleteIp { get; private set; } + public Ip? CreateIp { get; private set; } + public Ip? UpdateIp { get; private set; } + public Ip? DeleteIp { get; private set; } public virtual void Create(string? ip = null , long? createdBy = null) { this.CreateDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); this.CreatedBy = createdBy; - ValidateIp(ip); - this.CreateIp = ip; + this.CreateIp = new Ip(ip); } public virtual void Update(string? ip = null, long? updatedBy = null) { this.UpdateDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); this.UpdatedBy = updatedBy; - ValidateIp(ip); - this.UpdateIp = ip; + this.UpdateIp = new Ip(ip); } public virtual void Delete(string? ip = null, long? deletedBy = null) { this.DeleteDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); this.DeletedBy = deletedBy; - ValidateIp(ip); - this.DeleteIp = ip; + this.DeleteIp = new Ip(ip); + } + public virtual void Create(Ip? ip = null , long? createdBy = null) + { + this.CreateDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + this.CreatedBy = createdBy; + this.CreateIp = ip; } - void ValidateIp(string? ip) + public virtual void Update(Ip? ip = null, long? updatedBy = null) { - if(string.IsNullOrWhiteSpace(ip)) - return; - - if (!IsIpValid(ip)) - throw new InvalidIpException(); + this.UpdateDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + this.UpdatedBy = updatedBy; + this.UpdateIp = ip; + } + public virtual void Delete(Ip? ip = null, long? deletedBy = null) + { + this.DeleteDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + this.DeletedBy = deletedBy; + this.DeleteIp = ip; } - bool IsIpValid(string ip) => IPAddress.TryParse(ip, out _); } \ No newline at end of file diff --git a/Src/Product.Domain/Entities/CategoryModel.cs b/Src/Product.Domain/Entities/CategoryModel.cs index 3765928..8aef31c 100644 --- a/Src/Product.Domain/Entities/CategoryModel.cs +++ b/Src/Product.Domain/Entities/CategoryModel.cs @@ -4,14 +4,12 @@ public sealed class CategoryModel : BaseModel { public CategoryModel(string title , CategoryModel? subCategory = null) { - base.Create(); this.Title = title; SubCategory = subCategory; SubCategoryId = subCategory?.Id; } public CategoryModel(string title , long? subCategoryId = null) { - base.Create(); this.Title = title; SubCategoryId = subCategoryId; } diff --git a/Src/Product.Domain/Entities/ProductModel.cs b/Src/Product.Domain/Entities/ProductModel.cs index f46a70a..0c0d3f7 100644 --- a/Src/Product.Domain/Entities/ProductModel.cs +++ b/Src/Product.Domain/Entities/ProductModel.cs @@ -5,7 +5,6 @@ public sealed class ProductModel : BaseModel public ProductModel(string title, int price ,long? categoryId = null, string? subTitle = null, string? description = null) { - base.Create(); Title = title; SubTitle = subTitle; Description = description; diff --git a/Src/Product.Domain/Exceptions/DomainException.cs b/Src/Product.Domain/Exceptions/DomainException.cs deleted file mode 100644 index 2bd08db..0000000 --- a/Src/Product.Domain/Exceptions/DomainException.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Product.Domain.Exceptions; - -public abstract class DomainException : Exception -{ - public DomainException(string message) : base(message) - { - - } -} \ No newline at end of file diff --git a/Src/Product.Domain/Exceptions/DomainValidationException.cs b/Src/Product.Domain/Exceptions/DomainValidationException.cs new file mode 100644 index 0000000..5fef949 --- /dev/null +++ b/Src/Product.Domain/Exceptions/DomainValidationException.cs @@ -0,0 +1,9 @@ +namespace Product.Domain.Exceptions; + +public class DomainValidationException : Exception +{ + public DomainValidationException(string message) : base(message) + { + + } +} \ No newline at end of file diff --git a/Src/Product.Domain/Exceptions/Genrals/InvalidIpException.cs b/Src/Product.Domain/Exceptions/Genrals/InvalidIpException.cs index 4853db2..2a65ed4 100644 --- a/Src/Product.Domain/Exceptions/Genrals/InvalidIpException.cs +++ b/Src/Product.Domain/Exceptions/Genrals/InvalidIpException.cs @@ -1,7 +1,7 @@ using Product.Domain.Constants.Messages; namespace Product.Domain.Exceptions; -public sealed class InvalidIpException : DomainException +public sealed class InvalidIpException : DomainValidationException { public InvalidIpException() : base(message: ExceptionMessage.InvalidIp) { diff --git a/Src/Product.Domain/ValueObjects/IPAddress.cs b/Src/Product.Domain/ValueObjects/IPAddress.cs deleted file mode 100644 index 43ff17e..0000000 --- a/Src/Product.Domain/ValueObjects/IPAddress.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Product.Domain.ValueObjects; - -public class IPAddress -{ - -} \ No newline at end of file diff --git a/Src/Product.Domain/ValueObjects/Ip.cs b/Src/Product.Domain/ValueObjects/Ip.cs new file mode 100644 index 0000000..0faa159 --- /dev/null +++ b/Src/Product.Domain/ValueObjects/Ip.cs @@ -0,0 +1,42 @@ +using System.Security; +using Product.Domain.Constants.Messages; +using Product.Domain.Exceptions; + +namespace Product.Domain.ValueObjects; + +public class Ip : ValueObject +{ + private const int Lenght = 15; + public string Value{ get;private set; } + + public Ip(string ip) + { + SetValue(ip); + } + protected override IEnumerable GetEqualityComponents() + { + yield return Value; + } + + private void SetValue(string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + throw new DomainValidationException(ExceptionMessage.IpIsMandatry); + } + + if (value.Length > Lenght || value.Length < Lenght) + { + throw new DomainValidationException(ExceptionMessage.IpLenghtShouldBe(Lenght)); + } + + if (IsIpValid(value)) + { + throw new InvalidIpException(); + } + + Value = value; + } + + bool IsIpValid(string ip) => System.Net.IPAddress.TryParse(ip, out _); +} \ No newline at end of file