From 095b66f6f3a9d22e367b6decbc7736f4be12bb7c Mon Sep 17 00:00:00 2001 From: max Date: Sat, 12 Apr 2025 14:26:47 +0200 Subject: [PATCH] [CHANGE] Removed old result monad --- .../Repositories/RepositoryBase.cs | 15 +-- DotBased/ResultOld.cs | 94 ------------------- DotBased/Utilities/Cryptography.cs | 27 +++--- 3 files changed, 15 insertions(+), 121 deletions(-) delete mode 100755 DotBased/ResultOld.cs diff --git a/DotBased.AspNet.Authority.EFCore/Repositories/RepositoryBase.cs b/DotBased.AspNet.Authority.EFCore/Repositories/RepositoryBase.cs index 89fbf20..a32d566 100644 --- a/DotBased.AspNet.Authority.EFCore/Repositories/RepositoryBase.cs +++ b/DotBased.AspNet.Authority.EFCore/Repositories/RepositoryBase.cs @@ -2,18 +2,5 @@ namespace DotBased.AspNet.Authority.EFCore.Repositories; public abstract class RepositoryBase { - protected ResultOld HandleExceptionResult(string message, Exception ex) => new(HandleException(message, ex)); - - protected ListResultOld HandleExceptionListResult(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); - } + } \ No newline at end of file diff --git a/DotBased/ResultOld.cs b/DotBased/ResultOld.cs deleted file mode 100755 index 5805d6c..0000000 --- a/DotBased/ResultOld.cs +++ /dev/null @@ -1,94 +0,0 @@ -namespace DotBased; - -/// -/// Simple result class for returning a result state or a message and an exception. -/// -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 : 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 Ok(TValue value) => new(true, string.Empty, value, null); - - public new static ResultOld Failed(string message, Exception? exception = null) => - new(false, message, default, exception); - - public static ResultOld HandleResult(TValue? value, string failedMessage, Exception? exception = null) - { - return value == null ? Failed(failedMessage, exception) : Ok(value); - } -} - -public class ListResultOld : ResultOld -{ - public ListResultOld(bool success, string message, int totalCount, IEnumerable? items, int limit = -1, int offset = -1, Exception? exception = null) : base(success, message, exception) - { - Items = items != null ? new List(items) : new List(); - TotalCount = totalCount; - Limit = limit; - Offset = offset; - } - - public ListResultOld(ResultOld bObj) : base(bObj) - { - Items = new List(); - } - - public readonly IReadOnlyList Items; - /// - /// The amount of items that this result contains. - /// - public int Count => Items.Count; - - /// - /// The total amount of item that is available. - /// - public int TotalCount { get; } - - /// - /// The limit this result contains - /// - public int Limit { get; } - - /// - /// The offset this result has the items from. - /// - public int Offset { get; } - - public static ListResultOld Ok(IEnumerable items, int totalCount = -1, int limit = -1, int offset = -1) => - new(true, string.Empty, totalCount, items, limit, offset); - - public new static ListResultOld Failed(string message, Exception? exception = null) => - new(false, message, -1, null, exception: exception); -} \ No newline at end of file diff --git a/DotBased/Utilities/Cryptography.cs b/DotBased/Utilities/Cryptography.cs index c0fd022..31cc651 100755 --- a/DotBased/Utilities/Cryptography.cs +++ b/DotBased/Utilities/Cryptography.cs @@ -1,4 +1,5 @@ using System.Security.Cryptography; +using DotBased.Monads; namespace DotBased.Utilities; @@ -7,12 +8,12 @@ public static class Cryptography /* * https://gist.github.com/therightstuff/aa65356e95f8d0aae888e9f61aa29414 */ - public static ResultOld ExportPublicKeyToPem(RSACryptoServiceProvider csp) + public static Result ExportPublicKeyToPem(RSACryptoServiceProvider csp) { var outputStream = new StringWriter(); var parameters = csp.ExportParameters(false); if (parameters.Exponent == null || parameters.Modulus == null) - return ResultOld.Failed("RSAParameters are empty!"); + return ResultError.Fail("RSAParameters are empty!"); using (var stream = new MemoryStream()) { var writer = new BinaryWriter(stream); @@ -23,7 +24,7 @@ public static class Cryptography innerWriter.Write((byte)0x30); // SEQUENCE EncodeLength(innerWriter, 13); 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); innerWriter.Write(rsaEncryptionOid); innerWriter.Write((byte)0x05); // NULL @@ -44,20 +45,20 @@ public static class Cryptography bitStringWriter.Write(paramsStream.GetBuffer(), 0, paramsLength); } - int bitStringLength = (int)bitStringStream.Length; + var bitStringLength = (int)bitStringStream.Length; EncodeLength(innerWriter, bitStringLength); innerWriter.Write(bitStringStream.GetBuffer(), 0, bitStringLength); } - int length = (int)innerStream.Length; + var length = (int)innerStream.Length; EncodeLength(writer, 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 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("\n"); @@ -66,7 +67,7 @@ public static class Cryptography outputStream.Write("-----END PUBLIC KEY-----"); } - return ResultOld.Ok(outputStream.ToString()); + return outputStream.ToString(); } private static void EncodeLength(BinaryWriter stream, int length) @@ -82,15 +83,15 @@ public static class Cryptography default: { // Long form - int temp = length; - int bytesRequired = 0; + var temp = length; + var bytesRequired = 0; while (temp > 0) { temp >>= 8; bytesRequired++; } 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)); } @@ -102,7 +103,7 @@ public static class Cryptography private static void EncodeIntegerBigEndian(BinaryWriter stream, byte[] value, bool forceUnsigned = true) { 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) { EncodeLength(stream, 1); @@ -120,7 +121,7 @@ public static class Cryptography { 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]); }