Files
Pibd-21_Semin_D.A._SmallSof…/SmallSoftwareProject/SmallSoftwareContracts/DataModels/RequestDataModel.cs
2025-04-10 09:07:45 +04:00

61 lines
2.3 KiB
C#

using SmallSoftwareContracts.Exceptions;
using SmallSoftwareContracts.Extensions;
using SmallSoftwareContracts.Infrastructure;
using System.Text.RegularExpressions;
using System.Xml;
namespace SmallSoftwareContracts.DataModels;
public 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()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (WorkerId.IsEmpty())
throw new ValidationException("Field WorkerId is empty");
if (!WorkerId.IsGuid())
throw new ValidationException("The value in the field WorkerId is not a unique identifier");
if (Sum <= 0)
throw new ValidationException("Field Sum is less than or equal to 0");
if ((Softwares?.Count ?? 0) == 0)
throw new ValidationException("The request must include products");
if (!Regex.IsMatch(Email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$"))
{
throw new ValidationException("Invalid email format");
}
}
}