| | 1 | | using Domain.Entities; |
| | 2 | | using Microsoft.EntityFrameworkCore; |
| | 3 | | using Microsoft.EntityFrameworkCore.Query.SqlExpressions; |
| | 4 | | using Repository.Abastractions; |
| | 5 | | using Repository.Persistency.Generic; |
| | 6 | | using System.Linq.Expressions; |
| | 7 | |
|
| | 8 | | namespace Repository.Persistency.Implementations; |
| | 9 | | public class DespesaRepositorioImpl : BaseRepository<Despesa>, IRepositorio<Despesa> |
| | 10 | | { |
| 12 | 11 | | public RegisterContext Context { get; } |
| 8 | 12 | | public DespesaRepositorioImpl(RegisterContext context) : base(context) |
| 8 | 13 | | { |
| 8 | 14 | | Context = context; |
| 8 | 15 | | } |
| | 16 | |
|
| | 17 | | public override Despesa Get(Guid id) |
| 2 | 18 | | { |
| 2 | 19 | | return Context.Despesa.Include(d => d.Categoria).Include(d => d.Usuario).FirstOrDefault(d => d.Id.Equals(id)); |
| 2 | 20 | | } |
| | 21 | |
|
| | 22 | | public override List<Despesa> GetAll() |
| 1 | 23 | | { |
| 1 | 24 | | return Context.Despesa.Include(d => d.Categoria).Include(d => d.Usuario).ToList(); |
| 1 | 25 | | } |
| | 26 | |
|
| | 27 | | public override void Insert(Despesa entity) |
| 2 | 28 | | { |
| 2 | 29 | | var categoriaId = entity.CategoriaId; |
| 1 | 30 | | entity.Categoria = Context.Set<Categoria>().First(c => c.Id.Equals(categoriaId)); |
| 1 | 31 | | Context.Add(entity); |
| 1 | 32 | | Context.SaveChanges(); |
| 1 | 33 | | } |
| | 34 | |
|
| | 35 | | public override void Update(Despesa entity) |
| 2 | 36 | | { |
| 2 | 37 | | var despesaId = entity.Id; |
| 2 | 38 | | var categoriaId = entity.CategoriaId; |
| 2 | 39 | | entity.Categoria = Context.Set<Categoria>().First(c => c.Id.Equals(categoriaId)); |
| 1 | 40 | | var existingEntity = Context.Despesa.Single(d => d.Id.Equals(despesaId)); |
| 1 | 41 | | Context?.Entry(existingEntity).CurrentValues.SetValues(entity); |
| 1 | 42 | | Context?.SaveChanges(); |
| 1 | 43 | | entity = existingEntity; |
| 1 | 44 | | } |
| | 45 | |
|
| | 46 | | public override IEnumerable<Despesa> Find(Expression<Func<Despesa, bool>> expression) |
| 1 | 47 | | { |
| 1 | 48 | | return Context.Despesa |
| 1 | 49 | | .Include(d => d.Categoria) |
| 1 | 50 | | .ThenInclude(c => c.TipoCategoria) |
| 1 | 51 | | .Where(expression); |
| 1 | 52 | | } |
| | 53 | | } |