Refactoring, database structure changed
This commit is contained in:
8
WatchIt.DTO/Query/Filter.cs
Normal file
8
WatchIt.DTO/Query/Filter.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace WatchIt.DTO.Query;
|
||||
|
||||
public abstract record Filter<T>(Expression<Func<T, bool>> Condition)
|
||||
{
|
||||
public static implicit operator Expression<Func<T, bool>>(Filter<T> filter) => filter.Condition;
|
||||
}
|
||||
20
WatchIt.DTO/Query/IFilterQuery.cs
Normal file
20
WatchIt.DTO/Query/IFilterQuery.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace WatchIt.DTO.Query;
|
||||
|
||||
public interface IFilterQuery;
|
||||
|
||||
public interface IFilterQuery<T> : IFilterQuery
|
||||
{
|
||||
internal abstract IEnumerable<Filter<T>> GetFilters();
|
||||
}
|
||||
|
||||
public static class FilterQueryExtensions
|
||||
{
|
||||
public static IQueryable<T> ApplyFilter<T>(this IQueryable<T> data, IFilterQuery<T> filterQuery)
|
||||
{
|
||||
foreach (Filter<T> filter in filterQuery.GetFilters())
|
||||
{
|
||||
data = data.Where(filter);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
38
WatchIt.DTO/Query/OrderQuery.cs
Normal file
38
WatchIt.DTO/Query/OrderQuery.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Refit;
|
||||
using WatchIt.Database.Model.Media;
|
||||
|
||||
namespace WatchIt.DTO.Query;
|
||||
|
||||
public class OrderQuery
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
[FromQuery(Name = "order_asc")]
|
||||
[AliasAs("order_asc")]
|
||||
public bool OrderAscending { get; set; }
|
||||
|
||||
[FromQuery(Name = "order_by")]
|
||||
[AliasAs("order_by")]
|
||||
public string? OrderBy { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public static class OrderQueryExtensions
|
||||
{
|
||||
public static IQueryable<T> ApplyOrder<T>(this IQueryable<T> data, OrderQuery orderQuery, params IDictionary<string, Expression<Func<T, object?>>>[] orderKeys)
|
||||
{
|
||||
if (orderQuery.OrderBy is not null)
|
||||
{
|
||||
Dictionary<string, Expression<Func<T, object?>>> orderByKeys = orderKeys.SelectMany(x => x)
|
||||
.ToDictionary(x => x.Key, x => x.Value);
|
||||
if (orderByKeys.TryGetValue(orderQuery.OrderBy, out Expression<Func<T, object?>>? orderFunc))
|
||||
{
|
||||
data = orderQuery.OrderAscending ? data.OrderBy(orderFunc) : data.OrderByDescending(orderFunc);
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
28
WatchIt.DTO/Query/PagingQuery.cs
Normal file
28
WatchIt.DTO/Query/PagingQuery.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WatchIt.DTO.Query;
|
||||
|
||||
public sealed record PagingQuery
|
||||
{
|
||||
[FromQuery(Name = "first")]
|
||||
public int? First { get; set; }
|
||||
|
||||
[FromQuery(Name = "after")]
|
||||
public int? After { get; set; }
|
||||
}
|
||||
|
||||
public static class PagingQueryExtensions
|
||||
{
|
||||
public static IQueryable<T> ApplyPaging<T>(this IQueryable<T> data, PagingQuery pagingQuery)
|
||||
{
|
||||
if (pagingQuery.After is not null)
|
||||
{
|
||||
data = data.Skip(pagingQuery.After.Value);
|
||||
}
|
||||
if (pagingQuery.First is not null)
|
||||
{
|
||||
data = data.Take(pagingQuery.First.Value);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user