Files
Pibd-21_Semin_D.A._SmallSof…/SmallSoftwareProject/SmallSoftwareContracts/DataModels/SoftwareDataModel.cs
2025-04-24 00:06:05 +04:00

57 lines
2.7 KiB
C#

using Microsoft.Extensions.Localization;
using SmallSoftwareContracts.Enums;
using SmallSoftwareContracts.Exceptions;
using SmallSoftwareContracts.Extensions;
using SmallSoftwareContracts.Infrastructure;
using SmallSoftwareContracts.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace SmallSoftwareContracts.DataModels;
internal class SoftwareDataModel(string id, string softwareName, SoftwareType softwareType, string manufacturerId, double price, bool isDeleted) : IValidation
{
private readonly ManufacturerDataModel? _manufacturer;
public string Id { get; private set; } = id;
public string SoftwareName { get; private set; } = softwareName;
public SoftwareType SoftwareType { get; private set; } = softwareType;
public string ManufacturerId { get; private set; } = manufacturerId;
public double Price { get; private set; } = price;
public bool IsDeleted { get; private set; } = isDeleted;
public string ManufacturerName => _manufacturer?.ManufacturerName ?? string.Empty;
public SoftwareDataModel(string id, string softwareName, SoftwareType
softwareType, string manufacturerId, double price, bool isDeleted,
ManufacturerDataModel manufacturer) : this(id, softwareName, softwareType,
manufacturerId, price, isDeleted)
{
_manufacturer = manufacturer;
}
public SoftwareDataModel(string id, string softwareName, SoftwareType
softwareType, string manufacturerId, double price) : this(id, softwareName,
softwareType, manufacturerId, price, false)
{ }
public void Validate(IStringLocalizer<Messages> localizer)
{
if (Id.IsEmpty())
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "Id"));
if (!Id.IsGuid())
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "Id"));
if (SoftwareName.IsEmpty())
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "SoftwareName"));
if (SoftwareType == SoftwareType.None)
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "SoftwareType"));
if (ManufacturerId.IsEmpty())
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "ManufacturerId"));
if (!ManufacturerId.IsGuid())
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "ManufacturerId"));
if (Price <= 0)
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Price"));
}
}