[CHANGE] Removed old result monad

This commit is contained in:
max 2025-04-12 14:26:47 +02:00
parent 7ed219d08a
commit 095b66f6f3
3 changed files with 15 additions and 121 deletions

View File

@ -2,18 +2,5 @@ namespace DotBased.AspNet.Authority.EFCore.Repositories;
public abstract class RepositoryBase public abstract class RepositoryBase
{ {
protected ResultOld<T> HandleExceptionResult<T>(string message, Exception ex) => new(HandleException(message, ex));
protected ListResultOld<T> HandleExceptionListResult<T>(string message, Exception ex) =>
new(HandleException(message, ex));
protected ResultOld HandleException(string message, Exception ex)
{
if (ex is OperationCanceledException oce)
{
return ResultOld.Failed("Operation cancelled.", oce);
}
return ResultOld.Failed(message, ex);
}
} }

View File

@ -1,94 +0,0 @@
namespace DotBased;
/// <summary>
/// Simple result class for returning a result state or a message and an exception.
/// </summary>
public class ResultOld
{
public ResultOld(bool success, string message, Exception? exception)
{
Success = success;
Message = message;
Exception = exception;
}
public ResultOld(ResultOld bObj)
{
Success = bObj.Success;
Message = bObj.Message;
Exception = bObj.Exception;
}
public bool Success { get; set; }
public string Message { get; set; }
public Exception? Exception { get; set; }
public static ResultOld Ok() => new(true, string.Empty, null);
public static ResultOld Failed(string message, Exception? exception = null) => new(false, message, exception);
}
public class ResultOld<TValue> : ResultOld
{
public ResultOld(bool success, string message, TValue? value, Exception? exception) : base(success, message, exception)
{
Value = value;
}
public ResultOld(ResultOld bObj) : base(bObj)
{
}
public TValue? Value { get; set; }
public static ResultOld<TValue> Ok(TValue value) => new(true, string.Empty, value, null);
public new static ResultOld<TValue> Failed(string message, Exception? exception = null) =>
new(false, message, default, exception);
public static ResultOld<TValue> HandleResult(TValue? value, string failedMessage, Exception? exception = null)
{
return value == null ? Failed(failedMessage, exception) : Ok(value);
}
}
public class ListResultOld<TItem> : ResultOld
{
public ListResultOld(bool success, string message, int totalCount, IEnumerable<TItem>? items, int limit = -1, int offset = -1, Exception? exception = null) : base(success, message, exception)
{
Items = items != null ? new List<TItem>(items) : new List<TItem>();
TotalCount = totalCount;
Limit = limit;
Offset = offset;
}
public ListResultOld(ResultOld bObj) : base(bObj)
{
Items = new List<TItem>();
}
public readonly IReadOnlyList<TItem> Items;
/// <summary>
/// The amount of items that this result contains.
/// </summary>
public int Count => Items.Count;
/// <summary>
/// The total amount of item that is available.
/// </summary>
public int TotalCount { get; }
/// <summary>
/// The limit this result contains
/// </summary>
public int Limit { get; }
/// <summary>
/// The offset this result has the items from.
/// </summary>
public int Offset { get; }
public static ListResultOld<TItem> Ok(IEnumerable<TItem> items, int totalCount = -1, int limit = -1, int offset = -1) =>
new(true, string.Empty, totalCount, items, limit, offset);
public new static ListResultOld<TItem> Failed(string message, Exception? exception = null) =>
new(false, message, -1, null, exception: exception);
}

View File

@ -1,4 +1,5 @@
using System.Security.Cryptography; using System.Security.Cryptography;
using DotBased.Monads;
namespace DotBased.Utilities; namespace DotBased.Utilities;
@ -7,12 +8,12 @@ public static class Cryptography
/* /*
* https://gist.github.com/therightstuff/aa65356e95f8d0aae888e9f61aa29414 * https://gist.github.com/therightstuff/aa65356e95f8d0aae888e9f61aa29414
*/ */
public static ResultOld<string> ExportPublicKeyToPem(RSACryptoServiceProvider csp) public static Result<string> ExportPublicKeyToPem(RSACryptoServiceProvider csp)
{ {
var outputStream = new StringWriter(); var outputStream = new StringWriter();
var parameters = csp.ExportParameters(false); var parameters = csp.ExportParameters(false);
if (parameters.Exponent == null || parameters.Modulus == null) if (parameters.Exponent == null || parameters.Modulus == null)
return ResultOld<string>.Failed("RSAParameters are empty!"); return ResultError.Fail("RSAParameters are empty!");
using (var stream = new MemoryStream()) using (var stream = new MemoryStream())
{ {
var writer = new BinaryWriter(stream); var writer = new BinaryWriter(stream);
@ -23,7 +24,7 @@ public static class Cryptography
innerWriter.Write((byte)0x30); // SEQUENCE innerWriter.Write((byte)0x30); // SEQUENCE
EncodeLength(innerWriter, 13); EncodeLength(innerWriter, 13);
innerWriter.Write((byte)0x06); // OBJECT IDENTIFIER innerWriter.Write((byte)0x06); // OBJECT IDENTIFIER
byte[] rsaEncryptionOid = new byte[] { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01 }; var rsaEncryptionOid = new byte[] { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01 };
EncodeLength(innerWriter, rsaEncryptionOid.Length); EncodeLength(innerWriter, rsaEncryptionOid.Length);
innerWriter.Write(rsaEncryptionOid); innerWriter.Write(rsaEncryptionOid);
innerWriter.Write((byte)0x05); // NULL innerWriter.Write((byte)0x05); // NULL
@ -44,20 +45,20 @@ public static class Cryptography
bitStringWriter.Write(paramsStream.GetBuffer(), 0, paramsLength); bitStringWriter.Write(paramsStream.GetBuffer(), 0, paramsLength);
} }
int bitStringLength = (int)bitStringStream.Length; var bitStringLength = (int)bitStringStream.Length;
EncodeLength(innerWriter, bitStringLength); EncodeLength(innerWriter, bitStringLength);
innerWriter.Write(bitStringStream.GetBuffer(), 0, bitStringLength); innerWriter.Write(bitStringStream.GetBuffer(), 0, bitStringLength);
} }
int length = (int)innerStream.Length; var length = (int)innerStream.Length;
EncodeLength(writer, length); EncodeLength(writer, length);
writer.Write(innerStream.GetBuffer(), 0, length); writer.Write(innerStream.GetBuffer(), 0, length);
} }
char[] base64 = Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length).ToCharArray(); var base64 = Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length).ToCharArray();
// WriteLine terminates with \r\n, we want only \n // WriteLine terminates with \r\n, we want only \n
outputStream.Write("-----BEGIN PUBLIC KEY-----\n"); outputStream.Write("-----BEGIN PUBLIC KEY-----\n");
for (int i = 0; i < base64.Length; i += 64) for (var i = 0; i < base64.Length; i += 64)
{ {
outputStream.Write(base64, i, Math.Min(64, base64.Length - i)); outputStream.Write(base64, i, Math.Min(64, base64.Length - i));
outputStream.Write("\n"); outputStream.Write("\n");
@ -66,7 +67,7 @@ public static class Cryptography
outputStream.Write("-----END PUBLIC KEY-----"); outputStream.Write("-----END PUBLIC KEY-----");
} }
return ResultOld<string>.Ok(outputStream.ToString()); return outputStream.ToString();
} }
private static void EncodeLength(BinaryWriter stream, int length) private static void EncodeLength(BinaryWriter stream, int length)
@ -82,15 +83,15 @@ public static class Cryptography
default: default:
{ {
// Long form // Long form
int temp = length; var temp = length;
int bytesRequired = 0; var bytesRequired = 0;
while (temp > 0) while (temp > 0)
{ {
temp >>= 8; temp >>= 8;
bytesRequired++; bytesRequired++;
} }
stream.Write((byte)(bytesRequired | 0x80)); stream.Write((byte)(bytesRequired | 0x80));
for (int i = bytesRequired - 1; i >= 0; i--) for (var i = bytesRequired - 1; i >= 0; i--)
{ {
stream.Write((byte)(length >> (8 * i) & 0xff)); stream.Write((byte)(length >> (8 * i) & 0xff));
} }
@ -102,7 +103,7 @@ public static class Cryptography
private static void EncodeIntegerBigEndian(BinaryWriter stream, byte[] value, bool forceUnsigned = true) private static void EncodeIntegerBigEndian(BinaryWriter stream, byte[] value, bool forceUnsigned = true)
{ {
stream.Write((byte)0x02); // INTEGER stream.Write((byte)0x02); // INTEGER
int prefixZeros = value.TakeWhile(t => t == 0).Count(); var prefixZeros = value.TakeWhile(t => t == 0).Count();
if (value.Length - prefixZeros == 0) if (value.Length - prefixZeros == 0)
{ {
EncodeLength(stream, 1); EncodeLength(stream, 1);
@ -120,7 +121,7 @@ public static class Cryptography
{ {
EncodeLength(stream, value.Length - prefixZeros); EncodeLength(stream, value.Length - prefixZeros);
} }
for (int i = prefixZeros; i < value.Length; i++) for (var i = prefixZeros; i < value.Length; i++)
{ {
stream.Write(value[i]); stream.Write(value[i]);
} }