Working on the UserRepository functionality

+ Tests
+ QueryableExtensions. It is an add-on that allows you to implement exceptions if the returned List would be empty.
This commit is contained in:
Artemy
2025-06-17 13:11:56 +07:00
parent 568b09ba81
commit 694750e25d
7 changed files with 147 additions and 21 deletions
@@ -3,6 +3,7 @@ using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Data; using Govor.Data;
using Govor.Data.Repositories; using Govor.Data.Repositories;
using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace Govor.API.Tests.IntegrationTests.EF.Repositories; namespace Govor.API.Tests.IntegrationTests.EF.Repositories;
@@ -54,4 +55,74 @@ public class UsersRepositoryTests
Assert.That(result.Select(u => u.Id), Is.EquivalentTo(users.Select(u => u.Id))); Assert.That(result.Select(u => u.Id), Is.EquivalentTo(users.Select(u => u.Id)));
Assert.That(result.Select(u => u.Username), Is.EquivalentTo(users.Select(u => u.Username))); Assert.That(result.Select(u => u.Username), Is.EquivalentTo(users.Select(u => u.Username)));
} }
[Test]
public async Task Given_ValidUserId_When_FindById_Then_Returns_User()
{
// Arrange
var user = _fixture.Create<User>();
await using var context = new GovorDbContext(_options);
var userRepository = new UsersRepository(context, _userValidator);
context.Users.Add(user);
await context.SaveChangesAsync();
// Act
var result = await userRepository.FindById(user.Id);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Username, Is.EqualTo(user.Username));
Assert.That(result.Id, Is.EqualTo(user.Id));
}
[Test]
public async Task Given_InvalidUserId_When_FindById_Should_Throw_NotFoundException()
{
// Arrange
var id = Guid.NewGuid();
await using var context = new GovorDbContext(_options);
var userRepository = new UsersRepository(context, _userValidator);
// Act & Assert
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await userRepository.FindById(id));
}
[Test]
public async Task Given_RangeValidUserId_When_FindByRangeId_Then_Returns_Users()
{
// Arrange
var random = new Random();
var users = _fixture.CreateMany<User>(random.Next(2, 10)).ToList();
await using var context = new GovorDbContext(_options);
var userRepository = new UsersRepository(context, _userValidator);
context.Users.AddRange(users);
await context.SaveChangesAsync();
// Act
var result = await userRepository.FindByRangeId(users.Select(u => u.Id));
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Count, Is.EqualTo(users.Count));
Assert.That(result.Select(r => r.Id), Is.EquivalentTo(users.Select(u => u.Id)));
Assert.That(result.Select(u => u.Username), Is.EquivalentTo(users.Select(u => u.Username)));
}
[Test]
public async Task Given_InvalidRangeId_When_FindByRangeId_Should_Throw_NotFoundException()
{
// Arrange
var random = new Random();
var ids = _fixture.CreateMany<Guid>(random.Next(2, 10)).ToList();
await using var context = new GovorDbContext(_options);
var userRepository = new UsersRepository(context, _userValidator);
// Act & Assert
Assert.ThrowsAsync<NotFoundByKeyException<IEnumerable<Guid>>>(async () => await userRepository.FindByRangeId(ids));
}
} }
+4
View File
@@ -10,4 +10,8 @@
<Folder Include="DTOs\" /> <Folder Include="DTOs\" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.6" />
</ItemGroup>
</Project> </Project>
@@ -0,0 +1,13 @@
using Microsoft.EntityFrameworkCore;
namespace Govor.Core.Infrastructure.Extensions;
public static class QueryableExtensions
{
public static async Task<List<T>> ToListOrThrowIfEmpty<T>(this IQueryable<T> query, Exception ex)
{
var list = await query.ToListAsync();
if (list.Count == 0) throw ex;
return list;
}
}
+4 -5
View File
@@ -6,9 +6,8 @@ public interface IUsersReader
{ {
public Task<IEnumerable<User>> GetAll(); public Task<IEnumerable<User>> GetAll();
public Task<User> FindById(Guid id); public Task<User> FindById(Guid id);
public Task<IEnumerable<User>> FindByRangeId(IEnumerable<Guid> ids); public Task<List<User>> FindByRangeId(IEnumerable<Guid> ids);
public Task<User> FindByUsername(string username); public Task<List<User>> FindUsersByName(string username);
public Task<IEnumerable<User>> FindByRangeUsername(IEnumerable<string> usernames); public Task<List<User>> FindByRangeUsername(IEnumerable<string> usernames);
public Task<List<User>> FindUsersByCreatedDate(DateOnly createdDate);
public Task<IEnumerable<User>> FindUsersByCreatedDate(DateOnly createdDate);
} }
@@ -0,0 +1,14 @@
namespace Govor.Data.Repositories.Exceptions;
public class NotFoundByKeyException<T> : NotFoundException
{
public NotFoundByKeyException(T key)
: base($"Not found object by {key}"){}
public NotFoundByKeyException(T id, string message)
: base($"Not found object by {id}. Message: " + message) {}
public NotFoundByKeyException(string message, Exception innerException) : base(message, innerException)
{
}
}
@@ -0,0 +1,11 @@
using Govor.Core;
namespace Govor.Data.Repositories.Exceptions;
public class NotFoundException : GovorCoreException
{
public NotFoundException(string message)
: base(message) {}
public NotFoundException(string message, Exception innerException)
: base(message, innerException){}
}
+23 -9
View File
@@ -1,6 +1,8 @@
using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators; using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Repositories; using Govor.Core.Repositories;
using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace Govor.Data.Repositories; namespace Govor.Data.Repositories;
@@ -23,27 +25,39 @@ public class UsersRepository : IUsersRepository
.ToListAsync(); .ToListAsync();
} }
public Task<User> FindById(Guid id) public async Task<User> FindById(Guid id)
{ {
throw new NotImplementedException(); return await _context.Users
.AsNoTracking()
.FirstOrDefaultAsync(x => x.Id == id)
?? throw new NotFoundByKeyException<Guid>(id, "User with given id does not exist");
} }
public Task<IEnumerable<User>> FindByRangeId(IEnumerable<Guid> ids) public async Task<List<User>> FindByRangeId(IEnumerable<Guid> ids)
{ {
throw new NotImplementedException(); return await _context.Users
.AsNoTracking()
.Where(x => ids.Contains(x.Id))
.ToListOrThrowIfEmpty(new NotFoundByKeyException<IEnumerable<Guid>>(ids,"Users with given ids not found"));
} }
public Task<User> FindByUsername(string username) public async Task<List<User>> FindUsersByName(string username)
{ {
throw new NotImplementedException(); return await _context.Users
.AsNoTracking()
.Where(x => x.Username == username)
.ToListOrThrowIfEmpty(new NotFoundByKeyException<string>(username, "Users with given username not found"));
} }
public Task<IEnumerable<User>> FindByRangeUsername(IEnumerable<string> usernames) public async Task<List<User>> FindByRangeUsername(IEnumerable<string> usernames)
{ {
throw new NotImplementedException(); 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<IEnumerable<User>> FindUsersByCreatedDate(DateOnly createdDate) public Task<List<User>> FindUsersByCreatedDate(DateOnly createdDate)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }