Initial commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using GerstITS.Common;
|
||||
using GerstITS.Data;
|
||||
|
||||
namespace GerstITS.Examples.Logic.Shared
|
||||
{
|
||||
internal static class ISearchEngineExtensions
|
||||
{
|
||||
#region Methods
|
||||
|
||||
public static TEntity QueryBy<TEntity>(this IReadOnlyRepository repository, Expression<Func<TEntity, bool>> condition)
|
||||
where TEntity : IEntity
|
||||
{
|
||||
return repository.Query<TEntity>()
|
||||
.Where(condition)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public static TModel ThrowsAnExceptionIfNotFound<TModel, TKey>(this TModel model, TKey id)
|
||||
{
|
||||
if (model.IsNull())
|
||||
throw new EntityNotFoundException($"{typeof(TModel).Name} with id '{id}' not found.");
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace GerstITS.Examples.Logic.Shared.Search.Sorting
|
||||
{
|
||||
internal interface ISortable
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using GerstITS.Common;
|
||||
using GerstITS.Search;
|
||||
|
||||
namespace GerstITS.Examples.Logic.Shared.Search.Sorting
|
||||
{
|
||||
internal class SortingProvider : ISortingProvider
|
||||
{
|
||||
#region ISortingProvider
|
||||
|
||||
public bool CanSort<TItem>(IQueryable<TItem> query)
|
||||
{
|
||||
return typeof(TItem).IsAssignableTo(typeof(ISortable));
|
||||
}
|
||||
|
||||
public IQueryable<TItem> Sort<TItem>(IQueryable<TItem> query, ISorting sorting)
|
||||
{
|
||||
var propertyInfo = typeof(TItem).GetProperties()
|
||||
.SingleOrDefault(i => string.Equals(i.Name, sorting.SortBy, StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
if (!CanSort(query) || propertyInfo.IsNull())
|
||||
return query;
|
||||
|
||||
var sortExpression = CreateSortExpression<TItem>(propertyInfo);
|
||||
|
||||
return (sorting.SortDirection == SortingDirections.Ascending
|
||||
? query.OrderBy(sortExpression)
|
||||
: query.OrderByDescending(sortExpression))
|
||||
.AsQueryable();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
private static Expression<Func<TItem, object>> CreateSortExpression<TItem>(MemberInfo propertyInfo)
|
||||
{
|
||||
var parameterExpression = Expression.Parameter(typeof(TItem), "x");
|
||||
var propertyExpression = Expression.Property(parameterExpression, propertyInfo.Name);
|
||||
var convertExpression = Expression.Convert(propertyExpression, typeof(object));
|
||||
|
||||
return Expression.Lambda<Func<TItem, object>>(convertExpression, parameterExpression);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using FluentValidation;
|
||||
using GerstITS.Validation;
|
||||
|
||||
namespace GerstITS.Examples.Logic.Shared.Validation
|
||||
{
|
||||
internal sealed class IdValidationRule : ValidationRuleBase<int>
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public IdValidationRule()
|
||||
{
|
||||
RuleFor(x => x)
|
||||
.GreaterThan(0)
|
||||
.OverridePropertyName("id");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user