Improved results handling in service layer
This commit is contained in:
@@ -33,9 +33,9 @@ public sealed class BrowserLocalStorageService(ProtectedLocalStorage storage)
|
||||
{
|
||||
var retrieval = await storage.GetAsync<string>(key);
|
||||
|
||||
return retrieval.Success && !string.IsNullOrWhiteSpace(retrieval.Value)
|
||||
? Result.Ok(retrieval.Value)
|
||||
: Result.Fail(NotFoundError.Create($"Could not find object by key {key}"));
|
||||
return Result.FailIf(!retrieval.Success && string.IsNullOrWhiteSpace(retrieval.Value),
|
||||
NotFoundError.Create($"Could not find object by key {key}"))
|
||||
.ToResult(retrieval.Value!);
|
||||
}));
|
||||
|
||||
public async ValueTask<Result<TValue>> GetAsync<TValue>(string key) where TValue : class =>
|
||||
@@ -43,8 +43,8 @@ public sealed class BrowserLocalStorageService(ProtectedLocalStorage storage)
|
||||
{
|
||||
var retrieval = await storage.GetAsync<TValue>(key);
|
||||
|
||||
return retrieval.Success && retrieval.Value is not null
|
||||
? Result.Ok(retrieval.Value)
|
||||
: Result.Fail(NotFoundError.Create($"Could not find object by key {key}"));
|
||||
return Result.FailIf(!retrieval.Success && retrieval.Value is null,
|
||||
NotFoundError.Create($"Could not find object by key {key}"))
|
||||
.ToResult(retrieval.Value!);
|
||||
}));
|
||||
}
|
||||
@@ -31,17 +31,18 @@ public sealed partial class HashService(IHashids hasher) : IService
|
||||
return Result.Ok(Convert.ToHexString(bytes).ToLowerInvariant());
|
||||
}
|
||||
|
||||
public Result<string> HashEncodeHex(string input) => string.IsNullOrWhiteSpace(input) || !Patterns.HexHashRegex.IsMatch(input)
|
||||
? Result.Fail<string>(ValidationError.Create("Input must be a valid hexadecimal string."))
|
||||
: Result.Ok(hasher.EncodeHex(input));
|
||||
public Result<string> HashEncodeHex(string input) =>
|
||||
Result.FailIf(string.IsNullOrWhiteSpace(input) || !Patterns.HexHashRegex.IsMatch(input),
|
||||
ValidationError.Create("Input must be a valid hexadecimal string."))
|
||||
.ToResult(hasher.EncodeHex(input));
|
||||
|
||||
public Result<string> HashEncodeIntId(int id) => id < 0
|
||||
? Result.Fail<string>(ValidationError.Create("Id cannot be negative."))
|
||||
: Result.Ok(hasher.Encode(id));
|
||||
public Result<string> HashEncodeIntId(int id) =>
|
||||
Result.FailIf(id < 0, ValidationError.Create("Id cannot be negative."))
|
||||
.ToResult(hasher.Encode(id));
|
||||
|
||||
public Result<string> HashEncodeLongId(long id) => id < 0
|
||||
? Result.Fail<string>(ValidationError.Create("Id cannot be negative."))
|
||||
: Result.Ok(hasher.EncodeLong(id));
|
||||
public Result<string> HashEncodeLongId(long id) =>
|
||||
Result.FailIf(id < 0, ValidationError.Create("Id cannot be negative."))
|
||||
.ToResult(hasher.EncodeLong(id));
|
||||
|
||||
public Result<int> DecodeIntIdHash(string hash)
|
||||
{
|
||||
@@ -50,9 +51,8 @@ public sealed partial class HashService(IHashids hasher) : IService
|
||||
|
||||
int[] decoded = hasher.Decode(hash);
|
||||
|
||||
return decoded.Length == 1
|
||||
? Result.Ok(decoded[0]) :
|
||||
Result.Fail<int>(ValidationError.Create("Invalid or modified Int hash token."));
|
||||
return Result.FailIf(decoded.Length <= 0, ValidationError.Create("Invalid or modified Int hash token."))
|
||||
.ToResult(decoded[0]);
|
||||
}
|
||||
|
||||
public Result<long> DecodeLongIdHash(string hash)
|
||||
@@ -62,8 +62,8 @@ public sealed partial class HashService(IHashids hasher) : IService
|
||||
|
||||
long[] decoded = hasher.DecodeLong(hash);
|
||||
|
||||
return decoded.Length == 1 ? Result.Ok(decoded[0]) :
|
||||
Result.Fail<long>(ValidationError.Create("Invalid or modified Long hash token."));
|
||||
return Result.FailIf(decoded.Length <= 0, ValidationError.Create("Invalid or modified Long hash token."))
|
||||
.ToResult(decoded[0]);
|
||||
}
|
||||
|
||||
public async Task<Result<string>> DecodeHexHash(string hex) =>
|
||||
@@ -71,8 +71,7 @@ public sealed partial class HashService(IHashids hasher) : IService
|
||||
{
|
||||
string decoded = hasher.DecodeHex(hex);
|
||||
|
||||
return string.IsNullOrEmpty(decoded)
|
||||
? Result.Fail<string>(ValidationError.Create("Invalid or corrupted hex hash."))
|
||||
: Result.Ok(decoded);
|
||||
return Result.FailIf(string.IsNullOrEmpty(decoded), ValidationError.Create("Invalid or corrupted hex hash."))
|
||||
.ToResult(decoded);
|
||||
}));
|
||||
}
|
||||
@@ -15,6 +15,6 @@ public sealed class IdentityService(IDbContextFactory<ApplicationDbContext> cont
|
||||
var userExistsAndActive = await context.Users
|
||||
.AnyAsync(u => u.Id == userId && u.Status == ActivityStatus.Active, cancellationToken);
|
||||
|
||||
return Result.FailIf(!userExistsAndActive, NotFoundError.Create());
|
||||
return Result.OkIf(userExistsAndActive, NotFoundError.Create());
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -29,16 +29,16 @@ public sealed class TokenService(ISecurityConnectApi connectApi, IOptions<Securi
|
||||
{
|
||||
var tokenResponse = JsonSerializer.Deserialize<Domain.Security.Models.TokenResponse>(contentRaw);
|
||||
|
||||
return !string.IsNullOrWhiteSpace(tokenResponse?.AccessToken)
|
||||
? Result.Ok(tokenResponse)
|
||||
: Result.Fail<Domain.Security.Models.TokenResponse>(NotFoundError.Create("Authentication succeeded, but no access token was found in the response payload."));
|
||||
return Result.FailIf(string.IsNullOrWhiteSpace(tokenResponse?.AccessToken),
|
||||
NotFoundError.Create("Authentication succeeded, but no access token was found in the response payload."))
|
||||
.ToResult(tokenResponse!);
|
||||
}
|
||||
|
||||
return await Result.Try((Func<ValueTask<Result<Domain.Security.Models.TokenResponse>>>)(async () =>
|
||||
{
|
||||
var errorResult = JsonSerializer.Deserialize<TokenErrorResponse>(contentRaw);
|
||||
|
||||
return Result.FailIf(errorResult != null,
|
||||
return Result.OkIf(errorResult is not null,
|
||||
CreateError.Create($"{errorResult!.Error}: {errorResult.ErrorDescription}"));
|
||||
}));
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user