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
@@ -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){}
}
+29 -15
View File
@@ -1,6 +1,8 @@
using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models;
using Govor.Core.Repositories;
using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore;
namespace Govor.Data.Repositories;
@@ -23,27 +25,39 @@ public class UsersRepository : IUsersRepository
.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 async Task<List<User>> FindUsersByName(string username)
{
return await _context.Users
.AsNoTracking()
.Where(x => x.Username == username)
.ToListOrThrowIfEmpty(new NotFoundByKeyException<string>(username, "Users with given username not found"));
}
public async Task<List<User>> FindByRangeUsername(IEnumerable<string> 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<User> FindByUsername(string username)
{
throw new NotImplementedException();
}
public Task<IEnumerable<User>> FindByRangeUsername(IEnumerable<string> usernames)
{
throw new NotImplementedException();
}
public Task<IEnumerable<User>> FindUsersByCreatedDate(DateOnly createdDate)
public Task<List<User>> FindUsersByCreatedDate(DateOnly createdDate)
{
throw new NotImplementedException();
}