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
+81 -14
View File
@@ -22,11 +22,14 @@ public class UsersRepository : IUsersRepository
return await _context.Users
.AsNoTracking()
.Where(x => true)
.ToListAsync();
.ToListOrThrowIfEmpty(new NotFoundException("Users in Database not exists"));
}
public async Task<User> FindById(Guid id)
{
if(id == Guid.Empty)
throw new ArgumentException("Id must not be empty", nameof(id));
return await _context.Users
.AsNoTracking()
.FirstOrDefaultAsync(x => x.Id == id)
@@ -35,45 +38,101 @@ public class UsersRepository : IUsersRepository
public async Task<List<User>> FindByRangeId(IEnumerable<Guid> ids)
{
if (ids is null || !ids.Any())
throw new ArgumentException("Ids must not be empty", nameof(ids));
return await _context.Users
.AsNoTracking()
.Where(x => ids.Contains(x.Id))
.ToListOrThrowIfEmpty(new NotFoundByKeyException<IEnumerable<Guid>>(ids,"Users with given ids not found"));
}
public async Task<List<User>> FindUsersByName(string username)
public async Task<User> FindByUsername(string username)
{
if(username is null || username == string.Empty)
throw new ArgumentNullException(username, "Username cannot be empty");
return await _context.Users
.AsNoTracking()
.Where(x => x.Username == username)
.ToListOrThrowIfEmpty(new NotFoundByKeyException<string>(username, "Users with given username not found"));
.FirstOrDefaultAsync(x => x.Username == username)
?? throw new NotFoundByKeyException<string>(username, "User with given username does not exist");
}
public async Task<List<User>> FindByRangeUsername(IEnumerable<string> usernames)
public async Task<List<User>> FindByRangeUsernames(IEnumerable<string> usernames)
{
if (usernames is null || !usernames.Any())
throw new ArgumentException("Usernames must not be empty", nameof(usernames));
return await _context.Users
.AsNoTracking()
.Where(x => usernames.Contains(x.Username))
.ToListOrThrowIfEmpty(new NotFoundByKeyException<IEnumerable<string>>(usernames, "Users with given usernames not found"));
}
public Task<List<User>> FindUsersByCreatedDate(DateOnly createdDate)
public async Task<List<User>> FindUsersByCreatedDate(DateOnly createdDate)
{
throw new NotImplementedException();
if(createdDate == DateOnly.MinValue)
throw new ArgumentException("Created date cannot be earlier than MinValue", nameof(createdDate));
return await _context.Users
.AsNoTracking()
.Where(u => createdDate == u.CreatedOn)
.ToListOrThrowIfEmpty(new NotFoundByKeyException<DateOnly>(createdDate, "Users with given created date do not exist"));
}
public async Task Add(User user)
{
try
{
_validator.Validate(user);
_context.Users.Add(user);
await _context.SaveChangesAsync();
}
catch (InvalidObjectException<User> ex)
{
throw new AdditionUserException("User with given data invalid", ex);
}
catch (Exception ex)
{
throw new AdditionUserException("Cannot add user", ex);
}
}
public Task Add(User user)
public async Task Update(User user)
{
throw new NotImplementedException();
}
try
{
_validator.Validate(user);
public Task Update(User user)
{
throw new NotImplementedException();
var rowsAffected = await _context.Users
.Where(u => u.Id == user.Id)
.ExecuteUpdateAsync(u => u
.SetProperty(a => a.Username, user.Username)
.SetProperty(u => u.IconId, user.IconId)
.SetProperty(u => u.Description, user.Description)
.SetProperty(u => u.CreatedOn, user.CreatedOn)
.SetProperty(u => u.HashPassword, user.HashPassword)
.SetProperty(u => u.WasOnline, user.WasOnline)
);
if (rowsAffected == 0)
throw new NotFoundByKeyException<Guid>(user.Id);
}
catch (NotFoundByKeyException<Guid> ex)
{
throw new UserUpdateException($"Not found user by given id {user.Id}", ex);
}
catch (Exception ex)
{
throw new UserUpdateException($"Error when updating the user {user.Id}", ex);
}
}
public Task Remove(User user)
{
_validator.Validate(user);
throw new NotImplementedException();
}
@@ -84,6 +143,7 @@ public class UsersRepository : IUsersRepository
public Task<bool> Exists(User user)
{
_validator.Validate(user);
throw new NotImplementedException();
}
@@ -96,4 +156,11 @@ public class UsersRepository : IUsersRepository
{
throw new NotImplementedException();
}
}
}
public class AdditionUserException(string s, Exception ex)
: Exception(s, ex);
public class UserUpdateException(string s, Exception ex)
: Exception(s, ex);