Events-MVC (example with htmx)
This commit is contained in:
28
Events-MVC/Events.MVC/Util/Extensions/ExceptionExtensions.cs
Normal file
28
Events-MVC/Events.MVC/Util/Extensions/ExceptionExtensions.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Events.MVC.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using System.Text;
|
||||
|
||||
namespace Events.MVC.Util.Extensions
|
||||
{
|
||||
public static class ModelStateExtensions
|
||||
{
|
||||
public static string GetErrorsString(this ModelStateDictionary modelState)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
foreach (var modelStateEntry in modelState)
|
||||
{
|
||||
if (modelStateEntry.Value.Errors.Count > 0)
|
||||
{
|
||||
string key = modelStateEntry.Key;
|
||||
string error = string.Join(", ", modelStateEntry.Value.Errors.Select(e => e.ErrorMessage));
|
||||
sb.AppendFormat("{0}: {1}; ", key, error);
|
||||
}
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using Sieve.Models;
|
||||
|
||||
namespace Events.MVC.Util.Extensions;
|
||||
|
||||
public static class SieveModelExtensions
|
||||
{
|
||||
public static void SetDefaultPagingAndSorting(this SieveModel sieveModel, int defaultPageSize, string defaultSort)
|
||||
{
|
||||
sieveModel.Page ??= 1;
|
||||
|
||||
if (sieveModel.Page < 1)
|
||||
{
|
||||
sieveModel.Page = 1;
|
||||
}
|
||||
|
||||
if (sieveModel.PageSize is null || sieveModel.PageSize <= 0)
|
||||
{
|
||||
sieveModel.PageSize = defaultPageSize;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(sieveModel.Sorts))
|
||||
{
|
||||
sieveModel.Sorts = defaultSort;
|
||||
}
|
||||
}
|
||||
|
||||
public static string ExtractFilterValue(this SieveModel sieveModel, string propertyName)
|
||||
{
|
||||
var filters = sieveModel.Filters?.Trim() ?? string.Empty;
|
||||
return ExtractFilterValue(filters, propertyName);
|
||||
}
|
||||
|
||||
public static string ExtractFilterValue(string filters, string propertyName)
|
||||
{
|
||||
return ExtractFilterValue(filters, propertyName, "@=*", "@=");
|
||||
}
|
||||
|
||||
public static string ExtractFilterValue(string filters, string propertyName, params string[] operators)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(filters))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
foreach (var filter in filters.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
|
||||
{
|
||||
foreach (var filterOperator in operators)
|
||||
{
|
||||
var prefix = $"{propertyName}{filterOperator}";
|
||||
if (filter.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return filter[prefix.Length..];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
10
Events-MVC/Events.MVC/Util/Extensions/StringExtensions.cs
Normal file
10
Events-MVC/Events.MVC/Util/Extensions/StringExtensions.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Events.MVC.Util.Extensions;
|
||||
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static string? TrimToNull(this string? value)
|
||||
{
|
||||
var trimmed = value?.Trim();
|
||||
return string.IsNullOrEmpty(trimmed) ? null : trimmed;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user