using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; using MongoDB.Driver; using TelegramSaaS.Application.Common.Interfaces.Persistence; using TelegramSaaS.Domain.Common; namespace TelegramSaaS.Infrastructure.Persistence.Repositories; public class MongoRepository : IRepository where T : BaseEntity { protected readonly IMongoCollection Collection; public MongoRepository(MongoDbContext context, string collectionName) { Collection = context.GetCollection(collectionName); } public async Task GetByIdAsync(string id) { return await Collection.Find(x => x.Id == id).FirstOrDefaultAsync(); } public async Task> GetAllAsync() { return await Collection.Find(_ => true).ToListAsync(); } public async Task> FindAsync(Expression> predicate) { return await Collection.Find(predicate).ToListAsync(); } public async Task AddAsync(T entity) { entity.CreatedAt = DateTime.UtcNow; await Collection.InsertOneAsync(entity); } public async Task UpdateAsync(T entity) { entity.UpdatedAt = DateTime.UtcNow; await Collection.ReplaceOneAsync(x => x.Id == entity.Id, entity); } public async Task DeleteAsync(string id) { await Collection.DeleteOneAsync(x => x.Id == id); } }