[CHANGE] Reworking Result monads

This commit is contained in:
max
2025-04-07 14:59:37 +02:00
parent bb010b0cea
commit 0a5950cfa2
24 changed files with 390 additions and 322 deletions

View File

@@ -10,28 +10,28 @@ namespace DotBased.ASP.Auth;
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")]
public class MemoryAuthDataRepository : IAuthDataRepository
{
public async Task<Result> CreateUserAsync(UserModel user)
public async Task<ResultOld> CreateUserAsync(UserModel user)
{
if (MemoryData.users.Any(x => x.Id == user.Id || x.Email == user.Email))
return Result.Failed("User already exists.");
return ResultOld.Failed("User already exists.");
MemoryData.users.Add(user);
return Result.Ok();
return ResultOld.Ok();
}
public async Task<Result> UpdateUserAsync(UserModel user)
public async Task<ResultOld> UpdateUserAsync(UserModel user)
{
if (MemoryData.users.All(x => x.Id != user.Id))
return Result.Failed("User does not exist!");
return ResultOld.Failed("User does not exist!");
return Result.Ok();
return ResultOld.Ok();
}
public Task<Result> DeleteUserAsync(UserModel user)
public Task<ResultOld> DeleteUserAsync(UserModel user)
{
throw new NotImplementedException();
}
public async Task<Result<UserModel>> GetUserAsync(string id, string email, string username)
public async Task<ResultOld<UserModel>> GetUserAsync(string id, string email, string username)
{
UserModel? userModel = null;
if (!id.IsNullOrEmpty())
@@ -40,62 +40,62 @@ public class MemoryAuthDataRepository : IAuthDataRepository
userModel = MemoryData.users.FirstOrDefault(u => u.Email.Equals(email, StringComparison.OrdinalIgnoreCase));
if (!username.IsNullOrEmpty())
userModel = MemoryData.users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.OrdinalIgnoreCase));
return userModel != null ? Result<UserModel>.Ok(userModel) : Result<UserModel>.Failed("No user found!");
return userModel != null ? ResultOld<UserModel>.Ok(userModel) : ResultOld<UserModel>.Failed("No user found!");
}
public Task<ListResult<UserItemModel>> GetUsersAsync(int start = 0, int amount = 30, string search = "")
public Task<ListResultOld<UserItemModel>> GetUsersAsync(int start = 0, int amount = 30, string search = "")
{
throw new NotImplementedException();
}
public Task<Result> CreateGroupAsync(GroupModel group)
public Task<ResultOld> CreateGroupAsync(GroupModel group)
{
throw new NotImplementedException();
}
public Task<Result> UpdateGroupAsync(GroupModel group)
public Task<ResultOld> UpdateGroupAsync(GroupModel group)
{
throw new NotImplementedException();
}
public Task<Result> DeleteGroupAsync(GroupModel group)
public Task<ResultOld> DeleteGroupAsync(GroupModel group)
{
throw new NotImplementedException();
}
public Task<Result<GroupModel>> GetGroupAsync(string id)
public Task<ResultOld<GroupModel>> GetGroupAsync(string id)
{
throw new NotImplementedException();
}
public Task<ListResult<GroupItemModel>> GetGroupsAsync(int start = 0, int amount = 30, string search = "")
public Task<ListResultOld<GroupItemModel>> GetGroupsAsync(int start = 0, int amount = 30, string search = "")
{
throw new NotImplementedException();
}
public async Task<Result> CreateAuthenticationStateAsync(AuthenticationStateModel authenticationState)
public async Task<ResultOld> CreateAuthenticationStateAsync(AuthenticationStateModel authenticationState)
{
if (MemoryData.AuthenticationStates.Contains(authenticationState)) return Result.Failed("Item already exists!");
if (MemoryData.AuthenticationStates.Contains(authenticationState)) return ResultOld.Failed("Item already exists!");
MemoryData.AuthenticationStates.Add(authenticationState);
return Result.Ok();
return ResultOld.Ok();
}
public Task<Result> UpdateAuthenticationStateAsync(AuthenticationStateModel authenticationState)
public Task<ResultOld> UpdateAuthenticationStateAsync(AuthenticationStateModel authenticationState)
{
throw new NotImplementedException();
}
public async Task<Result> DeleteAuthenticationStateAsync(AuthenticationStateModel authenticationState)
public async Task<ResultOld> DeleteAuthenticationStateAsync(AuthenticationStateModel authenticationState)
{
MemoryData.AuthenticationStates.Remove(authenticationState);
return Result.Ok();
return ResultOld.Ok();
}
public async Task<Result<AuthenticationStateModel>> GetAuthenticationStateAsync(string id)
public async Task<ResultOld<AuthenticationStateModel>> GetAuthenticationStateAsync(string id)
{
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);
if (item == null) return ResultOld<AuthenticationStateModel>.Failed("Could not get the session state!");
return ResultOld<AuthenticationStateModel>.Ok(item);
}
}