PIbd-22_Khaliullov_R.M_ LabWork01 #1

Open
rakhaliullov wants to merge 2 commits from Task_1_Models into main
14 changed files with 336 additions and 0 deletions
Showing only changes of commit 31a0a4defe - Show all commits

View File

@ -0,0 +1,39 @@
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class CollectDataModel(string id, string workerId, string furnitureId, double sum) : IValidation
{
public string Id { get; private set; } = id;
public string WorkerId { get; private set; } = workerId;
public string FurnitureId { get; private set; } = furnitureId;
public DateTime SaleDate { get; private set; } = DateTime.UtcNow;
public double Sum { get; private set; } = sum;
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 (!FurnitureId?.IsGuid() ?? !FurnitureId?.IsEmpty() ?? false)
throw new ValidationException("The value in the field FurnitureId is not a unique identifier");
if (Sum <= 0)
throw new ValidationException("Field Sum is less than or equal to 0");
}
}

View File

@ -0,0 +1,37 @@
using SoftBedContracts.Enums;
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class FurnitureDataModel(string id, string furnitureName, FurnitureType furnitureType, double price, bool isDeleted) : IValidation
{
public string Id { get; private set; } = id;
public string FurnitureName { get; private set; } = furnitureName;
public FurnitureType FurnitureType { get; private set; } = furnitureType;
public double Price { get; private set; } = price;
public bool IsDeleted { get; private set; } = isDeleted;
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 (FurnitureName.IsEmpty())
throw new ValidationException("Field FurnitureName is empty");
if (FurnitureType == FurnitureType.None)
throw new ValidationException("Field FurnitureType is empty");
if (Price <= 0)
throw new ValidationException("Field Price is less than or equal to 0");
}
}

View File

@ -0,0 +1,26 @@
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class ProductHistoryDataModel(string furnitureId, double oldPrice) : IValidation
{
public string FurnitureId { get; private set; } = furnitureId;
public double OldPrice { get; private set; } = oldPrice;
public DateTime ChangeDate { get; private set; } = DateTime.UtcNow;
public void Validate()
{
if (FurnitureId.IsEmpty())
throw new ValidationException("Field FurnitureId is empty");
if (!FurnitureId.IsGuid())
throw new ValidationException("The value in the field FurnitureId is not a unique identifier");
if (OldPrice <= 0)
throw new ValidationException("Field OldPrice is less than or equal to 0");
}
}

View File

@ -0,0 +1,31 @@
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class FurnitureWorkPieceDataModel(string furnitureId, string workPieceId, int count) : IValidation
{
public string FurnitureId { get; private set; } = furnitureId;
public string WorkPieceId { get; private set; } = workPieceId;
public int Count { get; private set; } = count;
public void Validate()
{
if (FurnitureId.IsEmpty())
throw new ValidationException("Field FurnitureId is empty");
if (!FurnitureId.IsGuid())
throw new ValidationException("The value in the field FurnitureId is not a unique identifier");
if (WorkPieceId.IsEmpty())
throw new ValidationException("Field WorkPieceId is empty");
if (!WorkPieceId.IsGuid())
throw new ValidationException("The value in the field WorkPieceId is not a unique identifier");
if (Count <= 0)
throw new ValidationException("Field Count is less than or equal to 0");
}
}

View File

@ -0,0 +1,39 @@
using SoftBedContracts.Enums;
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class PostDataModel(string id, string postName, PostType postType, double salary, bool isActual, DateTime changeDate) : IValidation
{
public string Id { get; private set; } = id;
public string PostName { get; private set; } = postName;
public PostType PostType { get; private set; } = postType;
public double Salary { get; private set; } = salary;
public bool IsActual { get; private set; } = isActual;
public DateTime ChangeDate { get; private set; } = changeDate;
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 (PostName.IsEmpty())
throw new ValidationException("Field PostName is empty");
if (PostType == PostType.None)
throw new ValidationException("Field PostType is empty");
if (Salary <= 0)
throw new ValidationException("Field Salary is empty");
}
}

View File

@ -0,0 +1,26 @@
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class SalaryDataModel(string workerId, DateTime salaryDate, double salary) : IValidation
{
public string WorkerId { get; private set; } = workerId;
public DateTime SalaryDate { get; private set; } = salaryDate;
public double Salary { get; private set; } = salary;
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 (Salary <= 0)
throw new ValidationException("Field Salary is less than or equal to 0");
}
}

View File

@ -0,0 +1,38 @@
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
using System.Text.RegularExpressions;
namespace SoftBedContracts.DataModels;
public class SaleDataModel(string id, string size, double weight, List<FurnitureWorkPieceDataModel> workPieces) : IValidation
{
public string Id { get; private set; } = id;
public string Size { get; private set; } = size;
public double Weight { get; private set; } = weight;
public List<FurnitureWorkPieceDataModel> WorkPieces { get; private set; } = workPieces;
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 (Size.IsEmpty())
throw new ValidationException("Field Size is empty");
if (!Regex.IsMatch(Size, @"^\d+x\d+$"))
throw new ValidationException("Field Size has an invalid format. Expected format: number x number (e.g., '120x50').");
if (Weight <= 0)
throw new ValidationException("Field Weight is less than or equal to 0");
if ((WorkPieces?.Count ?? 0) == 0)
throw new ValidationException("The sale must include Work Pieces");
}
}

View File

@ -0,0 +1,45 @@
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class WorkerDataModel(string id, string fio, string postId, DateTime birthDate, DateTime employmentDate) : IValidation
{
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;
public DateTime EmploymentDate { get; private set; } = employmentDate;
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 (PostId.IsEmpty())
throw new ValidationException("Field PostId is empty");
if (!PostId.IsGuid())
throw new ValidationException("The value in the field PostId is not a unique identifier");
if (BirthDate.Date > DateTime.Now.AddYears(-18).Date)
throw new ValidationException($"Minors cannot be hired (BirthDate = {BirthDate.ToShortDateString()})");
if (EmploymentDate.Date < BirthDate.Date)
throw new ValidationException("The date of employment cannot be less than the date of birth");
if ((EmploymentDate - BirthDate).TotalDays / 365 < 18) // EmploymentDate.Year - BirthDate.Year
throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})");
}
}

View File

@ -0,0 +1,9 @@
namespace SoftBedContracts.Enums;
public enum FurnitureType
{
None = 0,
UpholsteredFurniture = 1,
CabinetFurniture = 2,
FoldingFurniture = 3
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoftBedContracts.Enums
{
internal class MaterialType
{
}
}

View File

@ -0,0 +1,9 @@
namespace SoftBedContracts.Enums;
public enum PostType
{
None = 0,
Assembler = 1,
Loader = 2,
Assistent = 3
}

View File

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

View File

@ -0,0 +1,14 @@
namespace SoftBedContracts.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 SoftBedContracts.Infrastructure;
public interface IValidation
{
void Validate();
}