Files
Final_Das/Modelo/RepositorioFactura.cs

59 lines
1.7 KiB
C#

using System.Collections.ObjectModel;
using Entidades;
using Microsoft.EntityFrameworkCore;
namespace Modelo
{
public sealed class RepositorioFactura : Repositorio<Factura>
{
public RepositorioFactura(Context context)
{
this.context = context;
}
public override List<Factura> Listar()
{
return context.Facturas
.AsNoTracking()
.Include(x=>x.Detalles)
.Include(x=>x.Cliente)
.ToList();
}
public override void Add(Factura t)
{
t.Cliente = context.Clientes.FirstOrDefault(x => x.Cuit == t.Cliente.Cuit);
foreach (var detalle in t.Detalles)
{
detalle.Producto = (detalle.Producto.EsPerecedero) ?
context.ProductoPercederos.FirstOrDefault(x => x.Id == detalle.Producto.Id) :
context.ProductoNoPercederos.FirstOrDefault(x => x.Id == detalle.Producto.Id) ;
}
context.Facturas.Add(t);
}
public override void Del(Factura t)
{
Factura fac = context.Facturas.First(x => x.Id == t.Id);
if (fac == null) return;
context.Facturas.Remove(fac);
}
public override void Mod(Factura t)
{
context.Facturas.Update(t);
}
public Factura ObtenerPorId(Factura fac)
{
var factura = context.Facturas
.Include(x=>x.Detalles)
.ThenInclude(x=>x.Producto)
.FirstOrDefault(x => x.Id == fac.Id);
return factura;
}
}
}