49 lines
2.1 KiB
C#
49 lines
2.1 KiB
C#
using SmallSoftwareContracts.Exceptions;
|
|
using SmallSoftwareContracts.Extensions;
|
|
using SmallSoftwareContracts.Infrastructure;
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
namespace SmallSoftwareContracts.DataModels;
|
|
|
|
|
|
public class RequestDataModel(string id, string workerId, string email, double sum, bool isCancel, List<InstallationRequestDataModel> installationRequests) : IValidation
|
|
{
|
|
private readonly WorkerDataModel? _worker;
|
|
public string Id { get; private set; } = id;
|
|
public string WorkerId { get; private set; } = workerId;
|
|
public string Email { get; private set; } = email;
|
|
public double Sum { get; private set; } = sum;
|
|
public bool IsCancel { get; private set; } = isCancel;
|
|
public List<InstallationRequestDataModel> Softwares { get; private set; } = installationRequests;
|
|
public string WorkerFIO => _worker?.FIO ?? string.Empty;
|
|
|
|
public RequestDataModel(string id, string workerId, string email, double sum, bool isCancel, List<InstallationRequestDataModel> installationRequests, WorkerDataModel worker)
|
|
: this(id, workerId,email,sum,isCancel, installationRequests)
|
|
{
|
|
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 (Email.IsEmpty())
|
|
throw new ValidationException("Worker Email is empty");
|
|
if (!Regex.IsMatch(Email, @"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"))
|
|
throw new ValidationException("Field Email is not a email");
|
|
if (Sum <= 0)
|
|
throw new ValidationException("Field Sum is less than or equal to 0");
|
|
if ((Softwares?.Count ?? 0) == 0)
|
|
throw new ValidationException("The sale must include Softwares");
|
|
}
|
|
}
|