Init
This commit is contained in:
107
Prefab/Data/Queries/GenericAttributeQueries.cs
Normal file
107
Prefab/Data/Queries/GenericAttributeQueries.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Prefab.Data.Entities;
|
||||
|
||||
using Prefab.Domain.Common;
|
||||
|
||||
namespace Prefab.Data.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// Provides extension methods for the <see cref="GenericAttribute"/> entity.
|
||||
/// </summary>
|
||||
public static class GenericAttributeQueries
|
||||
{
|
||||
/// <summary>
|
||||
/// Query for records where the <see cref="GenericAttribute.EntityId"/> field matches the given entity.
|
||||
/// </summary>
|
||||
public static IQueryable<GenericAttribute> ForEntity(this IQueryable<GenericAttribute> query, IEntity<Guid> entity)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(entity);
|
||||
|
||||
return query.Where(x => x.EntityId == entity.Id && x.KeyGroup == entity.GetType().FullName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Query for records in a module-specific view where the EntityId matches the given entity.
|
||||
/// </summary>
|
||||
public static IQueryable<TView> ForEntity<TView>(this IQueryable<TView> query, IEntity<Guid> entity, string keyGroup)
|
||||
where TView : class
|
||||
{
|
||||
return GenericAttributeViewQueries.ForEntity(query, entity, keyGroup);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Query for records where the <see cref="GenericAttribute.Value"/> field contains characters from the given string value.
|
||||
/// </summary>
|
||||
public static IQueryable<GenericAttribute> WhereTheValueContains(this IQueryable<GenericAttribute> query, string value)
|
||||
{
|
||||
return query.Where(x => x.Value.Contains(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Filter by key (exact match).
|
||||
/// </summary>
|
||||
private static IQueryable<GenericAttribute> WhereKey(
|
||||
this IQueryable<GenericAttribute> q, string key) =>
|
||||
q.Where(x => x.Key == key);
|
||||
|
||||
/// <summary>
|
||||
/// Filter by key-group (exact match).
|
||||
/// </summary>
|
||||
public static IQueryable<GenericAttribute> WhereGroup(
|
||||
this IQueryable<GenericAttribute> q, string keyGroup) =>
|
||||
q.Where(x => x.KeyGroup == keyGroup);
|
||||
|
||||
/// <summary>
|
||||
/// Filter by type (exact match).
|
||||
/// </summary>
|
||||
public static IQueryable<GenericAttribute> WhereType(
|
||||
this IQueryable<GenericAttribute> q, string type) =>
|
||||
q.Where(x => x.Type == type);
|
||||
|
||||
/// <summary>
|
||||
/// Filter by any of the given keys.
|
||||
/// </summary>
|
||||
public static IQueryable<GenericAttribute> WhereKeys(
|
||||
this IQueryable<GenericAttribute> q, params string[] keys) =>
|
||||
q.Where(x => keys.Contains(x.Key));
|
||||
|
||||
/// <summary>
|
||||
/// Case-insensitive substring search on Value using EF.Functions.Like.
|
||||
/// </summary>
|
||||
public static IQueryable<GenericAttribute> WhereValueLike(
|
||||
this IQueryable<GenericAttribute> q, string pattern) =>
|
||||
q.Where(x => EF.Functions.Like(x.Value, pattern));
|
||||
|
||||
/// <summary>
|
||||
/// Project into a key→value dictionary.
|
||||
/// </summary>
|
||||
public static async Task<Dictionary<string, string>> ToDictionaryAsync(
|
||||
this IQueryable<GenericAttribute> q,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
return await q
|
||||
.Select(x => new { x.Key, x.Value })
|
||||
.ToDictionaryAsync(x => x.Key, x => x.Value, ct)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shortcut to fetch a single attribute value (or null).
|
||||
/// </summary>
|
||||
public static async Task<string?> GetValueAsync(
|
||||
this IQueryable<GenericAttribute> q, string key, CancellationToken ct = default)
|
||||
{
|
||||
return await q
|
||||
.WhereKey(key)
|
||||
.Select(x => x.Value)
|
||||
.FirstOrDefaultAsync(ct)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Apply AsNoTracking for read-only queries.
|
||||
/// </summary>
|
||||
public static IQueryable<GenericAttribute> AsReadOnly(
|
||||
this IQueryable<GenericAttribute> q) =>
|
||||
q.AsNoTracking();
|
||||
}
|
||||
Reference in New Issue
Block a user