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

67 lines
2.6 KiB
C#

using Microsoft.Extensions.Localization;
using SmallSoftwareContracts.Exceptions;
using SmallSoftwareContracts.Extensions;
using SmallSoftwareContracts.Infrastructure;
using SmallSoftwareContracts.Resources;
using System.Text.RegularExpressions;
using System.Xml;
namespace SmallSoftwareContracts.DataModels;
internal class RequestDataModel : IValidation
{
private readonly WorkerDataModel? _worker;
public string Id { get; private set; }
public string WorkerId { get; private set; }
public DateTime RequestDate { get; private set; } = DateTime.UtcNow;
public string Email { get; private set; }
public double Sum { get; private set; }
public bool IsCancel { get; private set; }
public List<InstallationRequestDataModel>? Softwares { get; private set; }
public string WorkerFIO => _worker?.FIO ?? string.Empty;
public RequestDataModel(string id, string workerId, string email, bool isCancel, List<InstallationRequestDataModel> installationRequests, DateTime requestDate)
{
Id = id;
WorkerId = workerId;
Email = email;
IsCancel = isCancel;
Softwares = installationRequests;
Sum = Softwares?.Sum(x => x.Price * x.Count) ?? 0;
}
public RequestDataModel(string id, string workerId, string email, double sum, bool isCancel,
List<InstallationRequestDataModel> installationRequests, WorkerDataModel worker, DateTime requestDate)
: this(id, workerId, email, isCancel, installationRequests, requestDate)
{
Sum = sum;
_worker = worker;
}
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 (WorkerId.IsEmpty())
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "WorkerId"));
if (!WorkerId.IsGuid())
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "WorkerId"));
if (Sum <= 0)
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Sum"));
if ((Softwares?.Count ?? 0) == 0)
throw new ValidationException(localizer["ValidationExceptionMessageNoProductsInSale"]);
if (!Regex.IsMatch(Email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$"))
throw new ValidationException(localizer["ValidationExceptionMessageIncorrectEmail"]);
}
}