Skip to content

Commit

Permalink
feat : add currency validations
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikoo-Asadnejad committed Jan 12, 2024
1 parent 3e4c6a3 commit 57f421f
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

namespace Product.Domain.Aggregates.ProductAggregate;

public sealed class ProductModel : BaseEntity , IAggregateRoot
public sealed class ProductModel : BaseEntity, IAggregateRoot
{
public ProductModel(string title, int price, long? categoryId = null, string? subTitle = null,
string? description = null)
public ProductModel(string title,
Price price,
string subTitle = default,

Check warning on line 10 in Src/Product.Domain/Aggregates/Product/AggregateRoot/ProductModel.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
string description = default,

Check warning on line 11 in Src/Product.Domain/Aggregates/Product/AggregateRoot/ProductModel.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
CategoryId categoryId = default)

Check warning on line 12 in Src/Product.Domain/Aggregates/Product/AggregateRoot/ProductModel.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
Title = title;
SubTitle = subTitle;
Expand All @@ -18,18 +21,13 @@ public ProductModel(string title, int price, long? categoryId = null, string? su
public string Title { get; private set; }
public string? SubTitle { get; private set; }
public string? Description { get; private set; }
public int Price { get; private set; }
public long? CategoryId { get; private set; }

public CategoryModel Category { get; private set; }

public Price Price { get; private set; }
public CategoryId CategoryId { get; private set; }
public IReadOnlyList<ProductComment> ProductComments
{
get { return _productComments; }
}

private List<ProductComment> _productComments { get; set; }

public void Update(ProductModel newProduct)
{
base.Update(newProduct.CreateIp);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Product.Domain.Aggregates.ProductAggregate;

public class CommenterUser
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ namespace Product.Domain.Aggregates.Product.Entities;

public class ProductComment : BaseEntity
{

public string Body { get; set; }

Check warning on line 7 in Src/Product.Domain/Aggregates/Product/Entities/ProductComment.cs

View workflow job for this annotation

GitHub Actions / build

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Product.Domain.Aggregates.Product.Exceptions;

public class InvalidCurrencyException : Exception
{
public InvalidCurrencyException() : base(message: "Currency is not valid")
{

}
public InvalidCurrencyException(string msg) : base(message: msg)
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Product.Domain.Aggregates.Product.Exceptions;

public class InvalidPriceException : Exception
{
public InvalidPriceException() : base(message: "Price is not valid")
{

}
public InvalidPriceException(string msg) : base(message: msg)
{

}
}
14 changes: 13 additions & 1 deletion Src/Product.Domain/Aggregates/Product/ValueObjects/Currency.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
using Product.Domain.Aggregates.Product.Exceptions;
using Product.Domain.Base;

namespace Product.Domain.Aggregates.ProductAggregate;

public class Currency : ValueObject
{
public string Value { get; private set; }
public string Value { get; }

public Currency(string currency)
{
if (string.IsNullOrWhiteSpace(currency))
throw new InvalidCurrencyException();

if (currency.Length is not 3)
throw new InvalidCurrencyException("Currency lenght should be equal to 3");

Value = currency;
}
protected override IEnumerable<object> GetEqualityComponents()
{
throw new NotImplementedException();
Expand Down
26 changes: 12 additions & 14 deletions Src/Product.Domain/Aggregates/Product/ValueObjects/Price.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
using Product.Domain.Aggregates.Product.Exceptions;
using Product.Domain.Base;
using Product.Domain.Exceptions;

namespace Product.Domain.Aggregates.ProductAggregate;

public class Price : ValueObject
{
public int Value { get; private set; }
public Currency Currency { get; private set; }
public int Value { get; }
public Currency Currency { get; }

public Price()
public Price(int price , Currency currency)
{
if (price is 0)
throw new InvalidPriceException("price can not be 0");

if(price < 0)
throw new InvalidPriceException("price can not be negative");

Value = price;
Currency = currency;
}
protected override IEnumerable<object> GetEqualityComponents()
{
throw new NotImplementedException();
}

private void SetValue(int value)
{
if (value is 0)
throw new DomainValidationException("price can not be 0");

if(value < 0)
throw new DomainValidationException("price can not be negative");

Value = value;
}


}
1 change: 0 additions & 1 deletion Src/Product.Domain/Product.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<ItemGroup>
<Folder Include="Aggregates\Category\Exceptions" />
<Folder Include="Aggregates\Category\ValueObjects" />
<Folder Include="Aggregates\Product\Exceptions" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions Src/Product.Domain/Shared/Entity/UserModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Product.Domain.Base;

namespace Product.Domain.Shared.Entity;

public class UserModel : BaseEntity
{

}

0 comments on commit 57f421f

Please sign in to comment.