This commit is contained in:
2025-10-01 23:59:31 +03:00
parent 2a29571dbf
commit b6c4b9b6bb
28 changed files with 689 additions and 383 deletions

View File

@@ -1,47 +0,0 @@
using LctMonolith.Models.Database;
using Microsoft.EntityFrameworkCore;
using Serilog;
namespace LctMonolith.Database.Data;
/// <summary>
/// Development database seeder for initial ranks, competencies, sample store items.
/// Idempotent: checks existence before inserting.
/// </summary>
public static class DbSeeder
{
public static async Task SeedAsync(AppDbContext db, CancellationToken ct = default)
{
// await db.Database.EnsureCreatedAsync(ct);
//
// if (!await db.Ranks.AnyAsync(ct))
// {
// var ranks = new List<Rank>
// {
// new() { Title = "Искатель", ExpNeeded = 0 },
// new() { Title = "Пилот-кандидат", ExpNeeded = 500 },
// new() { Title = "Принятый в экипаж", ExpNeeded = 1500 }
// };
// db.Ranks.AddRange(ranks);
// Log.Information("Seeded {Count} ranks", ranks.Count);
// }
//
// if (!await db.Skills.AnyAsync(ct))
// {
// var comps = new[]
// {
// "Вера в дело","Стремление к большему","Общение","Аналитика","Командование","Юриспруденция","Трёхмерное мышление","Базовая экономика","Основы аэронавигации"
// }.Select(n => new Skill { Title = n });
// db.Skills.AddRange(comps);
// Log.Information("Seeded competencies");
// }
//
// if (!await db.StoreItems.AnyAsync(ct))
// {
// db.StoreItems.AddRange(new StoreItem { Name = "Футболка Алабуга", Price = 100 }, new StoreItem { Name = "Брелок Буран", Price = 50 });
// Log.Information("Seeded store items");
// }
//
// await db.SaveChangesAsync(ct);
}
}

View File

@@ -4,9 +4,6 @@ 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;
@@ -24,33 +21,61 @@ public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEnt
params Expression<Func<TEntity, object>>[] includes)
{
IQueryable<TEntity> query = Set;
if (filter != null) query = query.Where(filter);
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);
}
if (orderBy != null) query = orderBy(query);
return query;
}
public async Task<TEntity?> GetByIdAsync(object id) => await Set.FindAsync(id) ?? null;
public async Task<TEntity?> GetByIdAsync(object id)
{
return await Set.FindAsync(id);
}
public ValueTask<TEntity?> FindAsync(params object[] keyValues) => Set.FindAsync(keyValues);
public ValueTask<TEntity?> FindAsync(params object[] keyValues)
{
return Set.FindAsync(keyValues);
}
public async Task AddAsync(TEntity entity, CancellationToken ct = default) => await Set.AddAsync(entity, ct);
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 async Task AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken ct = default)
{
await Set.AddRangeAsync(entities, ct);
}
public void Update(TEntity entity) => Set.Update(entity);
public void Update(TEntity entity)
{
Set.Update(entity);
}
public void Remove(TEntity entity) => Set.Remove(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");
if (entity == null)
{
throw new KeyNotFoundException($"Entity {typeof(TEntity).Name} id={id} not found");
}
Set.Remove(entity);
}
}

View File

@@ -2,9 +2,6 @@ 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(
@@ -22,4 +19,3 @@ public interface IGenericRepository<TEntity> where TEntity : class
void Remove(TEntity entity);
Task RemoveByIdAsync(object id, CancellationToken ct = default);
}