more tests for user rep

This commit is contained in:
Artemy
2025-06-17 17:37:10 +07:00
parent b4b6d33432
commit 70e46e7648
6 changed files with 204 additions and 19 deletions
@@ -6,4 +6,4 @@ public interface IObjectValidator<T>
bool TryValidate(T objectToValidate);
}
class InvalidObjectException<T>(Exception ex) : GovorCoreException($"The object {typeof(T).FullName} is invalid.", ex);
public class InvalidObjectException<T>(Exception ex) : GovorCoreException($"The object {typeof(T).FullName} is invalid.", ex);
@@ -5,6 +5,8 @@ namespace Govor.Core.Infrastructure.Validators;
public class UserValidator : IObjectValidator<User>
{
public const int MIN_LENGHT_OF_NAME = 4;
public const int MAX_LENGHT_OF_NAME = 100;
public void Validate(User user)
{
try
@@ -13,9 +15,13 @@ public class UserValidator : IObjectValidator<User>
throw new ArgumentNullException(nameof(user));
if(user.Id == Guid.Empty)
throw new ArgumentException("User ID cannot be empty", nameof(user.Id));
if(user.Username is null
|| user.Username.Length < MIN_LENGHT_OF_NAME
|| user.Username.Length > MAX_LENGHT_OF_NAME)
throw new ArgumentException($"Username cannot be empty or less then {MIN_LENGHT_OF_NAME} chars or more then {MAX_LENGHT_OF_NAME}", nameof(user.Username));
if(user.HashPassword is null || user.HashPassword == string.Empty)
throw new ArgumentException("Password cannot be empty", nameof(user.HashPassword));
if(user.CreatedOn == DateTime.MinValue)
if(user.CreatedOn == DateOnly.MinValue)
throw new ArgumentException("Time of creation account cannot be empty", nameof(user.CreatedOn));
}
catch(Exception ex)
+3 -1
View File
@@ -1,3 +1,5 @@
using System.ComponentModel.DataAnnotations;
namespace Govor.Core.Models;
public class User
@@ -7,6 +9,6 @@ public class User
public string Description {get; set;}
public string HashPassword {get; set;}
public Guid IconId {get; set;}
public DateTime CreatedOn {get; set;}
public DateOnly CreatedOn {get; set;}
public DateTime WasOnline {get; set;}
}
+2 -2
View File
@@ -7,7 +7,7 @@ public interface IUsersReader
public Task<IEnumerable<User>> GetAll();
public Task<User> FindById(Guid id);
public Task<List<User>> FindByRangeId(IEnumerable<Guid> ids);
public Task<List<User>> FindUsersByName(string username);
public Task<List<User>> FindByRangeUsername(IEnumerable<string> usernames);
public Task<User> FindByUsername(string username);
public Task<List<User>> FindByRangeUsernames(IEnumerable<string> usernames);
public Task<List<User>> FindUsersByCreatedDate(DateOnly createdDate);
}