Improved results handling in service layer

This commit is contained in:
2026-09-11 10:32:51 +02:00
parent 66f720a20b
commit a15b25248d
11 changed files with 87 additions and 103 deletions
@@ -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);
}));
}