72 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C#
		
	
	
| using Brizco.Repository.Models;
 | |
| using Brizco.Repository.Repositories.UnitOfWork;
 | |
| using Microsoft.EntityFrameworkCore.ChangeTracking;
 | |
| using Microsoft.EntityFrameworkCore.Storage;
 | |
| 
 | |
| namespace Brizco.Repository.Repositories.Base;
 | |
| public class RepositoryWrapper : IRepositoryWrapper
 | |
| {
 | |
|     private readonly ApplicationContext _context;
 | |
|     private IDbContextTransaction? _currentTransaction;
 | |
|     public RepositoryWrapper(ApplicationContext context)
 | |
|     {
 | |
|         _context = context;
 | |
|     }
 | |
| 
 | |
|     public IBaseRepository<T> SetRepository<T>() where T : ApiEntity => new BaseRepository<T>(_context);
 | |
| 
 | |
|     public async Task RollBackAsync(CancellationToken cancellationToken)
 | |
|     {
 | |
|         if (_currentTransaction == null)
 | |
|             throw new ArgumentNullException(nameof(_currentTransaction));
 | |
|         await _currentTransaction.RollbackAsync(cancellationToken);
 | |
|     }
 | |
|     public async Task CommitAsync(CancellationToken cancellationToken)
 | |
|     {
 | |
|         if (_currentTransaction == null)
 | |
|             throw new ArgumentNullException(nameof(_currentTransaction));
 | |
|         await _currentTransaction.CommitAsync(cancellationToken);
 | |
|     }
 | |
|     public async Task BeginTransaction()
 | |
|     {
 | |
|         _currentTransaction = await _context.Database.BeginTransactionAsync();
 | |
|     }
 | |
|     public async Task SaveChangesAsync(CancellationToken cancellationToken = default)
 | |
|     {
 | |
|         SetAuditables();
 | |
|         await _context.SaveChangesAsync(cancellationToken);
 | |
|     }
 | |
| 
 | |
|     private void SetAuditables()
 | |
|     {
 | |
|         IEnumerable<EntityEntry<IApiEntity>> entries = _context.ChangeTracker.Entries<IApiEntity>();
 | |
|         foreach (EntityEntry<IApiEntity> entity in entries)
 | |
|         {
 | |
|             if (entity.State == EntityState.Added)
 | |
|             {
 | |
|                 entity.Property(e => e.CreatedAt)
 | |
|                     .CurrentValue = DateTime.Now;
 | |
|             }
 | |
| 
 | |
|             if (entity.State == EntityState.Modified)
 | |
|             {
 | |
|                 entity.Property(e => e.ModifiedAt)
 | |
|                     .CurrentValue = DateTime.Now;
 | |
|             }
 | |
| 
 | |
|             if (entity.State == EntityState.Deleted)
 | |
|             {
 | |
|                 entity.Property(e => e.RemovedAt)
 | |
|                     .CurrentValue = DateTime.Now;
 | |
|                 entity.Property(e => e.IsRemoved)
 | |
|                     .CurrentValue = true;
 | |
|             }
 | |
|         }
 | |
|     }
 | |
|     public void Dispose()
 | |
|     {
 | |
|         _currentTransaction?.Dispose();
 | |
|         _context?.Dispose();
 | |
|     }
 | |
| }
 |