WebApi + ClientApp, GraphQL, Reflection

This commit is contained in:
Boris Milašinović
2026-05-06 20:55:05 +02:00
parent 8f7c704a90
commit 4fb3de19f6
196 changed files with 10395 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace Events.WebAPI.Util.Extensions;
public static class AddJsonPatchSupportExtension
{
public static void AddJsonPatchSupport(this MvcOptions options)
{
options.InputFormatters.Insert(0, GetJsonPatchInputFormatter());
}
// Add Newtonsoft only to the JSON Patch formatter so the rest of the API keeps System.Text.Json.
private static NewtonsoftJsonPatchInputFormatter GetJsonPatchInputFormatter()
{
var builder = new ServiceCollection()
.AddLogging()
.AddControllers()
.AddNewtonsoftJson()
.Services
.BuildServiceProvider();
return builder
.GetRequiredService<IOptions<MvcOptions>>()
.Value
.InputFormatters
.OfType<NewtonsoftJsonPatchInputFormatter>()
.First();
}
}

View File

@@ -0,0 +1,20 @@
namespace Events.WebAPI.Util.Extensions;
public static class DictionaryExtensions
{
public static TValue GetOrCreate<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key) where TValue : new() where TKey : notnull
{
if (!dict.ContainsKey(key))
{
var item = new TValue();
dict[key] = item;
return item;
}
else
{
return dict[key];
}
}
}

View File

@@ -0,0 +1,28 @@
using System.Text;
namespace Events.WebAPI.Util.Extensions
{
/// <summary>
/// Class with useful extensions for exceptions handling
/// </summary>
public static class ExceptionExtensions
{
/// <summary>
/// return complete hierarchy of an exception. It checks whether the exception has inner exception,
/// and if it has, then it appends inner exception message.
/// Then it looks for inner exception of the inner exceptions, and so on.
/// </summary>
/// <param name="exc">Exception which message hiearchy should be obtained</param>
/// <returns>String containing all exception hierarchy messages</returns>
public static string CompleteExceptionMessage(this Exception? exc)
{
StringBuilder sb = new();
while (exc != null)
{
sb.AppendLine(exc.Message);
exc = exc.InnerException;
}
return sb.ToString();
}
}
}