mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Refactor repository methods to async and update credential logic
This commit is contained in:
@@ -13,11 +13,11 @@ namespace DataAccessLayer.Repositories
|
||||
return connection;
|
||||
}
|
||||
|
||||
public abstract Task Add(T entity);
|
||||
public abstract Task<IEnumerable<T>> GetAll(int? limit, int? offset);
|
||||
public abstract Task<T?> GetById(Guid id);
|
||||
public abstract Task Update(T entity);
|
||||
public abstract Task Delete(Guid id);
|
||||
public abstract Task AddAsync(T entity);
|
||||
public abstract Task<IEnumerable<T>> GetAllAsync(int? limit, int? offset);
|
||||
public abstract Task<T?> GetByIdAsync(Guid id);
|
||||
public abstract Task UpdateAsync(T entity);
|
||||
public abstract Task DeleteAsync(Guid id);
|
||||
|
||||
protected abstract T MapToEntity(SqlDataReader reader);
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@ namespace DataAccessLayer.Repositories.UserAccount
|
||||
{
|
||||
public interface IUserAccountRepository
|
||||
{
|
||||
Task Add(Entities.UserAccount userAccount);
|
||||
Task<Entities.UserAccount?> GetById(Guid id);
|
||||
Task<IEnumerable<Entities.UserAccount>> GetAll(int? limit, int? offset);
|
||||
Task Update(Entities.UserAccount userAccount);
|
||||
Task Delete(Guid id);
|
||||
Task<Entities.UserAccount?> GetByUsername(string username);
|
||||
Task<Entities.UserAccount?> GetByEmail(string email);
|
||||
Task AddAsync(Entities.UserAccount userAccount);
|
||||
Task<Entities.UserAccount?> GetByIdAsync(Guid id);
|
||||
Task<IEnumerable<Entities.UserAccount>> GetAllAsync(int? limit, int? offset);
|
||||
Task UpdateAsync(Entities.UserAccount userAccount);
|
||||
Task DeleteAsync(Guid id);
|
||||
Task<Entities.UserAccount?> GetByUsernameAsync(string username);
|
||||
Task<Entities.UserAccount?> GetByEmailAsync(string email);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace DataAccessLayer.Repositories.UserAccount
|
||||
public class UserAccountRepository(ISqlConnectionFactory connectionFactory)
|
||||
: Repository<Entities.UserAccount>(connectionFactory), IUserAccountRepository
|
||||
{
|
||||
public override async Task Add(Entities.UserAccount userAccount)
|
||||
public override async Task AddAsync(Entities.UserAccount userAccount)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_CreateUserAccount", connection);
|
||||
@@ -23,7 +23,7 @@ namespace DataAccessLayer.Repositories.UserAccount
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public override async Task<Entities.UserAccount?> GetById(Guid id)
|
||||
public override async Task<Entities.UserAccount?> GetByIdAsync(Guid id)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_GetUserAccountById", connection)
|
||||
@@ -37,7 +37,7 @@ namespace DataAccessLayer.Repositories.UserAccount
|
||||
return await reader.ReadAsync() ? MapToEntity(reader) : null;
|
||||
}
|
||||
|
||||
public override async Task<IEnumerable<Entities.UserAccount>> GetAll(int? limit, int? offset)
|
||||
public override async Task<IEnumerable<Entities.UserAccount>> GetAllAsync(int? limit, int? offset)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_GetAllUserAccounts", connection);
|
||||
@@ -60,7 +60,7 @@ namespace DataAccessLayer.Repositories.UserAccount
|
||||
return users;
|
||||
}
|
||||
|
||||
public override async Task Update(Entities.UserAccount userAccount)
|
||||
public override async Task UpdateAsync(Entities.UserAccount userAccount)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_UpdateUserAccount", connection);
|
||||
@@ -76,7 +76,7 @@ namespace DataAccessLayer.Repositories.UserAccount
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public override async Task Delete(Guid id)
|
||||
public override async Task DeleteAsync(Guid id)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_DeleteUserAccount", connection);
|
||||
@@ -86,7 +86,7 @@ namespace DataAccessLayer.Repositories.UserAccount
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public async Task<Entities.UserAccount?> GetByUsername(string username)
|
||||
public async Task<Entities.UserAccount?> GetByUsernameAsync(string username)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_GetUserAccountByUsername", connection);
|
||||
@@ -98,7 +98,7 @@ namespace DataAccessLayer.Repositories.UserAccount
|
||||
return await reader.ReadAsync() ? MapToEntity(reader) : null;
|
||||
}
|
||||
|
||||
public async Task<Entities.UserAccount?> GetByEmail(string email)
|
||||
public async Task<Entities.UserAccount?> GetByEmailAsync(string email)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_GetUserAccountByEmail", connection);
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
namespace DataAccessLayer.Repositories.UserCredential;
|
||||
using DataAccessLayer.Entities;
|
||||
|
||||
public interface IUserCredentialRepository
|
||||
{
|
||||
Task Add(Entities.UserCredential credential);
|
||||
Task<Entities.UserCredential?> GetById(Guid userCredentialId);
|
||||
Task<Entities.UserCredential?> GetByUserAccountId(Guid userAccountId);
|
||||
Task<IEnumerable<Entities.UserCredential>> GetAll(int? limit, int? offset);
|
||||
Task Update(Entities.UserCredential credential);
|
||||
Task Delete(Guid userCredentialId);
|
||||
}
|
||||
Task RotateCredentialAsync(Guid userAccountId, UserCredential credential);
|
||||
Task<UserCredential?> GetActiveCredentialByUserAccountIdAsync(Guid userAccountId);
|
||||
}
|
||||
|
||||
@@ -25,8 +25,8 @@ namespace DALTests
|
||||
};
|
||||
|
||||
// Act
|
||||
await _repository.Add(userAccount);
|
||||
var retrievedUser = await _repository.GetById(userAccount.UserAccountId);
|
||||
await _repository.AddAsync(userAccount);
|
||||
var retrievedUser = await _repository.GetByIdAsync(userAccount.UserAccountId);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(retrievedUser);
|
||||
@@ -48,10 +48,10 @@ namespace DALTests
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
DateOfBirth = new DateTime(1985, 5, 15),
|
||||
};
|
||||
await _repository.Add(userAccount);
|
||||
await _repository.AddAsync(userAccount);
|
||||
|
||||
// Act
|
||||
var retrievedUser = await _repository.GetById(userId);
|
||||
var retrievedUser = await _repository.GetByIdAsync(userId);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(retrievedUser);
|
||||
@@ -72,12 +72,12 @@ namespace DALTests
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
DateOfBirth = new DateTime(1992, 3, 10),
|
||||
};
|
||||
await _repository.Add(userAccount);
|
||||
await _repository.AddAsync(userAccount);
|
||||
|
||||
// Act
|
||||
userAccount.FirstName = "Updated";
|
||||
await _repository.Update(userAccount);
|
||||
var updatedUser = await _repository.GetById(userAccount.UserAccountId);
|
||||
await _repository.UpdateAsync(userAccount);
|
||||
var updatedUser = await _repository.GetByIdAsync(userAccount.UserAccountId);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(updatedUser);
|
||||
@@ -98,11 +98,11 @@ namespace DALTests
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
DateOfBirth = new DateTime(1995, 7, 20),
|
||||
};
|
||||
await _repository.Add(userAccount);
|
||||
await _repository.AddAsync(userAccount);
|
||||
|
||||
// Act
|
||||
await _repository.Delete(userAccount.UserAccountId);
|
||||
var deletedUser = await _repository.GetById(userAccount.UserAccountId);
|
||||
await _repository.DeleteAsync(userAccount.UserAccountId);
|
||||
var deletedUser = await _repository.GetByIdAsync(userAccount.UserAccountId);
|
||||
|
||||
// Assert
|
||||
Assert.Null(deletedUser);
|
||||
@@ -132,11 +132,11 @@ namespace DALTests
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
DateOfBirth = new DateTime(1992, 2, 2),
|
||||
};
|
||||
await _repository.Add(user1);
|
||||
await _repository.Add(user2);
|
||||
await _repository.AddAsync(user1);
|
||||
await _repository.AddAsync(user2);
|
||||
|
||||
// Act
|
||||
var allUsers = await _repository.GetAll(null, null);
|
||||
var allUsers = await _repository.GetAllAsync(null, null);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(allUsers);
|
||||
@@ -183,11 +183,11 @@ namespace DALTests
|
||||
|
||||
foreach (var user in users)
|
||||
{
|
||||
await _repository.Add(user);
|
||||
await _repository.AddAsync(user);
|
||||
}
|
||||
|
||||
// Act
|
||||
var page = (await _repository.GetAll(2, 0)).ToList();
|
||||
var page = (await _repository.GetAllAsync(2, 0)).ToList();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, page.Count);
|
||||
@@ -197,10 +197,10 @@ namespace DALTests
|
||||
public async Task GetAll_WithPagination_ShouldValidateArguments()
|
||||
{
|
||||
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () =>
|
||||
(await _repository.GetAll(0, 0)).ToList()
|
||||
(await _repository.GetAllAsync(0, 0)).ToList()
|
||||
);
|
||||
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () =>
|
||||
(await _repository.GetAll(1, -1)).ToList()
|
||||
(await _repository.GetAllAsync(1, -1)).ToList()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -209,7 +209,7 @@ namespace DALTests
|
||||
{
|
||||
private readonly Dictionary<Guid, UserAccount> _store = new();
|
||||
|
||||
public Task Add(UserAccount userAccount)
|
||||
public Task AddAsync(UserAccount userAccount)
|
||||
{
|
||||
if (userAccount.UserAccountId == Guid.Empty)
|
||||
{
|
||||
@@ -219,13 +219,13 @@ namespace DALTests
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<UserAccount?> GetById(Guid id)
|
||||
public Task<UserAccount?> GetByIdAsync(Guid id)
|
||||
{
|
||||
_store.TryGetValue(id, out var user);
|
||||
return Task.FromResult(user is null ? null : Clone(user));
|
||||
}
|
||||
|
||||
public Task<IEnumerable<UserAccount>> GetAll(int? limit, int? offset)
|
||||
public Task<IEnumerable<UserAccount>> GetAllAsync(int? limit, int? offset)
|
||||
{
|
||||
if (limit.HasValue && limit.Value <= 0) throw new ArgumentOutOfRangeException(nameof(limit));
|
||||
if (offset.HasValue && offset.Value < 0) throw new ArgumentOutOfRangeException(nameof(offset));
|
||||
@@ -240,26 +240,26 @@ namespace DALTests
|
||||
return Task.FromResult<IEnumerable<UserAccount>>(query.ToList());
|
||||
}
|
||||
|
||||
public Task Update(UserAccount userAccount)
|
||||
public Task UpdateAsync(UserAccount userAccount)
|
||||
{
|
||||
if (!_store.ContainsKey(userAccount.UserAccountId)) return Task.CompletedTask;
|
||||
_store[userAccount.UserAccountId] = Clone(userAccount);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Delete(Guid id)
|
||||
public Task DeleteAsync(Guid id)
|
||||
{
|
||||
_store.Remove(id);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<UserAccount?> GetByUsername(string username)
|
||||
public Task<UserAccount?> GetByUsernameAsync(string username)
|
||||
{
|
||||
var user = _store.Values.FirstOrDefault(u => u.Username == username);
|
||||
return Task.FromResult(user is null ? null : Clone(user));
|
||||
}
|
||||
|
||||
public Task<UserAccount?> GetByEmail(string email)
|
||||
public Task<UserAccount?> GetByEmailAsync(string email)
|
||||
{
|
||||
var user = _store.Values.FirstOrDefault(u => u.Email == email);
|
||||
return Task.FromResult(user is null ? null : Clone(user));
|
||||
|
||||
Reference in New Issue
Block a user