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