4 Commits

21 changed files with 775 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAPContracts.Exceptions;
using YAPContracts.Extentions;
using YAPContracts.Infrastructure;
namespace YAPContracts.DataModels
{
public class CommentDataModel(string id, string productSetId, string userId, string text, DateTime date) : IValidation
{
public string Id { get; private set; } = id;
public string ProductSetId { get; private set; } = productSetId;
public string UserId { get; private set; } = userId;
public string Text { get; private set; } = text;
public DateTime Date { get; private set; } = date;
public void Validate()
{
if (Id.IsEmpty())
{
throw new ValidationException("Id is empty");
}
if (!Id.IsGuid())
{
throw new ValidationException("Id value is not valid");
}
if (ProductSetId.IsEmpty())
{
throw new ValidationException("ProductSetId is empty");
}
if (!ProductSetId.IsGuid())
{
throw new ValidationException("ProductSetId value is not valid");
}
if (UserId.IsEmpty())
{
throw new ValidationException("UserId is empty");
}
if (!UserId.IsGuid())
{
throw new ValidationException("UserId value is not valid");
}
if (Text.IsEmpty())
{
throw new ValidationException("Text is empty");
}
}
}
}

View File

@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAPContracts.Enums;
using YAPContracts.Exceptions;
using YAPContracts.Extentions;
using YAPContracts.Infrastructure;
namespace YAPContracts.DataModels;
public class ComponentDataModel(string id, string name, ComponentType componentType) : IValidation
{
public string Id { get; private set; } = id;
public string Name { get; private set; } = name;
public ComponentType ComponentType { get; private set; } = componentType;
public void Validate()
{
if (!Id.IsGuid())
{
throw new ValidationException("Id value is not valid");
}
if (Id.IsEmpty())
{
throw new ValidationException("Id is empty");
}
if (Name.IsEmpty())
{
throw new ValidationException("Name is empty");
}
if (componentType == ComponentType.None)
{
throw new ValidationException("Product type is empty");
}
}
}

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAPContracts.Exceptions;
using YAPContracts.Extentions;
using YAPContracts.Infrastructure;
namespace YAPContracts.DataModels;
public class ComponentInProductDataModel(string componentId, string productId) : IValidation
{
public string ComponentId { get; set; } = componentId;
public string ProductId { get; set; } = productId;
public void Validate()
{
if(ComponentId.IsEmpty())
{
throw new ValidationException("ComponentId is empty");
}
if (!ComponentId.IsGuid())
{
throw new ValidationException("ComponentId value is not valid");
}
if (ProductId.IsEmpty())
{
throw new ValidationException("ProductId is empty");
}
if (!ProductId.IsGuid())
{
throw new ValidationException("ProductId value is not valid");
}
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAPContracts.Exceptions;
using YAPContracts.Extentions;
using YAPContracts.Infrastructure;
namespace YAPContracts.DataModels
{
public class ComponentInProductSetDataModel(string componentId, string productSetId, int amount) : IValidation
{
public string ComponentId { get; private set; } = componentId;
public string ProductSetId { get; private set; } = productSetId;
public int Amount { get; private set; } = amount;
public void Validate()
{
if (ComponentId.IsEmpty())
{
throw new ValidationException("ComponentId is empty");
}
if (!ComponentId.IsGuid())
{
throw new ValidationException("ComponentId value is not valid");
}
if (ProductSetId.IsEmpty())
{
throw new ValidationException("ProductSetId is empty");
}
if (!ProductSetId.IsGuid())
{
throw new ValidationException("ProductSetId value is not valid");
}
if (Amount <= 0)
{
throw new ValidationException("Amount is less than or equal to 0");
}
}
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using YAPContracts.Exceptions;
using YAPContracts.Extentions;
using YAPContracts.Infrastructure;
namespace YAPContracts.DataModels;
public class ProductDataModel(string id, string name, double price, List<ComponentInProductDataModel> componentProducts) : IValidation
{
public string Id { get; private set; } = id;
public string Name { get; private set; } = name;
public double Price { get; private set; } = price;
public List<ComponentInProductDataModel> ComponentProducts { get; private set; } = componentProducts;
public void Validate()
{
if (!Id.IsGuid())
{
throw new ValidationException("Id value is not valid");
}
if (Id.IsEmpty())
{
throw new ValidationException("Id is empty");
}
if (Name.IsEmpty())
{
throw new ValidationException("Name is empty");
}
if (Price <= 0)
{
throw new ValidationException("Price is less than or equal to 0");
}
foreach (var component in ComponentProducts)
{
component.Validate();
}
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAPContracts.Extentions;
using YAPContracts.Infrastructure;
using YAPContracts.Exceptions;
namespace YAPContracts.DataModels
{
public class ProductInPurchaseDataModel(string productId, string purchaseId, int amount) : IValidation
{
public string ProductId { get; private set; } = productId;
public string PurchaseId { get; private set; } = purchaseId;
public int Amount { get; private set; } = amount;
public void Validate()
{
if (ProductId.IsEmpty())
{
throw new ValidationException("ProductId is empty");
}
if (!ProductId.IsGuid())
{
throw new ValidationException("ProductId value is not valid");
}
if (PurchaseId.IsEmpty())
{
throw new ValidationException("PurchaseId is empty");
}
if (!PurchaseId.IsGuid())
{
throw new ValidationException("PurchaseId value is not valid");
}
if (Amount <= 0)
{
throw new ValidationException("Amount is less than or equal to 0");
}
}
}
}

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAPContracts.Exceptions;
using YAPContracts.Extentions;
using YAPContracts.Infrastructure;
namespace YAPContracts.DataModels;
public class ProductOrder(string id, DateTime orderDate, string dealerName, string productId) : IValidation
{
public string Id { get; private set; } = id;
public DateTime OrderDate { get; private set;} = orderDate;
public string DealerName { get; private set; } = dealerName;
public string ProductId { get; private set;} = productId;
public void Validate()
{
if (Id.IsEmpty())
{
throw new ValidationException("Id is empty");
}
if (!Id.IsGuid())
{
throw new ValidationException("Id value is not valid");
}
if (ProductId.IsEmpty())
{
throw new ValidationException("ProductSetId is empty");
}
if (!ProductId.IsGuid())
{
throw new ValidationException("ProductSetId value is not valid");
}
if (DealerName.IsEmpty())
{
throw new ValidationException("Dealer name is empty");
}
}
}

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAPContracts.Exceptions;
using YAPContracts.Extentions;
using YAPContracts.Infrastructure;
namespace YAPContracts.DataModels
{
public class ProductSetDataModel(string id, string setName, double totalPrice) : IValidation
{
public string Id { get; private set; } = id;
public string SetName { get; private set; } = setName;
public double TotalPrice { get; private set; } = totalPrice;
public void Validate()
{
if (Id.IsEmpty())
{
throw new ValidationException("Id is empty");
}
if (!Id.IsGuid())
{
throw new ValidationException("Id value is not valid");
}
if (TotalPrice <= 0)
{
throw new ValidationException("TotalPrice is less than or equal to 0");
}
}
}
}

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAPContracts.Extentions;
using YAPContracts.Infrastructure;
using YAPContracts.Exceptions;
namespace YAPContracts.DataModels
{
public class ProductSetInPurchaseDataModel(string purchaseId, string productSetId, int amount, double? discount) : IValidation
{
public string PurchaseId { get; private set; } = purchaseId;
public string ProductSetId { get; private set; } = productSetId;
public int Amount { get; private set; } = amount;
public double? Discount { get; private set; } = discount;
public void Validate()
{
if (PurchaseId.IsEmpty())
{
throw new ValidationException("PurchaseId is empty");
}
if (!PurchaseId.IsGuid())
{
throw new ValidationException("PurchaseId value is not valid");
}
if (ProductSetId.IsEmpty())
{
throw new ValidationException("ProductSetId is empty");
}
if (!ProductSetId.IsGuid())
{
throw new ValidationException("ProductSetId value is not valid");
}
if (Amount <= 0)
{
throw new ValidationException("Amount is less than or equal to 0");
}
if (Discount.HasValue && Discount < 0)
{
throw new ValidationException("Discount is less than 0");
}
}
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAPContracts.Exceptions;
using YAPContracts.Extentions;
using YAPContracts.Infrastructure;
namespace YAPContracts.DataModels
{
public class PurchaseDataModel(string id, double totalPrice, DateTime purchaseDate, List<ProductSetInPurchaseDataModel> productSets) : IValidation
{
public string Id { get; private set; } = id;
public double TotalPrice { get; private set; } = totalPrice;
public DateTime PurchaseDate { get; private set; } = purchaseDate;
public List<ProductSetInPurchaseDataModel> ProductSets = productSets;
public void Validate()
{
if (!Id.IsGuid())
{
throw new ValidationException("Id value is not valid");
}
if (Id.IsEmpty())
{
throw new ValidationException("Id is empty");
}
if (TotalPrice <= 0)
{
throw new ValidationException("TotalPrice is less than or equal to 0");
}
foreach (var productSet in ProductSets)
{
productSet.Validate();
}
}
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YAPContracts.DataModels.Roles
{
public class StorekeeperDataModel : UserDataModel
{
// storekeeper specific properties here
public StorekeeperDataModel(string id, string login, string email, string passwordHash, string role) : base(id, login, email, passwordHash, "Storekeeper")
{
}
public new void Validate()
{
base.Validate();
}
}
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAPContracts.Infrastructure;
namespace YAPContracts.DataModels.Roles
{
public class WorkerDataModel : UserDataModel
{
public List<PurchaseDataModel> Purchases { get; private set; }
public List<CommentDataModel> Comments { get; private set; }
public WorkerDataModel(string id, string login, string email, string passwordHash, List<PurchaseDataModel> purchases, List<CommentDataModel> comments) : base(id, login, email, passwordHash, "Worker")
{
Purchases = purchases;
Comments = comments;
}
public new void Validate()
{
base.Validate();
foreach (var purchase in Purchases)
{
purchase.Validate();
}
foreach (var comment in Comments)
{
comment.Validate();
}
}
}
}

View File

@@ -0,0 +1,56 @@
using System.Text.RegularExpressions;
using YAPContracts.Extentions;
using YAPContracts.Infrastructure;
namespace YAPContracts.DataModels
{
public class UserDataModel(string id, string login, string email, string passwordHash, string role) : IValidation
{
public string Id { get; set; } = id;
public string Login { get; set; } = login;
public string Email { get; set; } = email;
public string PasswordHash { get; set; } = passwordHash;
public string Role { get; set; } = role; // "Worker", "Storekeeper"
public void Validate()
{
if (Id.IsEmpty())
{
throw new Exceptions.ValidationException("Id is empty");
}
if (!Id.IsGuid())
{
throw new Exceptions.ValidationException("Id value is not valid");
}
if (Login.IsEmpty())
{
throw new Exceptions.ValidationException("Login is empty");
}
if (!Regex.IsMatch(Login, @"^[a-zA-Z0-9_-]+$"))
{
throw new Exceptions.ValidationException("Login value is not valid");
}
if (Email.IsEmpty())
{
throw new Exceptions.ValidationException("Email is empty");
}
if (!Regex.IsMatch(Email, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"))
{
throw new Exceptions.ValidationException("Email value is not valid");
}
if (PasswordHash.IsEmpty())
{
throw new Exceptions.ValidationException("PasswordHash is empty");
}
if (Role.IsEmpty())
{
throw new Exceptions.ValidationException("Role is empty");
}
if (!Role.IsValidRole())
{
throw new Exceptions.ValidationException("Role value is not valid");
}
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YAPContracts.Enums;
public enum ComponentType
{
None = 0,
Accessory = 1,
Trial = 2,
SparePart = 3
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YAPContracts.Exceptions
{
public class ValidationException(string message) : Exception(message)
{
}
}

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YAPContracts.Extentions
{
public static class StringExtention
{
public static bool IsEmpty(this string str)
{
return string.IsNullOrWhiteSpace(str);
}
public static bool IsGuid(this string str)
{
return Guid.TryParse(str, out _);
}
public static bool IsValidRole(this string str)
{
return str == "Worker" || str == "Storekeeper";
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YAPContracts.Infrastructure
{
public interface IValidation
{
void Validate();
}
}

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,114 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAPContracts.DataModels;
using YAPContracts.DataModels.Roles;
using YAPContracts.Exceptions;
namespace YAPTests.DataModelTests
{
[TestFixture]
internal class UserDataModelTest
{
[Test]
public void IdIsNullOrEmptyTest()
{
var user = CreateDataModel(null, "test", "mail@mail.ru", "hash", "Worker");
Assert.That(() => user.Validate(), Throws.TypeOf<ValidationException>());
user = CreateDataModel(string.Empty, "test", "mail@mail.ru", "hash", "Worker");
Assert.That(() => user.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var user = CreateDataModel("id", "test", "mail@mail.ru", "hash", "Worker");
Assert.That(() => user.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void LoginIsNullOrEmptyTest()
{
var user = CreateDataModel(Guid.NewGuid().ToString(), null, "mail@mail.ru", "hash", "Worker");
Assert.That(() => user.Validate(), Throws.TypeOf<ValidationException>());
user = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "mail@mail.ru", "hash", "Worker");
Assert.That(() => user.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void LoginIsIncorrectTest()
{
var user = CreateDataModel(Guid.NewGuid().ToString(), "+", "mail@mail.ru", "hash", "Worker");
Assert.That(() => user.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void EmailIsNullOrEmptyTest()
{
var user = CreateDataModel(Guid.NewGuid().ToString(), "login", null, "hash", "Worker");
Assert.That(() => user.Validate(), Throws.TypeOf<ValidationException>());
user = CreateDataModel(Guid.NewGuid().ToString(), "login", string.Empty, "hash", "Worker");
Assert.That(() => user.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void EmailIsIncorrectTest()
{
var user = CreateDataModel(Guid.NewGuid().ToString(), "login", "mail", "hash", "Worker");
Assert.That(() => user.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PasswordHashIsNullOrEmptyTest()
{
var user = CreateDataModel(Guid.NewGuid().ToString(), "login", "mail@mail.ru", null, "Worker");
Assert.That(() => user.Validate(), Throws.TypeOf<ValidationException>());
user = CreateDataModel(Guid.NewGuid().ToString(), "login", "mail@mail.ru", string.Empty, "Worker");
Assert.That(() => user.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void Worker_AllFieldsAreCorrectTest()
{
var userId = Guid.NewGuid().ToString();
List<CommentDataModel> comments = new List<CommentDataModel>() { new CommentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), userId, "text", DateTime.UtcNow) };
List<PurchaseDataModel> purchases = new List<PurchaseDataModel>() { new PurchaseDataModel(Guid.NewGuid().ToString(), 100, DateTime.UtcNow, []) };
var worker = new WorkerDataModel(userId, "login", "mail@mail.ru", "hash", purchases, comments);
Assert.That(() => worker.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(worker.Role, Is.EqualTo("Worker"));
Assert.That(worker.Purchases, Is.EqualTo(purchases));
Assert.That(worker.Comments, Is.EqualTo(comments));
});
}
[Test]
public void AllFieldsAreCorrectTest()
{
var userId = Guid.NewGuid().ToString();
var login = "name";
var email = "mail@mail.ru";
var passwordHash = "hash";
var role = "Worker";
var user = CreateDataModel(userId, login, email, passwordHash, role);
Assert.That(() => user.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(user.Id, Is.EqualTo(userId));
Assert.That(user.Login, Is.EqualTo(login));
Assert.That(user.Email, Is.EqualTo(email));
Assert.That(user.PasswordHash, Is.EqualTo(passwordHash));
Assert.That(user.Role, Is.EqualTo(role));
});
}
private static UserDataModel CreateDataModel(string? id, string? login, string? email, string? passwordHash, string? role) =>
new(id, login, email, passwordHash, role);
}
}

View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../YAPContracts/YAPContracts.csproj"/>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35931.197
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YAPContracts", "YAPContracts\YAPContracts.csproj", "{6F6A9D44-726A-44D7-83EA-4268D3205F2B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YAPTests", "YAPTests\YAPTests.csproj", "{DE74A5B9-3323-4DC9-8772-309A913012CE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6F6A9D44-726A-44D7-83EA-4268D3205F2B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F6A9D44-726A-44D7-83EA-4268D3205F2B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F6A9D44-726A-44D7-83EA-4268D3205F2B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F6A9D44-726A-44D7-83EA-4268D3205F2B}.Release|Any CPU.Build.0 = Release|Any CPU
{DE74A5B9-3323-4DC9-8772-309A913012CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE74A5B9-3323-4DC9-8772-309A913012CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE74A5B9-3323-4DC9-8772-309A913012CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE74A5B9-3323-4DC9-8772-309A913012CE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B9E91F39-FC9A-4616-A00D-01D594866CCF}
EndGlobalSection
EndGlobal