empezé a hacer cosas con el dbcontext, termine la clase de repositorio y añadi/implemente los skeleton de varios repositorios

This commit is contained in:
2024-08-14 02:27:25 -03:00
parent 3f526d09d6
commit 505ff5004e
12 changed files with 441 additions and 49 deletions

View File

@@ -7,11 +7,85 @@ public class Context : Microsoft.EntityFrameworkCore.DbContext
{
public DbSet<Categoria> Categorias { get; set; }
public DbSet<Cliente> Clientes { get; set; }
public DbSet<Factura> Facturas { get; set; }
public DbSet<Lote> Lotes { get; set; }
public DbSet<DetalleFactura> DetalleFacturas { get; set; }
public DbSet<DetallePresupuesto> DetallePresupuestos { get; set; }
public DbSet<DetalleOrdenDeCompra> DetalleOrdenDeCompras { get; set; }
public DbSet<OrdenDeCompra> OrdenDeCompras { get; set; }
public DbSet<Presupuesto> Presupuestos { get; set; }
public DbSet<ProductoPercedero> ProductoPercederos { get; set; }
public DbSet<ProductoNoPercedero> ProductoNoPercederos { get; set; }
public DbSet<Proveedor> Proveedores { get; set; }
public DbSet<Remito> Remitos { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ // Esto de poner credenciales sueltas está mal
optionsBuilder.UseSqlServer(@"Server=fedesrv.ddns.net,1433;Database=ControlStockDAS;User Id=ProyectoDas;Password=<IL-:aQI?TK{uRw:AU>A?5?~*=Gux'}JT0me,z5Cf^f(s'-gy[G#-jt%b&uHe+/,$Gl>qkXl-c[@&e(\.V?[3)\w|aMH+<GnkIo*99MNJ}:L=j<T,^$`8J\dXw6>^67x;Trusted_Connection=False;Encrypt=False;Connection Timeout=5;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
/// Categoria
modelBuilder.Entity<Categoria>()
.ToTable("Categoria")
.HasKey(x => x.Id);
modelBuilder.Entity<Categoria>()
.Property(c=>c.Descripcion)
.IsRequired()
.HasMaxLength(100);
/// Cliente
modelBuilder.Entity<Cliente>()
.ToTable("Clientes") // Nombre de la tabla
.HasKey(c => c.Cuit); // Configura la clave primaria
modelBuilder.Entity<Cliente>()
.Property(c => c.Nombre)
.IsRequired()
.HasMaxLength(100);
modelBuilder.Entity<Cliente>()
.Property(c => c.Apellido)
.IsRequired()
.HasMaxLength(100);
modelBuilder.Entity<Cliente>()
.Property(c => c.Direccion)
.HasMaxLength(200);
modelBuilder.Entity<Cliente>()
.Property(c => c.Correo)
.HasMaxLength(150);
modelBuilder.Entity<Cliente>()
.Property(c => c.Habilitado)
.HasDefaultValue(true);
/// Factura Y DetalleFactura WIP
modelBuilder.Entity<Factura>()
.ToTable("Facturas")
.HasKey(f => f.Id);
modelBuilder.Entity<Factura>()
.HasMany(f => f.Detalles)
.WithOne()
.HasForeignKey(df => df.FacturaId);
modelBuilder.Entity<DetalleFactura>()
.ToTable("DetallesFacturas")
.HasKey(df => df.Id);
modelBuilder.Entity<DetalleFactura>()
.Property(df => df.IdFactura)
.IsRequired();
modelBuilder.Entity<DetalleFactura>()
.HasOne(df => df.Producto)
.WithMany()
.HasForeignKey(df => df.Producto.Id);
}
}

View File

@@ -1,16 +0,0 @@
using System;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
namespace Modelo
{
public abstract class IRepositorio<T>
{
public IEnumerable<T> Listar();
public abstract T ObtenerPorId(int Tid);
public abstract void Add(T t);
public abstract void Del(T t);
public abstract void Mod(T t);
public abstract bool Guardar();
}
}

32
Modelo/Repositorio.cs Normal file
View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
namespace Modelo
{
public class Repositorio<T>
{
protected Context context;
public IEnumerable<T> Listar();
public abstract T ObtenerPorId(int Tid);
public abstract void Add(T t);
public abstract void Del(T t);
public abstract void Mod(T t);
public bool Guardar()
{
bool ret = false;
try
{
context.SaveChanges();
ret = true;
}
catch (DbUpdateException)
{
throw;
}
return ret;
}
}
}

View File

@@ -1,48 +1,46 @@
using System.Transactions;
using System.Reflection.Metadata.Ecma335;
using System.Transactions;
using Entidades;
using Microsoft.EntityFrameworkCore;
namespace Modelo
{
public sealed class RepositorioCategoria : IRepositorio<Categoria>
public sealed class RepositorioCategoria : Repositorio<Categoria>
{
private Context context;
public RepositorioCategoria(Context context)
{
this.context = context;
}
public override Categoria ObtenerPorId(int Tid)
public IEnumerable<Categoria> Listar()
{
return context.Categorias.Find(Tid);
return context.Categorias.ToList();
}
public override void Add(Categoria t)
public Categoria ObtenerPorId(int Tid)
{
Categoria cat = context.Categorias.Find(Tid);
return cat ?? new Categoria();
}
public void Add(Categoria t)
{
context.Categorias.Add(t);
}
public override void Del(Categoria t)
public void Del(Categoria t)
{
Categoria? cat = context.Categorias.Find(t.Id);
Categoria cat = context.Categorias.Find(t.Id);
if (cat == null) return;
context.Categorias.Remove(cat);
}
public override void Mod(Categoria t)
public void Mod(Categoria t)
{
context.Entry(t).State = EntityState.Modified;
context.Categorias.Update(t);
}
public override void Guardar()
{
try
{
context.SaveChanges();
}
catch (DbUpdateException)
{
throw;
}
}
}
}

View File

@@ -2,9 +2,42 @@
namespace Modelo
{
public sealed class RepositorioClientes : IRepositorio<Cliente>
public sealed class RepositorioClientes : Repositorio<Cliente>
{
public RepositorioClientes(Context context)
{
this.context = context;
}
public IEnumerable<Cliente> Listar()
{
return context.Clientes.ToList();
}
public Cliente ObtenerPorId(int Tid)
{
Cliente cli = context.Clientes.Find(Tid);
return cli ?? new Cliente();
}
public void Add(Cliente t)
{
context.Clientes.Add(t);
}
public void Del(Cliente t)
{
Cliente cli = context.Clientes.Find(t.Cuit);
if (cli == null) return;
context.Clientes.Remove(cli);
}
public void Mod(Cliente t)
{
context.Categorias.Update(t);
}
}
}

View File

@@ -3,8 +3,40 @@ using Entidades;
namespace Modelo
{
public sealed class RepositorioFactura : IRepositorio<Factura>
public sealed class RepositorioFactura : Repositorio<Factura>
{
public RepositorioFactura(Context context)
{
this.context = context;
}
public IEnumerable<Factura> Listar()
{
context.Facturas.ToList();
}
public Factura ObtenerPorId(int Tid)
{
Factura fac = context.Facturas.Find(Tid);
return fac ?? new Factura();
}
public void Add(Factura t)
{
throw new NotImplementedException();
}
public void Del(Factura t)
{
throw new NotImplementedException();
}
public void Mod(Factura t)
{
throw new NotImplementedException();
}
}
}

View File

@@ -2,8 +2,40 @@
namespace Modelo
{
public sealed class RepositorioLote : IRepositorio<Lote>
public sealed class RepositorioLote : Repositorio<Lote>
{
private Context context;
public RepositorioLote(Context context)
{
this.context = context;
}
public IEnumerable<Lote> Listar()
{
throw new NotImplementedException();
}
public Lote ObtenerPorId(int Tid)
{
throw new NotImplementedException();
}
public void Add(Lote t)
{
throw new NotImplementedException();
}
public void Del(Lote t)
{
throw new NotImplementedException();
}
public void Mod(Lote t)
{
throw new NotImplementedException();
}
}
}

View File

@@ -3,8 +3,40 @@ using Entidades;
namespace Modelo
{
public sealed class RepositorioOrdenDeCompra : IRepositorio<OrdenDeCompra>
public sealed class RepositorioOrdenDeCompra : Repositorio<OrdenDeCompra>
{
private Context context;
public RepositorioOrdenDeCompra(Context context)
{
this.context = context;
}
public IEnumerable<OrdenDeCompra> Listar()
{
throw new NotImplementedException();
}
public OrdenDeCompra ObtenerPorId(int Tid)
{
throw new NotImplementedException();
}
public void Add(OrdenDeCompra t)
{
throw new NotImplementedException();
}
public void Del(OrdenDeCompra t)
{
throw new NotImplementedException();
}
public void Mod(OrdenDeCompra t)
{
throw new NotImplementedException();
}
}
}

View File

@@ -3,8 +3,43 @@ using Entidades;
namespace Modelo
{
public sealed class RepositorioPresupuesto : IRepositorio<Presupuesto>
public sealed class RepositorioPresupuesto : Repositorio<Presupuesto>
{
private Context context;
public RepositorioPresupuesto(Context context)
{
this.context = context;
}
public IEnumerable<Presupuesto> Listar()
{
throw new NotImplementedException();
}
public Presupuesto ObtenerPorId(int Tid)
{
throw new NotImplementedException();
}
public void Add(Presupuesto t)
{
throw new NotImplementedException();
}
public void Del(Presupuesto t)
{
throw new NotImplementedException();
}
public void Mod(Presupuesto t)
{
throw new NotImplementedException();
}
public bool Guardar()
{
throw new NotImplementedException();
}
}
}

View File

@@ -2,8 +2,88 @@
namespace Modelo
{
public sealed class RepositorioProductos : IRepositorio<Producto>
public sealed class RepositorioProductos : Repositorio<ProductoPercedero>, Repositorio<ProductoNoPercedero>
{
private Context context;
public RepositorioProductos(Context context)
{
this.context = context;
}
public IEnumerable<Producto> Listar()
{
throw new NotImplementedException();
}
ProductoNoPercedero Repositorio<ProductoNoPercedero>.ObtenerPorId(int Tid)
{
throw new NotImplementedException();
}
public void Add(ProductoNoPercedero t)
{
throw new NotImplementedException();
}
public void Del(ProductoNoPercedero t)
{
throw new NotImplementedException();
}
public void Mod(ProductoNoPercedero t)
{
throw new NotImplementedException();
}
IEnumerable<ProductoNoPercedero> Repositorio<ProductoNoPercedero>.Listar()
{
throw new NotImplementedException();
}
ProductoPercedero Repositorio<ProductoPercedero>.ObtenerPorId(int Tid)
{
throw new NotImplementedException();
}
public void Add(ProductoPercedero t)
{
throw new NotImplementedException();
}
public void Del(ProductoPercedero t)
{
throw new NotImplementedException();
}
public void Mod(ProductoPercedero t)
{
throw new NotImplementedException();
}
IEnumerable<ProductoPercedero> Repositorio<ProductoPercedero>.Listar()
{
throw new NotImplementedException();
}
public Producto ObtenerPorId(int Tid)
{
throw new NotImplementedException();
}
public void Add(Producto t)
{
throw new NotImplementedException();
}
public void Del(Producto t)
{
throw new NotImplementedException();
}
public void Mod(Producto t)
{
throw new NotImplementedException();
}
}
}

View File

@@ -4,6 +4,38 @@ namespace Modelo
{
public sealed class RepositorioProveedor : IRepositorio<Proveedor>
{
private Context context;
public RepositorioProveedor(Context context)
{
this.context = context;
}
public IEnumerable<Proveedor> Listar()
{
throw new NotImplementedException();
}
public Proveedor ObtenerPorId(int Tid)
{
throw new NotImplementedException();
}
public void Add(Proveedor t)
{
throw new NotImplementedException();
}
public void Del(Proveedor t)
{
throw new NotImplementedException();
}
public void Mod(Proveedor t)
{
throw new NotImplementedException();
}
}
}

View File

@@ -5,8 +5,36 @@ namespace Modelo
{
public sealed class RepositorioRemito : IRepositorio<Remito>
{
private Context context;
public RepositorioRemito(Context context)
{
this.context = context;
}
public IEnumerable<Remito> Listar()
{
throw new NotImplementedException();
}
public Remito ObtenerPorId(int Tid)
{
throw new NotImplementedException();
}
public void Add(Remito t)
{
throw new NotImplementedException();
}
public void Del(Remito t)
{
throw new NotImplementedException();
}
public void Mod(Remito t)
{
throw new NotImplementedException();
}
}
}