| | 1 | | using Domain.Core.Aggreggates; |
| | 2 | | using Microsoft.EntityFrameworkCore; |
| | 3 | | using Repository; |
| | 4 | | using System.Linq.Expressions; |
| | 5 | |
|
| | 6 | | namespace Despesas.Repository.UnitOfWork.Abstractions; |
| | 7 | |
|
| | 8 | | public sealed class BaseUnitOfWork<T> : IUnitOfWorkRepository<T> where T : BaseDomain |
| | 9 | | { |
| | 10 | | protected readonly RegisterContext Context; |
| | 11 | |
|
| 8 | 12 | | public BaseUnitOfWork(RegisterContext context) |
| 8 | 13 | | { |
| 8 | 14 | | Context = context; |
| 8 | 15 | | } |
| | 16 | |
|
| | 17 | | public async Task<T?> Get(Guid entityId) |
| 2 | 18 | | { |
| 2 | 19 | | return await Context.Set<T>().FindAsync(entityId); |
| 2 | 20 | | } |
| | 21 | |
|
| | 22 | | public async Task<IEnumerable<T>> GetAll() |
| 1 | 23 | | { |
| 1 | 24 | | return await Context.Set<T>().ToListAsync(); |
| 1 | 25 | | } |
| | 26 | |
|
| | 27 | | public async Task Insert(T entity) |
| 2 | 28 | | { |
| 2 | 29 | | await Context.Set<T>().AddAsync(entity); |
| 2 | 30 | | } |
| | 31 | |
|
| | 32 | | public async Task Update(T entity) |
| 1 | 33 | | { |
| 1 | 34 | | var existingEntity = await Context.Set<T>().FirstOrDefaultAsync(c => c.Id.Equals(entity.Id)); |
| 1 | 35 | | Context?.Entry(existingEntity).CurrentValues.SetValues(entity); |
| 1 | 36 | | } |
| | 37 | |
|
| | 38 | | public async Task Delete(Guid entityId) |
| 1 | 39 | | { |
| 1 | 40 | | var entity = await Get(entityId); |
| 1 | 41 | | if (entity != null) |
| 1 | 42 | | Context.Set<T>().Remove(entity); |
| 1 | 43 | | } |
| | 44 | |
|
| | 45 | | public async Task<IEnumerable<T>> Find(Expression<Func<T, bool>> expression) |
| 1 | 46 | | { |
| 1 | 47 | | return await Context.Set<T>().Where(expression).ToListAsync(); |
| 1 | 48 | | } |
| | 49 | | } |