Skip to content

Commit

Permalink
feat : add ip valueObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikoo-Asadnejad committed Oct 28, 2023
1 parent cb498f9 commit c2dc672
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 36 deletions.
2 changes: 2 additions & 0 deletions Src/Product.Domain/Constants/Messages/ExceptionMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
}
41 changes: 24 additions & 17 deletions Src/Product.Domain/Entities/BaseModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;

using Product.Domain.Exceptions;
using Product.Domain.ValueObjects;

namespace Product.Domain.Entities;

Expand All @@ -12,39 +13,45 @@ public abstract class BaseModel<T>
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);

Check warning on line 24 in Src/Product.Domain/Entities/BaseModel.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'ip' in 'Ip.Ip(string 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);

Check warning on line 30 in Src/Product.Domain/Entities/BaseModel.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'ip' in 'Ip.Ip(string 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);

Check warning on line 36 in Src/Product.Domain/Entities/BaseModel.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'ip' in 'Ip.Ip(string 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 _);

}
2 changes: 0 additions & 2 deletions Src/Product.Domain/Entities/CategoryModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ public sealed class CategoryModel : BaseModel<long>
{
public CategoryModel(string title , CategoryModel? subCategory = null)

Check warning on line 5 in Src/Product.Domain/Entities/CategoryModel.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'SubCategory' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
base.Create();
this.Title = title;
SubCategory = subCategory;

Check warning on line 8 in Src/Product.Domain/Entities/CategoryModel.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
SubCategoryId = subCategory?.Id;
}
public CategoryModel(string title , long? subCategoryId = null)

Check warning on line 11 in Src/Product.Domain/Entities/CategoryModel.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'SubCategory' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
base.Create();
this.Title = title;
SubCategoryId = subCategoryId;
}
Expand Down
1 change: 0 additions & 1 deletion Src/Product.Domain/Entities/ProductModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ public sealed class ProductModel : BaseModel<long>

public ProductModel(string title, int price ,long? categoryId = null, string? subTitle = null, string? description = null)

Check warning on line 6 in Src/Product.Domain/Entities/ProductModel.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Category' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 6 in Src/Product.Domain/Entities/ProductModel.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Category' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
base.Create();
Title = title;
SubTitle = subTitle;
Description = description;
Expand Down
9 changes: 0 additions & 9 deletions Src/Product.Domain/Exceptions/DomainException.cs

This file was deleted.

9 changes: 9 additions & 0 deletions Src/Product.Domain/Exceptions/DomainValidationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Product.Domain.Exceptions;

public class DomainValidationException : Exception
{
public DomainValidationException(string message) : base(message)
{

}
}
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down
6 changes: 0 additions & 6 deletions Src/Product.Domain/ValueObjects/IPAddress.cs

This file was deleted.

42 changes: 42 additions & 0 deletions Src/Product.Domain/ValueObjects/Ip.cs
Original file line number Diff line number Diff line change
@@ -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)

Check warning on line 12 in Src/Product.Domain/ValueObjects/Ip.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Value' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
SetValue(ip);
}
protected override IEnumerable<object> 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 _);
}

0 comments on commit c2dc672

Please sign in to comment.