mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
I'm working on the UsersRepository
userValidotor has been added and tests are being developed
This commit is contained in:
@@ -8,7 +8,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="DTOs\" />
|
||||
<Folder Include="Infrastructure\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user