mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 10:36:33 +00:00
structure app by rest api standards
add getusers tidy up BusinessException
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Azaion.Common.Extensions;
|
||||
|
||||
/// <summary>Enum extensions</summary>
|
||||
public static class EnumExtensions
|
||||
{
|
||||
/// <summary>Get all enums with descriptions </summary>
|
||||
public static Dictionary<T, string> GetDescriptions<T>() where T : Enum =>
|
||||
Enum.GetValues(typeof(T)).Cast<T>()
|
||||
.ToDictionary(x => x, x => x.GetEnumAttrib<T, DescriptionAttribute>()?.Description ?? x.ToString());
|
||||
|
||||
/// <summary>
|
||||
/// Get the Description from the DescriptionAttribute.
|
||||
/// </summary>
|
||||
/// <param name="enumValue"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetDescription(this Enum enumValue)
|
||||
{
|
||||
return enumValue.GetType()
|
||||
.GetMember(enumValue.ToString())
|
||||
.First()
|
||||
.GetCustomAttribute<DescriptionAttribute>()?
|
||||
.Description ?? enumValue.ToString();
|
||||
}
|
||||
|
||||
/// <summary> Get attribute for enum's member, usually is used for getting Description attribute </summary>
|
||||
public static TAttrib GetEnumAttrib<T, TAttrib>(this T value) where T: Enum
|
||||
{
|
||||
var field = value.GetType().GetField(value.ToString());
|
||||
if (field == null)
|
||||
return default;
|
||||
|
||||
return field.GetCustomAttributes(typeof(TAttrib), false)
|
||||
.Cast<TAttrib>()
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get default value for enum
|
||||
/// </summary>
|
||||
/// <typeparam name="TEnum"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static TEnum GetDefaultValue<TEnum>() where TEnum : struct
|
||||
{
|
||||
var t = typeof(TEnum);
|
||||
var attributes = (DefaultValueAttribute[])t.GetCustomAttributes(typeof(DefaultValueAttribute), false);
|
||||
if (attributes is { Length: > 0 })
|
||||
return (TEnum)attributes[0].Value!;
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Azaion.Common.Extensions;
|
||||
|
||||
public static class QueryableExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds Where true predicate only if result of condition is true.
|
||||
/// If false predicate provided, uses it in case of false result
|
||||
/// Useful for filters, when filters should be applied only when it was set (not NULL)
|
||||
/// </summary>
|
||||
public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> query, bool? condition,
|
||||
Expression<Func<TSource, bool>> truePredicate,
|
||||
Expression<Func<TSource, bool>>? falsePredicate = null)
|
||||
{
|
||||
if (!condition.HasValue)
|
||||
return query;
|
||||
|
||||
if (condition.Value)
|
||||
return query.Where(truePredicate);
|
||||
|
||||
return falsePredicate != null
|
||||
? query.Where(falsePredicate)
|
||||
: query;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Azaion.Common.Extensions;
|
||||
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static string ToSnakeCase(this string text)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text))
|
||||
return text;
|
||||
|
||||
if (text.Length < 2)
|
||||
return text.ToLowerInvariant();
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.Append(char.ToLowerInvariant(text[0]));
|
||||
for (int i = 1; i < text.Length; ++i) {
|
||||
var c = text[i];
|
||||
if(char.IsUpper(c)) {
|
||||
sb.Append('_');
|
||||
sb.Append(char.ToLowerInvariant(c));
|
||||
} else {
|
||||
sb.Append(c);
|
||||
}
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user