DotBased/DotBased.ASP.Auth/MemoryAuthDataRepository.cs

107 lines
3.7 KiB
C#
Raw Permalink Normal View History

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