50 lines
1.9 KiB
C#
50 lines
1.9 KiB
C#
using Microsoft.Extensions.Localization;
|
|
using CandyHouseContracts.Exceptions;
|
|
using CandyHouseContracts.Extensions;
|
|
using CandyHouseContracts.Infrastructure;
|
|
using CandyHouseContracts.Resources;
|
|
|
|
namespace CandyHouseContracts.DataModels;
|
|
|
|
internal class SaleProductDataModel(string saleId, string productId, int count, double price) : IValidation
|
|
{
|
|
private readonly ProductDataModel? _product;
|
|
|
|
public string SaleId { get; private set; } = saleId;
|
|
|
|
public string ProductId { get; private set; } = productId;
|
|
|
|
public int Count { get; private set; } = count;
|
|
|
|
public double Price { get; private set; } = price;
|
|
|
|
public string ProductName => _product?.ProductName ?? string.Empty;
|
|
|
|
public SaleProductDataModel(string saleId, string productId, int count, double price, ProductDataModel product) : this(saleId, productId, count, price)
|
|
{
|
|
_product = product;
|
|
}
|
|
|
|
public SaleProductDataModel() : this(string.Empty, string.Empty, 0, 0) { }
|
|
|
|
public void Validate(IStringLocalizer<Messages> localizer)
|
|
{
|
|
if (SaleId.IsEmpty())
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "SaleId"));
|
|
|
|
if (!SaleId.IsGuid())
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "SaleId"));
|
|
|
|
if (ProductId.IsEmpty())
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "IProductIdd"));
|
|
|
|
if (!ProductId.IsGuid())
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "ProductId"));
|
|
|
|
if (Count <= 0)
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Count"));
|
|
|
|
if (Price <= 0)
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Price"));
|
|
}
|
|
} |