feat: Fixed errors

This commit is contained in:
ereshk1gal
2025-09-30 02:16:37 +03:00
parent 8d68a719d4
commit 8c0809d620
68 changed files with 538 additions and 490 deletions

View File

@@ -0,0 +1,56 @@
using System.Linq.Expressions;
using LctMonolith.Database.Data;
using Microsoft.EntityFrameworkCore;
namespace LctMonolith.Database.Repositories;
/// <summary>
/// Generic repository implementation for common CRUD and query composition.
/// </summary>
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
protected readonly AppDbContext Context;
protected readonly DbSet<TEntity> Set;
public GenericRepository(AppDbContext context)
{
Context = context;
Set = context.Set<TEntity>();
}
public IQueryable<TEntity> Query(
Expression<Func<TEntity, bool>>? filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null,
params Expression<Func<TEntity, object>>[] includes)
{
IQueryable<TEntity> query = Set;
if (filter != null) query = query.Where(filter);
if (includes != null)
{
foreach (var include in includes)
query = query.Include(include);
}
if (orderBy != null) query = orderBy(query);
return query;
}
public async Task<TEntity?> GetByIdAsync(object id) => await Set.FindAsync(id) ?? null;
public ValueTask<TEntity?> FindAsync(params object[] keyValues) => Set.FindAsync(keyValues);
public async Task AddAsync(TEntity entity, CancellationToken ct = default) => await Set.AddAsync(entity, ct);
public async Task AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken ct = default) => await Set.AddRangeAsync(entities, ct);
public void Update(TEntity entity) => Set.Update(entity);
public void Remove(TEntity entity) => Set.Remove(entity);
public async Task RemoveByIdAsync(object id, CancellationToken ct = default)
{
var entity = await Set.FindAsync([id], ct);
if (entity == null) throw new KeyNotFoundException($"Entity {typeof(TEntity).Name} id={id} not found");
Set.Remove(entity);
}
}

View File

@@ -0,0 +1,25 @@
using System.Linq.Expressions;
namespace LctMonolith.Database.Repositories;
/// <summary>
/// Generic repository abstraction for aggregate root / entity access. Read operations return IQueryable for composition.
/// </summary>
public interface IGenericRepository<TEntity> where TEntity : class
{
IQueryable<TEntity> Query(
Expression<Func<TEntity, bool>>? filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null,
params Expression<Func<TEntity, object>>[] includes);
Task<TEntity?> GetByIdAsync(object id);
ValueTask<TEntity?> FindAsync(params object[] keyValues);
Task AddAsync(TEntity entity, CancellationToken ct = default);
Task AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken ct = default);
void Update(TEntity entity);
void Remove(TEntity entity);
Task RemoveByIdAsync(object id, CancellationToken ct = default);
}