Adding
This commit is contained in:
+58
@@ -0,0 +1,58 @@
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Extensions;
|
||||
|
||||
public static class ActivityExtensions
|
||||
{
|
||||
public static StateChangeRequirement RequiredToChangeState(this Activity activity, ActivityState targetState)
|
||||
{
|
||||
return activity.State switch
|
||||
{
|
||||
ActivityState.Accepted => targetState switch
|
||||
{
|
||||
ActivityState.Accepted => StateChangeRequirement.AdminOrPermission,
|
||||
ActivityState.ManualReview => StateChangeRequirement.AdminOrPermission,
|
||||
ActivityState.PendingReferral when activity.Type == ActivityType.Referral => StateChangeRequirement.AdminOrPermission,
|
||||
ActivityState.PendingTransfer => StateChangeRequirement.AdminOrPermission,
|
||||
ActivityState.Rejected => StateChangeRequirement.AdminOrPermission,
|
||||
_ => StateChangeRequirement.NotAllowed
|
||||
},
|
||||
ActivityState.Created => targetState switch
|
||||
{
|
||||
ActivityState.Accepted => StateChangeRequirement.SystemOnly,
|
||||
ActivityState.ManualReview => StateChangeRequirement.SystemOnly,
|
||||
ActivityState.PendingReferral => StateChangeRequirement.SystemOnly,
|
||||
ActivityState.Rejected => StateChangeRequirement.SystemOnly,
|
||||
_ => StateChangeRequirement.NotAllowed
|
||||
},
|
||||
ActivityState.ManualReview => targetState switch
|
||||
{
|
||||
ActivityState.Accepted => StateChangeRequirement.AdminOrPermission,
|
||||
ActivityState.Rejected => StateChangeRequirement.Hierarchical,
|
||||
_ => StateChangeRequirement.SystemOnly
|
||||
},
|
||||
ActivityState.PendingReferral => targetState switch
|
||||
{
|
||||
ActivityState.Accepted => StateChangeRequirement.AdminOrPermission,
|
||||
ActivityState.Transferred => StateChangeRequirement.Hierarchical,
|
||||
_ => StateChangeRequirement.NotAllowed
|
||||
},
|
||||
ActivityState.PendingTransfer => targetState switch
|
||||
{
|
||||
ActivityState.Accepted => StateChangeRequirement.AdminOrPermission,
|
||||
ActivityState.Transferred => StateChangeRequirement.Hierarchical,
|
||||
_ => StateChangeRequirement.NotAllowed
|
||||
},
|
||||
ActivityState.Rejected => targetState switch
|
||||
{
|
||||
ActivityState.Accepted => StateChangeRequirement.AdminOrPermission,
|
||||
ActivityState.ManualReview => StateChangeRequirement.AdminOrPermission,
|
||||
ActivityState.PendingReferral when activity.Type == ActivityType.Referral => StateChangeRequirement.AdminOrPermission,
|
||||
_ => StateChangeRequirement.NotAllowed
|
||||
},
|
||||
ActivityState.Transferred => StateChangeRequirement.NotAllowed,
|
||||
_ => StateChangeRequirement.NotAllowed
|
||||
};
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Extensions;
|
||||
|
||||
public static class EntityFrameworkExtensions
|
||||
{
|
||||
public static IOrderedQueryable OrderBy<TSource>(this IQueryable query, string propertyName)
|
||||
{
|
||||
var entityType = typeof(TSource);
|
||||
|
||||
var propertyInfo = entityType.GetProperty(propertyName);
|
||||
ParameterExpression arg = Expression.Parameter(entityType, "x");
|
||||
MemberExpression property = Expression.Property(arg, propertyName);
|
||||
var selector = Expression.Lambda(property, new ParameterExpression[] { arg });
|
||||
|
||||
var enumarableType = typeof(System.Linq.Queryable);
|
||||
var method = enumarableType.GetMethods()
|
||||
.Where(m => m.Name == "OrderBy" && m.IsGenericMethodDefinition)
|
||||
.Where(m =>
|
||||
{
|
||||
var parameters = m.GetParameters().ToList();
|
||||
return parameters.Count == 2;
|
||||
}).Single();
|
||||
|
||||
MethodInfo genericMethod = method.MakeGenericMethod(entityType, propertyInfo.PropertyType);
|
||||
|
||||
var newQuery = (IOrderedQueryable)genericMethod.Invoke(genericMethod, new object[] { query, selector });
|
||||
return newQuery;
|
||||
}
|
||||
|
||||
public static IOrderedQueryable OrderByDescending<TSource>(this IQueryable query, string propertyName)
|
||||
{
|
||||
var entityType = typeof(TSource);
|
||||
|
||||
var propertyInfo = entityType.GetProperty(propertyName);
|
||||
ParameterExpression arg = Expression.Parameter(entityType, "x");
|
||||
MemberExpression property = Expression.Property(arg, propertyName);
|
||||
var selector = Expression.Lambda(property, new ParameterExpression[] { arg });
|
||||
|
||||
var enumarableType = typeof(System.Linq.Queryable);
|
||||
var method = enumarableType.GetMethods()
|
||||
.Where(m => m.Name == "OrderByDescending" && m.IsGenericMethodDefinition)
|
||||
.Where(m =>
|
||||
{
|
||||
var parameters = m.GetParameters().ToList();
|
||||
return parameters.Count == 2;
|
||||
}).Single();
|
||||
|
||||
MethodInfo genericMethod = method.MakeGenericMethod(entityType, propertyInfo.PropertyType);
|
||||
|
||||
var newQuery = (IOrderedQueryable)genericMethod.Invoke(genericMethod, new object[] { query, selector });
|
||||
return newQuery;
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
using LCU.Library.Internal.IncentiveApiModels.Common;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Extensions;
|
||||
|
||||
public static class ObjectExtensions
|
||||
{
|
||||
public static IEnumerable<PropertyValueVariance> GetPropertyValueVariances<T>(this T objectInstanceOne, T objectInstanceTwo)
|
||||
{
|
||||
var propertyValueVariances = new List<PropertyValueVariance>();
|
||||
var propertyInfoCollection = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(property => property.GetCustomAttributes().Any(attribute => attribute is UpdateAllowed));
|
||||
|
||||
foreach (var propertyInfo in propertyInfoCollection)
|
||||
{
|
||||
if (!Equals(propertyInfo.GetValue(objectInstanceOne), propertyInfo.GetValue(objectInstanceTwo)))
|
||||
{
|
||||
propertyValueVariances.Add(new PropertyValueVariance
|
||||
{
|
||||
ComparedProperty = propertyInfo.Name,
|
||||
PropertyValueOne = GetPropertyValueOrDefaultString(objectInstanceOne, propertyInfo),
|
||||
PropertyValueTwo = GetPropertyValueOrDefaultString(objectInstanceTwo, propertyInfo),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return propertyValueVariances;
|
||||
}
|
||||
|
||||
private static string GetPropertyValueOrDefaultString<T>(this T queryObject, PropertyInfo propertyInfo)
|
||||
{
|
||||
var propertyValue = propertyInfo.GetValue(queryObject);
|
||||
return propertyValue != null ? propertyValue.ToString() : "NULL";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user