|
| 1 | +using CodeChavez.Common.Extensions; |
| 2 | +using CodeChavez.Repository.Pattern.Extensions; |
| 3 | +using CodeChavez.Repository.Pattern.Interfaces; |
| 4 | +using MediatR; |
| 5 | +using Microsoft.EntityFrameworkCore; |
| 6 | +using MongoDB.Bson; |
| 7 | +using System.Linq.Expressions; |
| 8 | + |
| 9 | +namespace CodeChavez.Repository.Pattern.MongoDB; |
| 10 | + |
| 11 | +public class MongoRepository<TEntity> : INoSQLRepository<TEntity> where TEntity : DomainDocument |
| 12 | +{ |
| 13 | + private readonly DbContext _dbContext; |
| 14 | + private readonly IMediator _mediator; |
| 15 | + private readonly IQueryableCacheService _cache; |
| 16 | + |
| 17 | + public MongoRepository( |
| 18 | + DbContext dbContext, |
| 19 | + IQueryableCacheService cache, |
| 20 | + IMediator mediator) |
| 21 | + { |
| 22 | + _dbContext = dbContext; |
| 23 | + _mediator = mediator; |
| 24 | + _cache = cache; |
| 25 | + } |
| 26 | + |
| 27 | + public async Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken = default) |
| 28 | + { |
| 29 | + _ = await _dbContext.Set<TEntity>().AddAsync(entity, cancellationToken); |
| 30 | + _dbContext.Database.AutoTransactionBehavior = AutoTransactionBehavior.Never; |
| 31 | + await _dbContext.SaveChangesAsync(); |
| 32 | + |
| 33 | + if (_mediator.IsNotNull()) |
| 34 | + await _mediator.DispatchDomainDocumentEventsAsync(entity); |
| 35 | + |
| 36 | + return entity; |
| 37 | + } |
| 38 | + |
| 39 | + public Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) |
| 40 | + { |
| 41 | + throw new NotImplementedException(); |
| 42 | + } |
| 43 | + |
| 44 | + public Task DeleteAsync<TKey>(TKey id, CancellationToken cancellationToken = default) |
| 45 | + { |
| 46 | + throw new NotImplementedException(); |
| 47 | + } |
| 48 | + |
| 49 | + public async Task<IEnumerable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate, |
| 50 | + bool readOnly = true, bool cache = true, |
| 51 | + CancellationToken cancellationToken = default) |
| 52 | + { |
| 53 | + if (readOnly && cache) |
| 54 | + return await _dbContext.Set<TEntity>() |
| 55 | + .AsNoTracking() |
| 56 | + .Where(predicate) |
| 57 | + .FromCacheAsync(_cache, cancellationToken); |
| 58 | + else if (readOnly && cache == false) |
| 59 | + return await _dbContext.Set<TEntity>() |
| 60 | + .AsNoTracking() |
| 61 | + .Where(predicate) |
| 62 | + .ToListAsync(cancellationToken); |
| 63 | + else |
| 64 | + return await _dbContext.Set<TEntity>() |
| 65 | + .Where(predicate) |
| 66 | + .FromCacheAsync(_cache, cancellationToken); |
| 67 | + } |
| 68 | + |
| 69 | + public async Task<TEntity?> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate, bool readOnly = true, bool cache = true, CancellationToken cancellationToken = default) |
| 70 | + { |
| 71 | + if (readOnly && cache) |
| 72 | + return await _dbContext.Set<TEntity>().AsNoTracking() |
| 73 | + .FromCacheSingleOrDefaultAsync(predicate, _cache, cancellationToken); |
| 74 | + else if (readOnly && cache == false) |
| 75 | + return await _dbContext.Set<TEntity>().AsNoTracking() |
| 76 | + .FirstOrDefaultAsync(predicate, cancellationToken: cancellationToken); |
| 77 | + else |
| 78 | + return await _dbContext.Set<TEntity>() |
| 79 | + .FirstOrDefaultAsync(predicate, cancellationToken: cancellationToken); |
| 80 | + } |
| 81 | + |
| 82 | + public async Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default) |
| 83 | + { |
| 84 | + var existing = await _dbContext.Set<TEntity>().FindAsync(entity.Id); |
| 85 | + |
| 86 | + if (existing == null) |
| 87 | + throw new KeyNotFoundException("Not found"); |
| 88 | + |
| 89 | + if (existing != null) |
| 90 | + _dbContext.Set<TEntity>().Entry(existing).State = EntityState.Detached; |
| 91 | + |
| 92 | + _dbContext.Set<TEntity>().Attach(entity); |
| 93 | + |
| 94 | + _dbContext.Update(entity); |
| 95 | + await _dbContext.SaveChangesAsync(); |
| 96 | + |
| 97 | + if (_mediator.IsNotNull()) |
| 98 | + await _mediator.DispatchDomainDocumentEventsAsync(entity); |
| 99 | + |
| 100 | + return entity; |
| 101 | + } |
| 102 | +} |
0 commit comments