39 lines
1.7 KiB
C#
39 lines
1.7 KiB
C#
using System.Linq.Expressions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace General_Ledger_Service.Repositories
|
|
{
|
|
// Shared boilerplate for the GetByIdAsync/ExistsAsync/AddAsync one-liners duplicated across
|
|
// most repositories (01_Full_Development_Plan.md Section 10.4) - same shape as
|
|
// RecursiveTreeQueries.cs: a parameterized static helper, not a generic repository base class
|
|
// (CLAUDE.md Rule 2). Each repository keeps its own public interface/method names and calls
|
|
// this internally only where its own method body is literally this one-line shape.
|
|
internal static class RepositoryHelpers
|
|
{
|
|
public static async Task<T?> GetByIdAsync<T>(DbSet<T> set, params object[] keyValues) where T : class =>
|
|
await set.FindAsync(keyValues);
|
|
|
|
public static async Task<bool> ExistsAsync<T>(DbSet<T> set, Expression<Func<T, bool>> predicate) where T : class =>
|
|
await set.AnyAsync(predicate);
|
|
|
|
public static async Task<T> AddAsync<T>(DbSet<T> set, T entity) where T : class
|
|
{
|
|
await set.AddAsync(entity);
|
|
return entity;
|
|
}
|
|
|
|
// Paging is only applied when the caller explicitly supplies both page and pageSize -
|
|
// no invented defaults (e.g. page=1/pageSize=20) when either is omitted, so a caller that
|
|
// doesn't need paging at a given moment gets the full matching result set back untouched.
|
|
public static IQueryable<T> ApplyPaging<T>(IQueryable<T> query, int? page, int? pageSize)
|
|
{
|
|
if (page.HasValue && pageSize.HasValue)
|
|
{
|
|
query = query.Skip((page.Value - 1) * pageSize.Value).Take(pageSize.Value);
|
|
}
|
|
|
|
return query;
|
|
}
|
|
}
|
|
}
|