using System.Linq.Expressions; using Marten; namespace NetinaShop.Repository.Repositories.Entity; public class MartenRepository : IMartenRepository { private readonly IDocumentStore _documentStore; public MartenRepository(IDocumentStore documentStore) { _documentStore = documentStore; } public async Task> GetEntitiesAsync(CancellationToken cancellation) where TSetting : notnull { await using var session = _documentStore.QuerySession(); var entities = await session.Query().ToListAsync(cancellation); return entities.ToList(); } public async Task> GetEntitiesAsync(Expression> expression, CancellationToken cancellation) where TSetting : notnull { await using var session = _documentStore.QuerySession(); var entities = await session.Query().Where(expression).ToListAsync(cancellation); return entities.ToList(); } public async Task GetEntityAsync(Guid id,CancellationToken cancellation) where TSetting : notnull { await using var session = _documentStore.QuerySession(); var setting = await session.LoadAsync(id,cancellation); if (setting == null) throw new AppException($"{nameof(setting)} not found", ApiResultStatusCode.NotFound); return setting; } public async Task GetEntityAsync(Expression> expression, CancellationToken cancellation) where TSetting : notnull { await using var session = _documentStore.QuerySession(); var entity = await session.Query().FirstOrDefaultAsync(expression,cancellation); if (entity == null) throw new AppException($"{nameof(entity)} not found", ApiResultStatusCode.NotFound); return entity; } public async Task AddOrUpdateEntityAsync(TSetting setting, CancellationToken cancellation) where TSetting : notnull { if (setting == null) throw new AppException($"{nameof(setting)} is null", ApiResultStatusCode.BadRequest); await using var session = _documentStore.LightweightSession(); session.Store(setting); await session.SaveChangesAsync(cancellation); } public Task RemoveEntityAsync(CancellationToken cancellation) { throw new NotImplementedException(); } }