Сделал модельки

This commit is contained in:
2025-02-07 18:55:21 +04:00
parent b785f00ba3
commit f565514739
13 changed files with 280 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
using TwoFromTheCasketContracts.Extensions;
using TwoFromTheCasketContracts.Exceptions;
using TwoFromTheCasketContracts.Infastructure;
namespace TwoFromTheCasketContracts.DataModels;
public class ComplitedWorkDataModel(string id, string workId, string roomId, List<WorkerComplitedWorkDataModel> workers ) :IValidation
{
public string Id { get; private set; } = id;
public string WorkId { get; private set; } = workId;
public string RoomId { get; private set; } = roomId;
public DateTime Date { get; private set; } = DateTime.Now;
public List<WorkerComplitedWorkDataModel> Workers { get; private set; } = workers;
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 (WorkId.IsEmpty())
throw new ValidationException("Field WorkId is empty");
if (!WorkId.IsGuid())
throw new ValidationException("The value in the field WorkId is not a unique identifier");
if (RoomId.IsEmpty())
throw new ValidationException("Field RoomId is empty");
if (!RoomId.IsGuid())
throw new ValidationException("The value in the field RoomId is not a unique identifier");
if((Workers?.Count ?? 0) == 0)
throw new ValidationException("The value in the Workers must include workers");
}
}

View File

@@ -0,0 +1,31 @@
using System.Text.RegularExpressions;
using TwoFromTheCasketContracts.Enums;
using TwoFromTheCasketContracts.Exceptions;
using TwoFromTheCasketContracts.Extensions;
using TwoFromTheCasketContracts.Infastructure;
namespace TwoFromTheCasketContracts.DataModels;
public class RoomsDataModel(string id, string address, double space, TypeRoom type) : IValidation
{
public string Id { get; private set; } = id;
public string Address { get; private set; } = address;
public double Space { get; private set; } = space;
public TypeRoom Type { get; private set; } = type;
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 (Address.IsEmpty())
throw new ValidationException("Field Address is empty");
if (!Regex.IsMatch(Address, @"^([А-ЯЁа-яё0-9\s.,-]+),\s?д\.\s?\d+(,\s?кв\.\s?\d+)?$"))
throw new ValidationException("Field Address is not address");
if (Space <= 0)
throw new ValidationException("Field Space is empty");
if(Type == TypeRoom.None)
throw new ValidationException("Field Type is empty");
}
}

View File

@@ -0,0 +1,28 @@
using TwoFromTheCasketContracts.Extensions;
using TwoFromTheCasketContracts.Exceptions;
using TwoFromTheCasketContracts.Infastructure;
namespace TwoFromTheCasketContracts.DataModels;
public class SalaryHistoryDataModel(string id, string workerId, double sum) : IValidation
{
public string Id { get; private set; } = id;
public string WorkerId { get; private set; } = workerId;
public double Sum { get; private set; } = sum;
public DateTime Date { get; private set; } = DateTime.Now;
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 empty");
}
}

View File

@@ -0,0 +1,30 @@
using TwoFromTheCasketContracts.Enums;
using TwoFromTheCasketContracts.Exceptions;
using TwoFromTheCasketContracts.Extensions;
using TwoFromTheCasketContracts.Infastructure;
namespace TwoFromTheCasketContracts.DataModels;
public class WorkDataModel(string id, TypeWork type, string description, DateTime date) : IValidation
{
public string Id { get; private set; } = id;
public TypeWork Type { get; private set; } = type;
public string Description { get; private set; } = description;
public DateTime Date { get; private set; } = date;
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 (Type == TypeWork.None)
throw new ValidationException("Field Type is empty");
if(Description.IsEmpty())
throw new ValidationException("Field Description is empty");
if (Date < DateTime.Now)
throw new ValidationException("Field Date is earlier than today");
}
}

View File

@@ -0,0 +1,28 @@
using System.Text.RegularExpressions;
using TwoFromTheCasketContracts.Exceptions;
using TwoFromTheCasketContracts.Extensions;
using TwoFromTheCasketContracts.Infastructure;
namespace TwoFromTheCasketContracts.DataModels;
public class WorkerComplitedWorkDataModel(string workerId, string complitedWorkId, double numberOfWorkingHours) : IValidation
{
public string WorkerId { get; private set; } = workerId;
public string ComplitedWorkId { get; private set; } = complitedWorkId;
public double NumberOfWorkingHours { get; private set; } = numberOfWorkingHours;
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 (ComplitedWorkId.IsEmpty())
throw new ValidationException("Field ComplitedWorkId is empty");
if (!ComplitedWorkId.IsGuid())
throw new ValidationException("The value in the field ComplitedWorkId is not a unique identifier");
if(NumberOfWorkingHours <= 0)
throw new ValidationException("Field NumberOfWorkingHours is empty");
}
}

View File

@@ -0,0 +1,43 @@
using TwoFromTheCasketContracts.Extensions;
using TwoFromTheCasketContracts.Exceptions;
using TwoFromTheCasketContracts.Infastructure;
using System.Text.RegularExpressions;
using TwoFromTheCasketContracts.Enums;
namespace TwoFromTheCasketContracts.DataModels;
public class WorkerDataModel(string id, string fio, Specializations specialization, double salary, string phoneNumber,
DateTime dateBirthday) : IValidation
{
public string Id { get; private set; } = id;
public string FIO { get; private set; } = fio;
public Specializations Specialization { get; private set; } = specialization;
public double Salary { get; private set; } = salary;
public string PhoneNumber { get; private set; } = phoneNumber;
public DateTime DateBirthDay { get; private set; } = dateBirthday;
public DateTime ValidFrom { get; private set; } = DateTime.Today;
public DateTime? ValidTo { get; private set; } = null;
public bool IsCurrent { get; private set; } = true;
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 (FIO.IsEmpty())
throw new ValidationException("Field FIO is empty");
if(Specialization == Specializations.None)
throw new ValidationException("Field Specialization is empty");
if(Salary <= 0)
throw new ValidationException("Field Salary is empty");
if (PhoneNumber.IsEmpty())
throw new ValidationException("Field PhoneNumber is empty");
if (!Regex.IsMatch(PhoneNumber, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\-]?)?[\d\- ]{7,10}$"))
throw new ValidationException("Field PhoneNumber is not a phone number");
if (DateBirthDay.Date > DateTime.Now.AddYears(-16).Date)
throw new ValidationException($"Minors cannot be hired (DateBirthDay = { DateBirthDay.ToShortDateString() })");
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TwoFromTheCasketContracts.Enums;
[Flags]
public enum Specializations
{
None = 0,
Plasterer = 1,
Painter = 2,
Carpenter = 4
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TwoFromTheCasketContracts.Enums;
public enum TypeRoom
{
None = 0,
Plasterer = 1,
Painter = 2,
Carpenter = 4,
Electrician = 8,
Plumber = 16,
Mason = 32
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TwoFromTheCasketContracts.Enums;
[Flags]
public enum TypeWork
{
None = 0,
Plaster = 1,
Painting = 2,
Carpentry = 4,
Electrical = 8,
Plumbing = 16
}

View File

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

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TwoFromTheCasketContracts.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,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TwoFromTheCasketContracts.Infastructure;
public interface IValidation
{
void Validate();
}