mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Restructure project
This commit is contained in:
14
Repository/Repository.Core/Entities/UserAccount.cs
Normal file
14
Repository/Repository.Core/Entities/UserAccount.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace DataAccessLayer.Entities;
|
||||
|
||||
public class UserAccount
|
||||
{
|
||||
public Guid UserAccountId { get; set; }
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
public string LastName { get; set; } = string.Empty;
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime? UpdatedAt { get; set; }
|
||||
public DateTime DateOfBirth { get; set; }
|
||||
public byte[]? Timer { get; set; }
|
||||
}
|
||||
11
Repository/Repository.Core/Entities/UserCredential.cs
Normal file
11
Repository/Repository.Core/Entities/UserCredential.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace DataAccessLayer.Entities;
|
||||
|
||||
public class UserCredential
|
||||
{
|
||||
public Guid UserCredentialId { get; set; }
|
||||
public Guid UserAccountId { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime Expiry { get; set; }
|
||||
public string Hash { get; set; } = string.Empty;
|
||||
public byte[]? Timer { get; set; }
|
||||
}
|
||||
9
Repository/Repository.Core/Entities/UserVerification.cs
Normal file
9
Repository/Repository.Core/Entities/UserVerification.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace DataAccessLayer.Entities;
|
||||
|
||||
public class UserVerification
|
||||
{
|
||||
public Guid UserVerificationId { get; set; }
|
||||
public Guid UserAccountId { get; set; }
|
||||
public DateTime VerificationDateTime { get; set; }
|
||||
public byte[]? Timer { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using DataAccessLayer.Entities;
|
||||
|
||||
namespace DataAccessLayer.Repositories
|
||||
{
|
||||
public interface IUserAccountRepository
|
||||
{
|
||||
Task Add(UserAccount userAccount);
|
||||
Task<UserAccount?> GetById(Guid id);
|
||||
Task<IEnumerable<UserAccount>> GetAll(int? limit, int? offset);
|
||||
Task Update(UserAccount userAccount);
|
||||
Task Delete(Guid id);
|
||||
Task<UserAccount?> GetByUsername(string username);
|
||||
Task<UserAccount?> GetByEmail(string email);
|
||||
}
|
||||
}
|
||||
24
Repository/Repository.Core/Repositories/Repository.cs
Normal file
24
Repository/Repository.Core/Repositories/Repository.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using DataAccessLayer.Sql;
|
||||
using Microsoft.Data.SqlClient;
|
||||
|
||||
namespace DataAccessLayer.Repositories
|
||||
{
|
||||
public abstract class Repository<T>(ISqlConnectionFactory connectionFactory)
|
||||
where T : class
|
||||
{
|
||||
protected async Task<SqlConnection> CreateConnection()
|
||||
{
|
||||
var connection = connectionFactory.CreateConnection();
|
||||
await connection.OpenAsync();
|
||||
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);
|
||||
|
||||
protected abstract T MapToEntity(SqlDataReader reader);
|
||||
}
|
||||
}
|
||||
134
Repository/Repository.Core/Repositories/UserAccountRepository.cs
Normal file
134
Repository/Repository.Core/Repositories/UserAccountRepository.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using DataAccessLayer.Entities;
|
||||
using DataAccessLayer.Sql;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using System.Data;
|
||||
|
||||
namespace DataAccessLayer.Repositories
|
||||
{
|
||||
public class UserAccountRepository(ISqlConnectionFactory connectionFactory)
|
||||
: Repository<UserAccount>(connectionFactory), IUserAccountRepository
|
||||
{
|
||||
public override async Task Add(UserAccount userAccount)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_CreateUserAccount", connection);
|
||||
command.CommandType = CommandType.StoredProcedure;
|
||||
|
||||
command.Parameters.Add("@UserAccountId", SqlDbType.UniqueIdentifier).Value = userAccount.UserAccountId;
|
||||
command.Parameters.Add("@Username", SqlDbType.NVarChar, 100).Value = userAccount.Username;
|
||||
command.Parameters.Add("@FirstName", SqlDbType.NVarChar, 100).Value = userAccount.FirstName;
|
||||
command.Parameters.Add("@LastName", SqlDbType.NVarChar, 100).Value = userAccount.LastName;
|
||||
command.Parameters.Add("@Email", SqlDbType.NVarChar, 256).Value = userAccount.Email;
|
||||
command.Parameters.Add("@DateOfBirth", SqlDbType.Date).Value = userAccount.DateOfBirth;
|
||||
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public override async Task<UserAccount?> GetById(Guid id)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_GetUserAccountById", connection)
|
||||
{
|
||||
CommandType = CommandType.StoredProcedure
|
||||
};
|
||||
|
||||
command.Parameters.Add("@UserAccountId", SqlDbType.UniqueIdentifier).Value = id;
|
||||
|
||||
await using var reader = await command.ExecuteReaderAsync();
|
||||
return await reader.ReadAsync() ? MapToEntity(reader) : null;
|
||||
}
|
||||
|
||||
public override async Task<IEnumerable<UserAccount>> GetAll(int? limit, int? offset)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_GetAllUserAccounts", connection);
|
||||
command.CommandType = CommandType.StoredProcedure;
|
||||
|
||||
if (limit.HasValue)
|
||||
command.Parameters.Add("@Limit", SqlDbType.Int).Value = limit.Value;
|
||||
|
||||
if (offset.HasValue)
|
||||
command.Parameters.Add("@Offset", SqlDbType.Int).Value = offset.Value;
|
||||
|
||||
await using var reader = await command.ExecuteReaderAsync();
|
||||
var users = new List<UserAccount>();
|
||||
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
users.Add(MapToEntity(reader));
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
public override async Task Update(UserAccount userAccount)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_UpdateUserAccount", connection);
|
||||
command.CommandType = CommandType.StoredProcedure;
|
||||
|
||||
command.Parameters.Add("@UserAccountId", SqlDbType.UniqueIdentifier).Value = userAccount.UserAccountId;
|
||||
command.Parameters.Add("@Username", SqlDbType.NVarChar, 100).Value = userAccount.Username;
|
||||
command.Parameters.Add("@FirstName", SqlDbType.NVarChar, 100).Value = userAccount.FirstName;
|
||||
command.Parameters.Add("@LastName", SqlDbType.NVarChar, 100).Value = userAccount.LastName;
|
||||
command.Parameters.Add("@Email", SqlDbType.NVarChar, 256).Value = userAccount.Email;
|
||||
command.Parameters.Add("@DateOfBirth", SqlDbType.Date).Value = userAccount.DateOfBirth;
|
||||
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public override async Task Delete(Guid id)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_DeleteUserAccount", connection);
|
||||
command.CommandType = CommandType.StoredProcedure;
|
||||
|
||||
command.Parameters.Add("@UserAccountId", SqlDbType.UniqueIdentifier).Value = id;
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public async Task<UserAccount?> GetByUsername(string username)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_GetUserAccountByUsername", connection);
|
||||
command.CommandType = CommandType.StoredProcedure;
|
||||
|
||||
command.Parameters.Add("@Username", SqlDbType.NVarChar, 100).Value = username;
|
||||
|
||||
await using var reader = await command.ExecuteReaderAsync();
|
||||
return await reader.ReadAsync() ? MapToEntity(reader) : null;
|
||||
}
|
||||
|
||||
public async Task<UserAccount?> GetByEmail(string email)
|
||||
{
|
||||
await using var connection = await CreateConnection();
|
||||
await using var command = new SqlCommand("usp_GetUserAccountByEmail", connection);
|
||||
command.CommandType = CommandType.StoredProcedure;
|
||||
|
||||
command.Parameters.Add("@Email", SqlDbType.NVarChar, 256).Value = email;
|
||||
|
||||
await using var reader = await command.ExecuteReaderAsync();
|
||||
return await reader.ReadAsync() ? MapToEntity(reader) : null;
|
||||
}
|
||||
|
||||
protected override UserAccount MapToEntity(SqlDataReader reader)
|
||||
{
|
||||
return new UserAccount
|
||||
{
|
||||
UserAccountId = reader.GetGuid(reader.GetOrdinal("UserAccountId")),
|
||||
Username = reader.GetString(reader.GetOrdinal("Username")),
|
||||
FirstName = reader.GetString(reader.GetOrdinal("FirstName")),
|
||||
LastName = reader.GetString(reader.GetOrdinal("LastName")),
|
||||
Email = reader.GetString(reader.GetOrdinal("Email")),
|
||||
CreatedAt = reader.GetDateTime(reader.GetOrdinal("CreatedAt")),
|
||||
UpdatedAt = reader.IsDBNull(reader.GetOrdinal("UpdatedAt"))
|
||||
? null
|
||||
: reader.GetDateTime(reader.GetOrdinal("UpdatedAt")),
|
||||
DateOfBirth = reader.GetDateTime(reader.GetOrdinal("DateOfBirth")),
|
||||
Timer = reader.IsDBNull(reader.GetOrdinal("Timer"))
|
||||
? null
|
||||
: (byte[])reader["Timer"]
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Repository/Repository.Core/Repository.Core.csproj
Normal file
17
Repository/Repository.Core/Repository.Core.csproj
Normal file
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>DataAccessLayer</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.1.3" />
|
||||
<PackageReference
|
||||
Include="Microsoft.SqlServer.Types"
|
||||
Version="160.1000.6"
|
||||
/>
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,20 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
|
||||
namespace DataAccessLayer.Sql
|
||||
{
|
||||
public class DefaultSqlConnectionFactory(IConfiguration configuration) : ISqlConnectionFactory
|
||||
{
|
||||
private readonly string _connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")
|
||||
?? configuration.GetConnectionString("Default")
|
||||
?? throw new InvalidOperationException(
|
||||
"Database connection string not configured. Set DB_CONNECTION_STRING env var or ConnectionStrings:Default."
|
||||
);
|
||||
|
||||
public SqlConnection CreateConnection()
|
||||
{
|
||||
return new SqlConnection(_connectionString);
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Repository/Repository.Core/Sql/ISqlConnectionFactory.cs
Normal file
9
Repository/Repository.Core/Sql/ISqlConnectionFactory.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
|
||||
namespace DataAccessLayer.Sql
|
||||
{
|
||||
public interface ISqlConnectionFactory
|
||||
{
|
||||
SqlConnection CreateConnection();
|
||||
}
|
||||
}
|
||||
24
Repository/Repository.Tests/Repository.Tests.csproj
Normal file
24
Repository/Repository.Tests/Repository.Tests.csproj
Normal file
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
<RootNamespace>DALTests</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Repository.Core\Repository.Core.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
280
Repository/Repository.Tests/UserAccountRepositoryTests.cs
Normal file
280
Repository/Repository.Tests/UserAccountRepositoryTests.cs
Normal file
@@ -0,0 +1,280 @@
|
||||
using DataAccessLayer;
|
||||
using DataAccessLayer.Entities;
|
||||
using DataAccessLayer.Repositories;
|
||||
|
||||
namespace DALTests
|
||||
{
|
||||
public class UserAccountRepositoryTests
|
||||
{
|
||||
private readonly IUserAccountRepository _repository = new InMemoryUserAccountRepository();
|
||||
|
||||
[Fact]
|
||||
public async Task Add_ShouldInsertUserAccount()
|
||||
{
|
||||
// Arrange
|
||||
var userAccount = new UserAccount
|
||||
{
|
||||
UserAccountId = Guid.NewGuid(),
|
||||
Username = "testuser",
|
||||
FirstName = "Test",
|
||||
LastName = "User",
|
||||
Email = "testuser@example.com",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
DateOfBirth = new DateTime(1990, 1, 1),
|
||||
};
|
||||
|
||||
// Act
|
||||
await _repository.Add(userAccount);
|
||||
var retrievedUser = await _repository.GetById(userAccount.UserAccountId);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(retrievedUser);
|
||||
Assert.Equal(userAccount.Username, retrievedUser.Username);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetById_ShouldReturnUserAccount()
|
||||
{
|
||||
// Arrange
|
||||
var userId = Guid.NewGuid();
|
||||
var userAccount = new UserAccount
|
||||
{
|
||||
UserAccountId = userId,
|
||||
Username = "existinguser",
|
||||
FirstName = "Existing",
|
||||
LastName = "User",
|
||||
Email = "existinguser@example.com",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
DateOfBirth = new DateTime(1985, 5, 15),
|
||||
};
|
||||
await _repository.Add(userAccount);
|
||||
|
||||
// Act
|
||||
var retrievedUser = await _repository.GetById(userId);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(retrievedUser);
|
||||
Assert.Equal(userId, retrievedUser.UserAccountId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Update_ShouldModifyUserAccount()
|
||||
{
|
||||
// Arrange
|
||||
var userAccount = new UserAccount
|
||||
{
|
||||
UserAccountId = Guid.NewGuid(),
|
||||
Username = "updatableuser",
|
||||
FirstName = "Updatable",
|
||||
LastName = "User",
|
||||
Email = "updatableuser@example.com",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
DateOfBirth = new DateTime(1992, 3, 10),
|
||||
};
|
||||
await _repository.Add(userAccount);
|
||||
|
||||
// Act
|
||||
userAccount.FirstName = "Updated";
|
||||
await _repository.Update(userAccount);
|
||||
var updatedUser = await _repository.GetById(userAccount.UserAccountId);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(updatedUser);
|
||||
Assert.Equal("Updated", updatedUser.FirstName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Delete_ShouldRemoveUserAccount()
|
||||
{
|
||||
// Arrange
|
||||
var userAccount = new UserAccount
|
||||
{
|
||||
UserAccountId = Guid.NewGuid(),
|
||||
Username = "deletableuser",
|
||||
FirstName = "Deletable",
|
||||
LastName = "User",
|
||||
Email = "deletableuser@example.com",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
DateOfBirth = new DateTime(1995, 7, 20),
|
||||
};
|
||||
await _repository.Add(userAccount);
|
||||
|
||||
// Act
|
||||
await _repository.Delete(userAccount.UserAccountId);
|
||||
var deletedUser = await _repository.GetById(userAccount.UserAccountId);
|
||||
|
||||
// Assert
|
||||
Assert.Null(deletedUser);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAll_ShouldReturnAllUserAccounts()
|
||||
{
|
||||
// Arrange
|
||||
var user1 = new UserAccount
|
||||
{
|
||||
UserAccountId = Guid.NewGuid(),
|
||||
Username = "user1",
|
||||
FirstName = "User",
|
||||
LastName = "One",
|
||||
Email = "user1@example.com",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
DateOfBirth = new DateTime(1990, 1, 1),
|
||||
};
|
||||
var user2 = new UserAccount
|
||||
{
|
||||
UserAccountId = Guid.NewGuid(),
|
||||
Username = "user2",
|
||||
FirstName = "User",
|
||||
LastName = "Two",
|
||||
Email = "user2@example.com",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
DateOfBirth = new DateTime(1992, 2, 2),
|
||||
};
|
||||
await _repository.Add(user1);
|
||||
await _repository.Add(user2);
|
||||
|
||||
// Act
|
||||
var allUsers = await _repository.GetAll(null, null);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(allUsers);
|
||||
Assert.True(allUsers.Count() >= 2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAll_WithPagination_ShouldRespectLimit()
|
||||
{
|
||||
// Arrange
|
||||
var users = new List<UserAccount>
|
||||
{
|
||||
new UserAccount
|
||||
{
|
||||
UserAccountId = Guid.NewGuid(),
|
||||
Username = $"pageuser_{Guid.NewGuid():N}",
|
||||
FirstName = "Page",
|
||||
LastName = "User",
|
||||
Email = $"pageuser_{Guid.NewGuid():N}@example.com",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
DateOfBirth = new DateTime(1991, 4, 4),
|
||||
},
|
||||
new UserAccount
|
||||
{
|
||||
UserAccountId = Guid.NewGuid(),
|
||||
Username = $"pageuser_{Guid.NewGuid():N}",
|
||||
FirstName = "Page",
|
||||
LastName = "User",
|
||||
Email = $"pageuser_{Guid.NewGuid():N}@example.com",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
DateOfBirth = new DateTime(1992, 5, 5),
|
||||
},
|
||||
new UserAccount
|
||||
{
|
||||
UserAccountId = Guid.NewGuid(),
|
||||
Username = $"pageuser_{Guid.NewGuid():N}",
|
||||
FirstName = "Page",
|
||||
LastName = "User",
|
||||
Email = $"pageuser_{Guid.NewGuid():N}@example.com",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
DateOfBirth = new DateTime(1993, 6, 6),
|
||||
},
|
||||
};
|
||||
|
||||
foreach (var user in users)
|
||||
{
|
||||
await _repository.Add(user);
|
||||
}
|
||||
|
||||
// Act
|
||||
var page = (await _repository.GetAll(2, 0)).ToList();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, page.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAll_WithPagination_ShouldValidateArguments()
|
||||
{
|
||||
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () =>
|
||||
(await _repository.GetAll(0, 0)).ToList()
|
||||
);
|
||||
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () =>
|
||||
(await _repository.GetAll(1, -1)).ToList()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
internal class InMemoryUserAccountRepository : IUserAccountRepository
|
||||
{
|
||||
private readonly Dictionary<Guid, UserAccount> _store = new();
|
||||
|
||||
public Task Add(UserAccount userAccount)
|
||||
{
|
||||
if (userAccount.UserAccountId == Guid.Empty)
|
||||
{
|
||||
userAccount.UserAccountId = Guid.NewGuid();
|
||||
}
|
||||
_store[userAccount.UserAccountId] = Clone(userAccount);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<UserAccount?> GetById(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)
|
||||
{
|
||||
if (limit.HasValue && limit.Value <= 0) throw new ArgumentOutOfRangeException(nameof(limit));
|
||||
if (offset.HasValue && offset.Value < 0) throw new ArgumentOutOfRangeException(nameof(offset));
|
||||
|
||||
var query = _store.Values
|
||||
.OrderBy(u => u.Username)
|
||||
.Select(Clone);
|
||||
|
||||
if (offset.HasValue) query = query.Skip(offset.Value);
|
||||
if (limit.HasValue) query = query.Take(limit.Value);
|
||||
|
||||
return Task.FromResult<IEnumerable<UserAccount>>(query.ToList());
|
||||
}
|
||||
|
||||
public Task Update(UserAccount userAccount)
|
||||
{
|
||||
if (!_store.ContainsKey(userAccount.UserAccountId)) return Task.CompletedTask;
|
||||
_store[userAccount.UserAccountId] = Clone(userAccount);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Delete(Guid id)
|
||||
{
|
||||
_store.Remove(id);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<UserAccount?> GetByUsername(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)
|
||||
{
|
||||
var user = _store.Values.FirstOrDefault(u => u.Email == email);
|
||||
return Task.FromResult(user is null ? null : Clone(user));
|
||||
}
|
||||
|
||||
private static UserAccount Clone(UserAccount u) => new()
|
||||
{
|
||||
UserAccountId = u.UserAccountId,
|
||||
Username = u.Username,
|
||||
FirstName = u.FirstName,
|
||||
LastName = u.LastName,
|
||||
Email = u.Email,
|
||||
CreatedAt = u.CreatedAt,
|
||||
UpdatedAt = u.UpdatedAt,
|
||||
DateOfBirth = u.DateOfBirth,
|
||||
Timer = u.Timer is null ? null : (byte[])u.Timer.Clone(),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user