using System.Diagnostics.CodeAnalysis;
using DotBased.ASP.Auth.Domains.Auth;
using DotBased.ASP.Auth.Domains.Identity;
using DotBased.Extensions;
namespace DotBased.ASP.Auth;
///
/// In memory data provider, for testing only!
///
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")]
public class MemoryAuthDataRepository : IAuthDataRepository
{
public async Task CreateUserAsync(UserModel user)
{
if (MemoryData.users.Any(x => x.Id == user.Id || x.Email == user.Email))
return Result.Failed("User already exists.");
MemoryData.users.Add(user);
return Result.Ok();
}
public async Task UpdateUserAsync(UserModel user)
{
if (MemoryData.users.All(x => x.Id != user.Id))
return Result.Failed("User does not exist!");
return Result.Ok();
}
public Task DeleteUserAsync(UserModel user)
{
throw new NotImplementedException();
}
public async Task> GetUserAsync(string id, string email, string username)
{
UserModel? userModel = null;
if (!id.IsNullOrWhiteSpace())
userModel = MemoryData.users.FirstOrDefault(u => u.Id.Equals(id, StringComparison.OrdinalIgnoreCase));
if (!email.IsNullOrWhiteSpace())
userModel = MemoryData.users.FirstOrDefault(u => u.Email.Equals(email, StringComparison.OrdinalIgnoreCase));
if (!username.IsNullOrWhiteSpace())
userModel = MemoryData.users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.OrdinalIgnoreCase));
return userModel != null ? Result.Ok(userModel) : Result.Failed("No user found!");
}
public Task> GetUsersAsync(int start = 0, int amount = 30, string search = "")
{
throw new NotImplementedException();
}
public Task CreateGroupAsync(GroupModel group)
{
throw new NotImplementedException();
}
public Task UpdateGroupAsync(GroupModel group)
{
throw new NotImplementedException();
}
public Task DeleteGroupAsync(GroupModel group)
{
throw new NotImplementedException();
}
public Task> GetGroupAsync(string id)
{
throw new NotImplementedException();
}
public Task> GetGroupsAsync(int start = 0, int amount = 30, string search = "")
{
throw new NotImplementedException();
}
public async Task CreateAuthenticationStateAsync(AuthenticationStateModel authenticationState)
{
if (MemoryData.AuthenticationStates.Contains(authenticationState)) return Result.Failed("Item already exists!");
MemoryData.AuthenticationStates.Add(authenticationState);
return Result.Ok();
}
public Task UpdateAuthenticationStateAsync(AuthenticationStateModel authenticationState)
{
throw new NotImplementedException();
}
public async Task DeleteAuthenticationStateAsync(AuthenticationStateModel authenticationState)
{
MemoryData.AuthenticationStates.Remove(authenticationState);
return Result.Ok();
}
public async Task> GetAuthenticationStateAsync(string id)
{
var item = MemoryData.AuthenticationStates.FirstOrDefault(x => x.Id == id);
if (item == null) return Result.Failed("Could not get the session state!");
return Result.Ok(item);
}
}
internal static class MemoryData
{
public static readonly List users = [];
public static readonly List Groups = [];
public static readonly List AuthenticationStates = [];
}