119 lines
4.9 KiB
C#
119 lines
4.9 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json;
|
|
using PimpMyRideContracts.Enums;
|
|
using PimpMyRideContracts.Exceptions;
|
|
using PimpMyRideContracts.Extensions;
|
|
using PimpMyRideContracts.Infrastructure;
|
|
using PimpMyRideContracts.Infrastructure.ServiceConfiguration;
|
|
using Microsoft.Extensions.Localization;
|
|
using PimpMyRideContracts.Resources;
|
|
using PimpMyRideContracts.Mapper;
|
|
|
|
namespace PimpMyRideContracts.DataModels;
|
|
|
|
internal class ServiceDataModel : IValidation
|
|
{
|
|
[SourceFromMember(SourcePropertyName = "Worker")]
|
|
private readonly WorkerDataModel? _worker;
|
|
|
|
public string Id { get; private set; }
|
|
|
|
public string WorkerId { get; private set; }
|
|
|
|
public WorkType WorkType { get; private set; }
|
|
|
|
[AlternativeName("Configuration")]
|
|
[AlternativeName("ConfigurationJson")]
|
|
[PostProcessing(MappingCallMethodName = "ParseJson")]
|
|
public ServiceConfiguration ConfigurationModel { get; private set; }
|
|
|
|
[AlternativeName("ServiceMaterials")]
|
|
public List<ServiceMaterialDataModel> Materials { get; private set; }
|
|
|
|
[IgnoreValue]
|
|
public DateTime ServiceDate { get; private set; } = DateTime.UtcNow;
|
|
|
|
public string WorkerFIO => _worker?.FIO ?? string.Empty;
|
|
|
|
public ServiceDataModel(string id, string workerId, WorkType workType, ServiceConfiguration configurationModel, List<ServiceMaterialDataModel> serviceMaterials)
|
|
{
|
|
Id = id;
|
|
WorkerId = workerId;
|
|
WorkType = workType;
|
|
ConfigurationModel = configurationModel;
|
|
Materials = serviceMaterials;
|
|
}
|
|
|
|
public ServiceDataModel(string id, string workerId, WorkType workType, ServiceConfiguration configurationModel, List<ServiceMaterialDataModel> serviceMaterials, DateTime serviceDate)
|
|
{
|
|
Id = id;
|
|
WorkerId = workerId;
|
|
WorkType = workType;
|
|
ConfigurationModel = configurationModel;
|
|
Materials = serviceMaterials;
|
|
ServiceDate = serviceDate;
|
|
}
|
|
|
|
public ServiceDataModel(string id, string workerId, WorkType workType, ServiceConfiguration configurationModel, List<ServiceMaterialDataModel> serviceMaterials, WorkerDataModel worker, DateTime serviceDate) : this(id, workerId, workType, configurationModel, serviceMaterials, serviceDate)
|
|
{
|
|
_worker = worker;
|
|
}
|
|
|
|
public ServiceDataModel(string id, string workerId, WorkType workType, ServiceConfiguration configurationModel, List<ServiceMaterialDataModel> serviceMaterials, WorkerDataModel worker) : this(id, workerId, workType, configurationModel, serviceMaterials)
|
|
{
|
|
_worker = worker;
|
|
}
|
|
|
|
public ServiceDataModel(string id, string workerId, int workType, ServiceConfiguration configurationModel, List<ServiceMaterialDataModel> materials) : this(id, workerId, (WorkType)workType, configurationModel, materials) { }
|
|
|
|
public ServiceDataModel() : this(string.Empty, string.Empty, WorkType.None, (ServiceConfiguration)null, new List<ServiceMaterialDataModel>()) { }
|
|
|
|
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["ValidationExceptionMessageNotAnId"], "Id"));
|
|
|
|
if (WorkerId.IsEmpty())
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "WorkerId"));
|
|
|
|
if (!WorkerId.IsGuid())
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAnId"], "WorkerId"));
|
|
|
|
if (WorkType == WorkType.None)
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "WorkType"));
|
|
|
|
if (ConfigurationModel is null)
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotInitialized"], "ConfigurationModel"));
|
|
|
|
if (ConfigurationModel!.Rate <= 0)
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Rate"));
|
|
|
|
if ((Materials?.Count ?? 0) == 0)
|
|
throw new ValidationException(localizer["ValidationExceptionMessageNoMaterialsInService"]);
|
|
}
|
|
|
|
private ServiceConfiguration? ParseJson(string json)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(json)) return null;
|
|
|
|
var obj = JToken.Parse(json);
|
|
var type = obj.Value<string>("Type");
|
|
switch (type)
|
|
{
|
|
case nameof(EngineServiceConfiguration):
|
|
ConfigurationModel = JsonConvert.DeserializeObject<EngineServiceConfiguration>(json);
|
|
break;
|
|
case nameof(WheelsServiceConfiguration):
|
|
ConfigurationModel = JsonConvert.DeserializeObject<WheelsServiceConfiguration>(json);
|
|
break;
|
|
default:
|
|
ConfigurationModel = JsonConvert.DeserializeObject<ServiceConfiguration>(json);
|
|
break;
|
|
}
|
|
return ConfigurationModel;
|
|
}
|
|
}
|