fleshout backend
This commit is contained in:
58
backend/Data/InMemory/InMemoryUserRepository.cs
Normal file
58
backend/Data/InMemory/InMemoryUserRepository.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Backend.Data.InMemory;
|
||||
|
||||
/// <summary>
|
||||
/// Default implementation so the skeleton runs with no database. State is per-process
|
||||
/// and lost on restart.
|
||||
/// </summary>
|
||||
public sealed class InMemoryUserRepository : IUserRepository
|
||||
{
|
||||
private readonly ConcurrentDictionary<Guid, User> _users = new();
|
||||
|
||||
public Task<IReadOnlyList<User>> GetAllAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
IReadOnlyList<User> users = _users.Values
|
||||
.OrderBy(user => user.CreatedAt)
|
||||
.ToList();
|
||||
|
||||
return Task.FromResult(users);
|
||||
}
|
||||
|
||||
public Task<User?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_users.TryGetValue(id, out var user);
|
||||
|
||||
return Task.FromResult(user);
|
||||
}
|
||||
|
||||
public Task<User?> GetByEmailAsync(string email, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var user = _users.Values
|
||||
.FirstOrDefault(candidate => string.Equals(candidate.Email, email, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
return Task.FromResult(user);
|
||||
}
|
||||
|
||||
public Task<User> AddAsync(User user, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_users[user.Id] = user;
|
||||
|
||||
return Task.FromResult(user);
|
||||
}
|
||||
|
||||
public Task<User?> UpdateAsync(User user, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (!_users.ContainsKey(user.Id))
|
||||
{
|
||||
return Task.FromResult<User?>(null);
|
||||
}
|
||||
|
||||
_users[user.Id] = user;
|
||||
|
||||
return Task.FromResult<User?>(user);
|
||||
}
|
||||
|
||||
public Task<bool> DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
=> Task.FromResult(_users.TryRemove(id, out _));
|
||||
}
|
||||
Reference in New Issue
Block a user