This commit is contained in:
2025-10-27 17:39:18 -04:00
commit 31f723bea4
1579 changed files with 642409 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
using Prefab.Domain.Common;
namespace Prefab.Data.Queries;
/// <summary>
/// Provides reusable audit-related query filters for entity sets.
/// </summary>
public static class EntityAuditQueries
{
/// <summary>
/// Filters entities created before the specified timestamp.
/// </summary>
public static IQueryable<IAudit> ThatWasCreateBefore(this IQueryable<IAudit> query, DateTime dateTime) => query.Where(x => x.CreatedOn < dateTime);
/// <summary>
/// Filters entities created after the specified timestamp.
/// </summary>
public static IQueryable<IAudit> ThatWasCreatedAfter(this IQueryable<IAudit> query, DateTime dateTime) => query.Where(x => x.CreatedOn > dateTime);
/// <summary>
/// Filters entities created between the specified start and end timestamps.
/// </summary>
public static IQueryable<IAudit> ThatWasCreatedBetween(this IQueryable<IAudit> query, DateTime start, DateTime end) => query.ThatWasCreatedAfter(start).ThatWasCreateBefore(end);
/// <summary>
/// Filters entities last modified before the specified timestamp.
/// </summary>
public static IQueryable<IAudit> ThatWasLastModifiedBefore(this IQueryable<IAudit> query, DateTime dateTime) => query.Where(x => x.LastModifiedOn < dateTime);
/// <summary>
/// Filters entities last modified after the specified timestamp.
/// </summary>
public static IQueryable<IAudit> ThatWasLastModifiedAfter(this IQueryable<IAudit> query, DateTime dateTime) => query.Where(x => x.LastModifiedOn > dateTime);
/// <summary>
/// Filters entities last modified between the specified start and end timestamps.
/// </summary>
public static IQueryable<IAudit> ThatWasLastModifiedBetween(this IQueryable<IAudit> query, DateTime start, DateTime end) => query.ThatWasLastModifiedAfter(start).ThatWasLastModifiedBefore(end);
}

View File

@@ -0,0 +1,24 @@
using Prefab.Domain.Common;
namespace Prefab.Data.Queries;
/// <summary>
/// Provides query helpers for filtering entities by audit status.
/// </summary>
public static class EntityStatusQueries
{
/// <summary>
/// Filters entities with an <see cref="AuditStatus.Active"/> status.
/// </summary>
public static IQueryable<IStatus> WithAnActiveStatus(this IQueryable<IStatus> query) => query.Where(x => x.AuditStatus == AuditStatus.Active);
/// <summary>
/// Filters entities with an <see cref="AuditStatus.Inactive"/> status.
/// </summary>
public static IQueryable<IStatus> WithAnInactiveStatus(this IQueryable<IStatus> query) => query.Where(x => x.AuditStatus == AuditStatus.Inactive);
/// <summary>
/// Filters entities with an <see cref="AuditStatus.Deleted"/> status.
/// </summary>
public static IQueryable<IStatus> WithADeletedStatus(this IQueryable<IStatus> query) => query.Where(x => x.AuditStatus == AuditStatus.Deleted);
}

View 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();
}

View File

@@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore;
using Prefab.Domain.Common;
namespace Prefab.Data.Queries;
/// <summary>
/// Provides shared filters for working with generic attribute views.
/// </summary>
public static class GenericAttributeViewQueries
{
/// <summary>
/// Filters a module-specific view by entity identifier and key group.
/// </summary>
public static IQueryable<TView> ForEntity<TView>(this IQueryable<TView> query, IEntity<Guid> entity, string keyGroup)
where TView : class
{
if (entity == null)
{
throw new ArgumentNullException(nameof(entity));
}
if (string.IsNullOrEmpty(keyGroup))
{
throw new ArgumentException("KeyGroup cannot be null or empty.", nameof(keyGroup));
}
return query.Where(x => EF.Property<Guid>(x, "EntityId") == entity.Id &&
EF.Property<string>(x, "KeyGroup") == keyGroup);
}
}