Refactor enum handling in MemoryCache and HistoryController

Updated `GetEnumAsDictionary<TEnum>` in `MemoryCacheExtensions.cs` to use a loop for populating a dictionary of enum values, removing LINQ for simplicity.

Modified `HistoryController.cs` to adjust method signatures for `GetReferenceTypes` and `GetEnvelopeStatus`, allowing optional parameters for better conditional responses. Added necessary using directives.
This commit is contained in:
Developer 02 2025-05-08 12:00:48 +02:00
parent 3035ec7e9c
commit 7fefc68061
2 changed files with 16 additions and 25 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.Extensions.Caching.Memory;
using System;
namespace EnvelopeGenerator.Extensions;
@ -7,15 +8,13 @@ public static class MemoryCacheExtensions
public static IDictionary<string, int> GetEnumAsDictionary<TEnum>(this IMemoryCache memoryCache)
where TEnum : Enum
{
var referenceTypes = Enum.GetValues(typeof(TEnum))
.Cast<TEnum>()
.ToDictionary(rt =>
{
var key = rt.ToString();
var keyAsCamelCase = char.ToLowerInvariant(key[0]) + key[1..];
return keyAsCamelCase;
}, rt => Convert.ToInt32(rt));
var dict = new Dictionary<string, int>();
return referenceTypes;
foreach (TEnum role in Enum.GetValues(typeof(TEnum)))
{
dict[role.ToString()] = Convert.ToInt32(role);
}
return dict;
}
}

View File

@ -1,9 +1,11 @@
using EnvelopeGenerator.Application.Contracts.Services;
using DigitalData.EmailProfilerDispatcher.Abstraction.Entities;
using EnvelopeGenerator.Application.Contracts.Services;
using EnvelopeGenerator.Application.Histories.Queries.Read;
using EnvelopeGenerator.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.IdentityModel.Tokens;
using static EnvelopeGenerator.Common.Constants;
@ -51,9 +53,9 @@ public class HistoryController : ControllerBase
/// <response code="200"></response>
[HttpGet("related")]
[Authorize]
public IActionResult GetReferenceTypes()
public IActionResult GetReferenceTypes(ReferenceType? referenceType = null)
{
return Ok(_memoryCache.GetEnumAsDictionary<ReferenceType>());
return referenceType is null ? Ok(_memoryCache.GetEnumAsDictionary<ReferenceType>()) : Ok(referenceType.ToString());
}
/// <summary>
@ -85,7 +87,7 @@ public class HistoryController : ControllerBase
/// 3004: MessageDeletionSent
/// 3005: MessageCompletionSent
/// </summary>
/// <param name="related">
/// <param name="status">
/// Abfrageparameter, der angibt, auf welche Referenz sich der Status bezieht.
/// 0 - Sender: Historische Datensätze, die sich auf den Status des Absenders beziehen. Sie haben Statuscodes, die mit 1* beginnen.
/// 1 - Receiver: Historische Datensätze über den Status der Empfänger. Diese haben Statuscodes, die mit 2* beginnen.
@ -96,19 +98,9 @@ public class HistoryController : ControllerBase
/// <response code="200"></response>
[HttpGet("status")]
[Authorize]
public IActionResult GetEnvelopeStatus([FromQuery] ReferenceType? related = null)
public IActionResult GetEnvelopeStatus([FromQuery] EnvelopeStatus? status = null)
{
// Enum zu Schlüssel-Wert-Paar
var referenceTypes = Enum.GetValues(typeof(ReferenceType))
.Cast<ReferenceType>()
.ToDictionary(rt =>
{
var key = rt.ToString();
var keyAsCamelCase = char.ToLower(key[0]) + key[1..];
return keyAsCamelCase;
}, rt => (int)rt);
return Ok(referenceTypes);
return status is null ? Ok(_memoryCache.GetEnumAsDictionary<EnvelopeStatus>()) : Ok(status.ToString());
}
/// <summary>