84 lines
4.2 KiB
C#
84 lines
4.2 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json;
|
|
using SmallSoftwareContracts.Exceptions;
|
|
using SmallSoftwareContracts.Extensions;
|
|
using SmallSoftwareContracts.Infrastructure;
|
|
using SmallSoftwareContracts.Infrastructure.PostConfigurations;
|
|
using Microsoft.Extensions.Localization;
|
|
using SmallSoftwareContracts.Resources;
|
|
|
|
namespace SmallSoftwareContracts.DataModels;
|
|
|
|
internal class WorkerDataModel(string id, string fio, string postId, DateTime birthDate, DateTime employmentDate, bool isDeleted, PostConfiguration configuration) : IValidation
|
|
{
|
|
private readonly PostDataModel? _post;
|
|
public string Id { get; private set; } = id;
|
|
public string FIO { get; private set; } = fio;
|
|
public string PostId { get; private set; } = postId;
|
|
public DateTime BirthDate { get; private set; } = birthDate.ToUniversalTime();
|
|
public DateTime EmploymentDate { get; private set; } = employmentDate.ToUniversalTime();
|
|
public bool IsDeleted { get; private set; } = isDeleted;
|
|
public string PostName => _post?.PostName ?? string.Empty;
|
|
public PostConfiguration ConfigurationModel { get; private set; } = configuration;
|
|
|
|
public WorkerDataModel(string id, string fio, string postId, DateTime
|
|
birthDate, DateTime employmentDate, bool isDeleted, PostDataModel post) :
|
|
this(id, fio, postId, birthDate, employmentDate, isDeleted, new PostConfiguration { Rate = 10 })
|
|
{
|
|
_post = post;
|
|
}
|
|
|
|
public WorkerDataModel(string id, string fio, string postId, DateTime birthDate, DateTime employmentDate)
|
|
: this(id, fio, postId, birthDate, employmentDate, false, new PostConfiguration { Rate = 10 })
|
|
{ }
|
|
|
|
public WorkerDataModel(string id, string fio, string postId, DateTime
|
|
birthDate, DateTime employmentDate, string configurationJson) :
|
|
this(id, fio, postId, birthDate, employmentDate, false, new PostConfiguration { Rate = 10 })
|
|
{
|
|
var obj = JToken.Parse(configurationJson);
|
|
if (obj is not null)
|
|
{
|
|
ConfigurationModel = obj.Value<string>("Type") switch
|
|
{
|
|
nameof(CashierPostConfiguration) => JsonConvert.DeserializeObject<CashierPostConfiguration>(configurationJson)!,
|
|
nameof(SupervisorPostConfiguration) => JsonConvert.DeserializeObject<SupervisorPostConfiguration>(configurationJson)!,
|
|
_ => JsonConvert.DeserializeObject<PostConfiguration>(configurationJson)!,
|
|
};
|
|
}
|
|
}
|
|
|
|
public void Validate(IStringLocalizer<Messages> localizer)
|
|
{
|
|
if (Id.IsEmpty())
|
|
throw new ValidationException(localizer[Messages.ValidationExceptionMessageEmptyField, nameof(Id)]);
|
|
|
|
if (!Id.IsGuid())
|
|
throw new ValidationException(localizer[Messages.ValidationExceptionMessageNotAId, nameof(Id)]);
|
|
|
|
if (FIO.IsEmpty())
|
|
throw new ValidationException(localizer[Messages.ValidationExceptionMessageEmptyField, nameof(FIO)]);
|
|
|
|
if (PostId.IsEmpty())
|
|
throw new ValidationException(localizer[Messages.ValidationExceptionMessageEmptyField, nameof(PostId)]);
|
|
|
|
if (!PostId.IsGuid())
|
|
throw new ValidationException(localizer[Messages.ValidationExceptionMessageNotAId, nameof(PostId)]);
|
|
|
|
if (BirthDate.Date > DateTime.Now.AddYears(-16).Date)
|
|
throw new ValidationException(localizer[Messages.ValidationExceptionMessageMinorsBirthDate, BirthDate.ToShortDateString()]);
|
|
|
|
if (EmploymentDate.Date < BirthDate.Date)
|
|
throw new ValidationException(localizer[Messages.ValidationExceptionMessageEmploymentDateAndBirthDate, EmploymentDate.ToShortDateString(), BirthDate.ToShortDateString()]);
|
|
|
|
if ((EmploymentDate - BirthDate).TotalDays / 365 < 16)
|
|
throw new ValidationException(localizer[Messages.ValidationExceptionMessageMinorsEmploymentDate, EmploymentDate.ToShortDateString(), BirthDate.ToShortDateString()]);
|
|
|
|
if (ConfigurationModel is null)
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotInitialized"], "ConfigurationModel"));
|
|
|
|
if (ConfigurationModel!.Rate <= 0)
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Rate"));
|
|
}
|
|
}
|