DataModels

This commit is contained in:
Павел Ладягин 2025-02-03 18:09:39 +04:00
parent 940056695c
commit bf8eece4e6
13 changed files with 271 additions and 0 deletions

View File

@ -0,0 +1,35 @@
using System.Text.RegularExpressions;
using PimpMyRideContracts.Exceptions;
using PimpMyRideContracts.Extensions;
using PimpMyRideContracts.Infrastructure;
namespace PimpMyRideContracts.DataModels;
public class CarDataModel(string carId, string carMark, string carModel, string carNumber) : IValidation
{
public string CarId { get; private set; } = carId;
public string CarMark { get; private set; } = carMark;
public string CarModel { get; private set; } = carModel;
public string CarNumber { get; private set; } = carNumber;
public void Validate()
{
if (CarId.IsEmpty())
throw new ValidationException("Field CarId is empty");
if (!CarId.IsGuid())
throw new ValidationException("The value in the field CarId is not a unique identifier");
if (CarMark.IsEmpty())
throw new ValidationException("Field CarMark is empty");
if (CarModel.IsEmpty())
throw new ValidationException("Field CarModel is empty");
if (CarNumber.IsEmpty())
throw new ValidationException("Field CarNumber is empty");
if (!Regex.IsMatch(CarNumber, @"^[АВЕКМНОРСТУХ]\d{3}(?<!000)[АВЕКМНОРСТУХ]{2}\d{2,3}$"))
throw new ValidationException("Field CarNumber is not a car number");
}
}

View File

@ -0,0 +1,25 @@
using PimpMyRideContracts.Enums;
using PimpMyRideContracts.Exceptions;
using PimpMyRideContracts.Extensions;
using PimpMyRideContracts.Infrastructure;
namespace PimpMyRideContracts.DataModels;
public class OperationDataModel(string operationId, OperationType operationType, List<OperationSupplieDataModel> supplies) : IValidation
{
public string OperationId { get; private set; } = operationId;
public OperationType OperationType { get; private set; } = operationType;
public List<OperationSupplieDataModel> Supplies { get; private set; } = supplies;
public void Validate()
{
if (OperationId.IsEmpty())
throw new ValidationException("Field OperationId is empty");
if (!OperationId.IsGuid())
throw new ValidationException("The value in the field OperationId is not a unique identifier");
if ((Supplies?.Count ?? 0) == 0)
throw new ValidationException("The operation must include supplies");
}
}

View File

@ -0,0 +1,30 @@
using PimpMyRideContracts.Exceptions;
using PimpMyRideContracts.Extensions;
using PimpMyRideContracts.Infrastructure;
namespace PimpMyRideContracts.DataModels;
public class OperationSupplieDataModel(string operationId, string supplieId, int supplieCount) : IValidation
{
public string OperationId { get; private set; } = operationId;
public string SupplieId { get; private set; } = supplieId;
public int SupplieCount { get; private set; } = supplieCount;
public void Validate()
{
if (OperationId.IsEmpty())
throw new ValidationException("Field OperationId is empty");
if (!OperationId.IsGuid())
throw new ValidationException("The value in the field OperationId is not a unique identifier");
if (SupplieId.IsEmpty())
throw new ValidationException("Field SupplieId is empty");
if (!SupplieId.IsGuid())
throw new ValidationException("The value in the field SupplieId is not a unique identifier");
if (SupplieCount <= 0)
throw new ValidationException("The value in field SupplieCount less ot equal 0");
}
}

View File

@ -0,0 +1,37 @@
using PimpMyRideContracts.Exceptions;
using PimpMyRideContracts.Extensions;
using PimpMyRideContracts.Infrastructure;
namespace PimpMyRideContracts.DataModels;
public class ServiceDataModel(string serviceId, string workerId, string carId, List<ServiceOperationDataModel> operations) : IValidation
{
public string ServiceId { get; private set; } = serviceId;
public string WorkerId { get; private set; } = workerId;
public string CarId { get; private set; } = carId;
public List<ServiceOperationDataModel> Operations { get; private set; } = operations;
public void Validate()
{
if (ServiceId.IsEmpty())
throw new ValidationException("Field ServiceId is empty");
if (!ServiceId.IsGuid())
throw new ValidationException("The value in the field ServiceId 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 (CarId.IsEmpty())
throw new ValidationException("Field CarId is empty");
if (!CarId.IsGuid())
throw new ValidationException("The value in the field CarId is not a unique identifier");
if ((Operations?.Count ?? 0) == 0)
throw new ValidationException("The service must include operations");
}
}

View File

@ -0,0 +1,26 @@
using PimpMyRideContracts.Exceptions;
using PimpMyRideContracts.Extensions;
using PimpMyRideContracts.Infrastructure;
namespace PimpMyRideContracts.DataModels;
public class ServiceOperationDataModel(string serviceId, string operationId) : IValidation
{
public string ServiceId { get; private set; } = serviceId;
public string OperationId { get; private set; } = operationId;
public void Validate()
{
if(OperationId.IsEmpty())
throw new ValidationException("Field OperationId is empty");
if (!OperationId.IsGuid())
throw new ValidationException("The value in the field OperationId is not a unique identifier");
if(ServiceId.IsEmpty())
throw new ValidationException("Field ServiceId is empty");
if (!ServiceId.IsGuid())
throw new ValidationException("The value in the field ServiceId is not a unique identifier");
}
}

View File

@ -0,0 +1,28 @@
using PimpMyRideContracts.Enums;
using PimpMyRideContracts.Exceptions;
using PimpMyRideContracts.Extensions;
using PimpMyRideContracts.Infrastructure;
namespace PimpMyRideContracts.DataModels;
public class SuppliesDataModel(string supplieId, string supplieName, SuppliesType supplieType) : IValidation
{
public string SupplieId { get; private set; } = supplieId;
public string SupplieName { get; private set; } = supplieName;
public SuppliesType SupplieType { get; private set; } = supplieType;
public void Validate()
{
if (SupplieId.IsEmpty())
throw new ValidationException("Field SupplieId is empty");
if (!SupplieId.IsGuid())
throw new ValidationException("The value in the field SupplieId is not a unique identifier");
if (SupplieName.IsEmpty())
throw new ValidationException("Field SupplieName is empty");
if (SupplieType == SuppliesType.None)
throw new ValidationException("Field SupplieType is empty");
}
}

View File

@ -0,0 +1,28 @@
using PimpMyRideContracts.Enums;
using PimpMyRideContracts.Exceptions;
using PimpMyRideContracts.Extensions;
using PimpMyRideContracts.Infrastructure;
namespace PimpMyRideContracts.DataModels;
public class WorkerDataModel(string workerId, string workerFIO, PostType postType) : IValidation
{
public string WorkerId { get; private set; } = workerId;
public string WorkerFIO { get; private set; } = workerFIO;
public PostType PostType { get; private set; } = postType;
public void Validate()
{
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 (WorkerFIO.IsEmpty())
throw new ValidationException("Field WorkerFIO is empty");
if (PostType == PostType.None)
throw new ValidationException("Field PostType is empty");
}
}

View File

@ -0,0 +1,11 @@
namespace PimpMyRideContracts.Enums;
public enum OperationType
{
None = 0,
TireReplacement = 1,
PistonReplacement = 2,
TimingBeltReplacing = 3,
OilChange = 4,
BodyPainting = 5
}

View File

@ -0,0 +1,13 @@
namespace PimpMyRideContracts.Enums;
public enum PostType
{
None = 0,
CarMechanic = 1,
CarElectrician = 2,
Straightener = 3,
BodyRepairSpecialist = 4,
TireFitter = 5,
CarDiagnosticTechnician = 6,
CarAirConditioningSpecialist = 7
}

View File

@ -0,0 +1,13 @@
namespace PimpMyRideContracts.Enums;
public enum SuppliesType
{
None = 0,
Suspension = 1,
Engine = 2,
Gearbox = 3,
Tires = 4,
Rims = 5,
Electronics = 6,
Body = 7
}

View File

@ -0,0 +1,5 @@
namespace PimpMyRideContracts.Exceptions;
public class ValidationException(string message) : Exception(message)
{
}

View File

@ -0,0 +1,14 @@
namespace PimpMyRideContracts.Extensions;
public static class StringExtensions
{
public static bool IsEmpty(this string str)
{
return string.IsNullOrWhiteSpace(str);
}
public static bool IsGuid(this string str)
{
return Guid.TryParse(str, out _);
}
}

View File

@ -0,0 +1,6 @@
namespace PimpMyRideContracts.Infrastructure;
public interface IValidation
{
void Validate();
}