I'm working on the UsersRepository

userValidotor has been added and tests are being developed
This commit is contained in:
Artemy
2025-06-17 11:57:57 +07:00
parent 5747f26421
commit 568b09ba81
12 changed files with 180 additions and 25 deletions
-1
View File
@@ -8,7 +8,6 @@
<ItemGroup>
<Folder Include="DTOs\" />
<Folder Include="Infrastructure\" />
</ItemGroup>
</Project>
+15
View File
@@ -0,0 +1,15 @@
namespace Govor.Core;
/// <summary>
/// Base exception class for Govor solutions
/// </summary>
public class GovorCoreException : Exception
{
public GovorCoreException() { }
public GovorCoreException(string message)
: base(message) { }
public GovorCoreException(string message, Exception innerException)
: base(message, innerException) { }
}
@@ -0,0 +1,9 @@
namespace Govor.Core.Infrastructure.Validators;
public interface IObjectValidator<T>
{
void Validate(T objectToValidate);
bool TryValidate(T objectToValidate);
}
class InvalidObjectException<T>(Exception ex) : GovorCoreException($"The object {typeof(T).FullName} is invalid.", ex);
@@ -0,0 +1,39 @@
using Govor.Core.Models;
using ArgumentNullException = System.ArgumentNullException;
namespace Govor.Core.Infrastructure.Validators;
public class UserValidator : IObjectValidator<User>
{
public void Validate(User user)
{
try
{
if (user is null)
throw new ArgumentNullException(nameof(user));
if(user.Id == Guid.Empty)
throw new ArgumentException("User ID cannot be empty", nameof(user.Id));
if(user.HashPassword is null || user.HashPassword == string.Empty)
throw new ArgumentException("Password cannot be empty", nameof(user.HashPassword));
if(user.CreatedOn == DateTime.MinValue)
throw new ArgumentException("Time of creation account cannot be empty", nameof(user.CreatedOn));
}
catch(Exception ex)
{
throw new InvalidObjectException<User>(ex);
}
}
public bool TryValidate(User objectToValidate)
{
try
{
Validate(objectToValidate);
return true;
}
catch (InvalidObjectException<User> ex)
{
return false;
}
}
}