From 062c1e1693ef5dcef50c9f2f71065a811a9f5710 Mon Sep 17 00:00:00 2001 From: fede Date: Wed, 14 Aug 2024 02:27:25 -0300 Subject: [PATCH] =?UTF-8?q?empez=C3=A9=20a=20hacer=20cosas=20con=20el=20db?= =?UTF-8?q?context,=20termine=20la=20clase=20de=20repositorio=20y=20a?= =?UTF-8?q?=C3=B1adi/implemente=20los=20skeleton=20de=20varios=20repositor?= =?UTF-8?q?ios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Modelo/Context.cs | 76 ++++++++++++++++++++++++++- Modelo/IRepositorio.cs | 16 ------ Modelo/Repositorio.cs | 30 +++++++++++ Modelo/RepositorioCategoria.cs | 32 ++++++------ Modelo/RepositorioClientes.cs | 37 +++++++++++++- Modelo/RepositorioFactura.cs | 36 ++++++++++++- Modelo/RepositorioLote.cs | 34 ++++++++++++- Modelo/RepositorioOrdenDeCompra.cs | 34 ++++++++++++- Modelo/RepositorioPresupuesto.cs | 37 +++++++++++++- Modelo/RepositorioProductos.cs | 82 +++++++++++++++++++++++++++++- Modelo/RepositorioProveedor.cs | 35 ++++++++++++- Modelo/RepositorioRemito.cs | 33 ++++++++++-- 12 files changed, 435 insertions(+), 47 deletions(-) delete mode 100644 Modelo/IRepositorio.cs create mode 100644 Modelo/Repositorio.cs diff --git a/Modelo/Context.cs b/Modelo/Context.cs index 95e35d1..1b69ab1 100644 --- a/Modelo/Context.cs +++ b/Modelo/Context.cs @@ -7,11 +7,85 @@ public class Context : Microsoft.EntityFrameworkCore.DbContext { public DbSet Categorias { get; set; } public DbSet Clientes { get; set; } - + public DbSet Facturas { get; set; } + public DbSet Lotes { get; set; } + public DbSet DetalleFacturas { get; set; } + public DbSet DetallePresupuestos { get; set; } + public DbSet DetalleOrdenDeCompras { get; set; } + public DbSet OrdenDeCompras { get; set; } + public DbSet Presupuestos { get; set; } + public DbSet ProductoPercederos { get; set; } + public DbSet ProductoNoPercederos { get; set; } + public DbSet Proveedores { get; set; } + public DbSet 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=A?5?~*=Gux'}JT0me,z5Cf^f(s'-gy[G#-jt%b&uHe+/,$Gl>qkXl-c[@&e(\.V?[3)\w|aMH+^67x;Trusted_Connection=False;Encrypt=False;Connection Timeout=5;"); } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + /// Categoria + modelBuilder.Entity() + .ToTable("Categoria") + .HasKey(x => x.Id); + modelBuilder.Entity() + .Property(c=>c.Descripcion) + .IsRequired() + .HasMaxLength(100); + + /// Cliente + modelBuilder.Entity() + .ToTable("Clientes") // Nombre de la tabla + .HasKey(c => c.Cuit); // Configura la clave primaria + + modelBuilder.Entity() + .Property(c => c.Nombre) + .IsRequired() + .HasMaxLength(100); + + modelBuilder.Entity() + .Property(c => c.Apellido) + .IsRequired() + .HasMaxLength(100); + + modelBuilder.Entity() + .Property(c => c.Direccion) + .HasMaxLength(200); + + modelBuilder.Entity() + .Property(c => c.Correo) + .HasMaxLength(150); + + modelBuilder.Entity() + .Property(c => c.Habilitado) + .HasDefaultValue(true); + + /// Factura Y DetalleFactura WIP + modelBuilder.Entity() + .ToTable("Facturas") + .HasKey(f => f.Id); + + modelBuilder.Entity() + .HasMany(f => f.Detalles) + .WithOne() + .HasForeignKey(df => df.FacturaId); + + modelBuilder.Entity() + .ToTable("DetallesFacturas") + .HasKey(df => df.Id); + + modelBuilder.Entity() + .Property(df => df.IdFactura) + .IsRequired(); + + modelBuilder.Entity() + .HasOne(df => df.Producto) + .WithMany() + .HasForeignKey(df => df.Producto.Id); + } } diff --git a/Modelo/IRepositorio.cs b/Modelo/IRepositorio.cs deleted file mode 100644 index 4aef744..0000000 --- a/Modelo/IRepositorio.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.ObjectModel; -using System.Diagnostics.Contracts; - -namespace Modelo -{ - public abstract class IRepositorio - { - public IEnumerable 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(); - } -} \ No newline at end of file diff --git a/Modelo/Repositorio.cs b/Modelo/Repositorio.cs new file mode 100644 index 0000000..30401f0 --- /dev/null +++ b/Modelo/Repositorio.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; + +namespace Modelo +{ + public abstract class Repositorio + { + protected Context context; + public abstract IEnumerable 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; + } + } +} \ No newline at end of file diff --git a/Modelo/RepositorioCategoria.cs b/Modelo/RepositorioCategoria.cs index 382e01b..e8f6a7b 100644 --- a/Modelo/RepositorioCategoria.cs +++ b/Modelo/RepositorioCategoria.cs @@ -1,20 +1,27 @@ -using System.Transactions; +using System.Reflection.Metadata.Ecma335; +using System.Transactions; using Entidades; using Microsoft.EntityFrameworkCore; namespace Modelo { - public sealed class RepositorioCategoria : IRepositorio + public sealed class RepositorioCategoria : Repositorio { - private Context context; + public RepositorioCategoria(Context context) { this.context = context; } + public override IEnumerable Listar() + { + return context.Categorias.ToList(); + } + public override Categoria ObtenerPorId(int Tid) { - return context.Categorias.Find(Tid); + Categoria cat = context.Categorias.Find(Tid); + return cat ?? new Categoria(); } public override void Add(Categoria t) @@ -24,25 +31,16 @@ namespace Modelo public override 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) { - context.Entry(t).State = EntityState.Modified; + context.Categorias.Update(t); } - public override void Guardar() - { - try - { - context.SaveChanges(); - } - catch (DbUpdateException) - { - throw; - } - } + } } diff --git a/Modelo/RepositorioClientes.cs b/Modelo/RepositorioClientes.cs index 69bb87f..cf5a9ae 100644 --- a/Modelo/RepositorioClientes.cs +++ b/Modelo/RepositorioClientes.cs @@ -2,9 +2,42 @@ namespace Modelo { - public sealed class RepositorioClientes : IRepositorio + public sealed class RepositorioClientes : Repositorio { - + public RepositorioClientes(Context context) + { + this.context = context; + } + + public override IEnumerable Listar() + { + return context.Clientes.ToList(); + } + + public override Cliente ObtenerPorId(int Tid) + { + Cliente cli = context.Clientes.Find(Tid); + return cli ?? new Cliente(); + } + + public override void Add(Cliente t) + { + context.Clientes.Add(t); + } + + public override void Del(Cliente t) + { + Cliente cli = context.Clientes.Find(t.Cuit); + if (cli == null) return; + context.Clientes.Remove(cli); + } + + public override void Mod(Cliente t) + { + context.Categorias.Update(t); + } + + } } diff --git a/Modelo/RepositorioFactura.cs b/Modelo/RepositorioFactura.cs index bb25982..2af65ca 100644 --- a/Modelo/RepositorioFactura.cs +++ b/Modelo/RepositorioFactura.cs @@ -3,8 +3,40 @@ using Entidades; namespace Modelo { - public sealed class RepositorioFactura : IRepositorio + public sealed class RepositorioFactura : Repositorio { - + + public RepositorioFactura(Context context) + { + this.context = context; + } + + public override IEnumerable Listar() + { + return context.Facturas.ToList(); + } + + public override Factura ObtenerPorId(int Tid) + { + Factura fac = context.Facturas.Find(Tid); + return fac ?? new Factura(); + } + + public override void Add(Factura t) + { + throw new NotImplementedException(); + } + + public override void Del(Factura t) + { + throw new NotImplementedException(); + } + + public override void Mod(Factura t) + { + throw new NotImplementedException(); + } + + } } diff --git a/Modelo/RepositorioLote.cs b/Modelo/RepositorioLote.cs index d6704ea..5317ff1 100644 --- a/Modelo/RepositorioLote.cs +++ b/Modelo/RepositorioLote.cs @@ -2,8 +2,40 @@ namespace Modelo { - public sealed class RepositorioLote : IRepositorio + public sealed class RepositorioLote : Repositorio { + private Context context; + + public RepositorioLote(Context context) + { + this.context = context; + } + + public override IEnumerable Listar() + { + throw new NotImplementedException(); + } + + public override Lote ObtenerPorId(int Tid) + { + throw new NotImplementedException(); + } + + public override void Add(Lote t) + { + throw new NotImplementedException(); + } + + public override void Del(Lote t) + { + throw new NotImplementedException(); + } + + public override void Mod(Lote t) + { + throw new NotImplementedException(); + } + } } diff --git a/Modelo/RepositorioOrdenDeCompra.cs b/Modelo/RepositorioOrdenDeCompra.cs index 9f9f6c2..c7bee43 100644 --- a/Modelo/RepositorioOrdenDeCompra.cs +++ b/Modelo/RepositorioOrdenDeCompra.cs @@ -3,8 +3,40 @@ using Entidades; namespace Modelo { - public sealed class RepositorioOrdenDeCompra : IRepositorio + public sealed class RepositorioOrdenDeCompra : Repositorio { + private Context context; + + public RepositorioOrdenDeCompra(Context context) + { + this.context = context; + } + + public override IEnumerable Listar() + { + throw new NotImplementedException(); + } + + public override OrdenDeCompra ObtenerPorId(int Tid) + { + throw new NotImplementedException(); + } + + public override void Add(OrdenDeCompra t) + { + throw new NotImplementedException(); + } + + public override void Del(OrdenDeCompra t) + { + throw new NotImplementedException(); + } + + public override void Mod(OrdenDeCompra t) + { + throw new NotImplementedException(); + } + } } diff --git a/Modelo/RepositorioPresupuesto.cs b/Modelo/RepositorioPresupuesto.cs index 9ccdbab..99dce9e 100644 --- a/Modelo/RepositorioPresupuesto.cs +++ b/Modelo/RepositorioPresupuesto.cs @@ -3,8 +3,43 @@ using Entidades; namespace Modelo { - public sealed class RepositorioPresupuesto : IRepositorio + public sealed class RepositorioPresupuesto : Repositorio { + private Context context; + public RepositorioPresupuesto(Context context) + { + this.context = context; + } + + public override IEnumerable Listar() + { + throw new NotImplementedException(); + } + + public override Presupuesto ObtenerPorId(int Tid) + { + throw new NotImplementedException(); + } + + public override void Add(Presupuesto t) + { + throw new NotImplementedException(); + } + + public override void Del(Presupuesto t) + { + throw new NotImplementedException(); + } + + public override void Mod(Presupuesto t) + { + throw new NotImplementedException(); + } + + public bool Guardar() + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/Modelo/RepositorioProductos.cs b/Modelo/RepositorioProductos.cs index 95de249..ebe1b5e 100644 --- a/Modelo/RepositorioProductos.cs +++ b/Modelo/RepositorioProductos.cs @@ -2,8 +2,88 @@ namespace Modelo { - public sealed class RepositorioProductos : IRepositorio + public sealed class RepositorioProductos : Repositorio, Repositorio { + private Context context; + public RepositorioProductos(Context context) + { + this.context = context; + } + + public override IEnumerable Listar() + { + throw new NotImplementedException(); + } + + ProductoNoPercedero Repositorio.ObtenerPorId(int Tid) + { + throw new NotImplementedException(); + } + + public override void Add(ProductoNoPercedero t) + { + throw new NotImplementedException(); + } + + public override void Del(ProductoNoPercedero t) + { + throw new NotImplementedException(); + } + + public override void Mod(ProductoNoPercedero t) + { + throw new NotImplementedException(); + } + + IEnumerable Repositorio.Listar() + { + throw new NotImplementedException(); + } + + ProductoPercedero Repositorio.ObtenerPorId(int Tid) + { + throw new NotImplementedException(); + } + + public override void Add(ProductoPercedero t) + { + throw new NotImplementedException(); + } + + public override void Del(ProductoPercedero t) + { + throw new NotImplementedException(); + } + + public override void Mod(ProductoPercedero t) + { + throw new NotImplementedException(); + } + + IEnumerable Repositorio.Listar() + { + throw new NotImplementedException(); + } + + public override 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(); + } } } diff --git a/Modelo/RepositorioProveedor.cs b/Modelo/RepositorioProveedor.cs index d227075..cb185b8 100644 --- a/Modelo/RepositorioProveedor.cs +++ b/Modelo/RepositorioProveedor.cs @@ -2,8 +2,39 @@ namespace Modelo { - public sealed class RepositorioProveedor : IRepositorio + public sealed class RepositorioProveedor : Repositorio { - + + public RepositorioProveedor(Context context) + { + this.context = context; + } + + public override IEnumerable Listar() + { + throw new NotImplementedException(); + } + + public override Proveedor ObtenerPorId(int Tid) + { + throw new NotImplementedException(); + } + + public override void Add(Proveedor t) + { + throw new NotImplementedException(); + } + + public override void Del(Proveedor t) + { + throw new NotImplementedException(); + } + + public override void Mod(Proveedor t) + { + throw new NotImplementedException(); + } + + } } diff --git a/Modelo/RepositorioRemito.cs b/Modelo/RepositorioRemito.cs index 6245a18..9a50deb 100644 --- a/Modelo/RepositorioRemito.cs +++ b/Modelo/RepositorioRemito.cs @@ -3,10 +3,37 @@ using Entidades; namespace Modelo { - public sealed class RepositorioRemito : IRepositorio + public sealed class RepositorioRemito : Repositorio { - - + public RepositorioRemito(Context context) + { + this.context = context; + } + + public override IEnumerable Listar() + { + throw new NotImplementedException(); + } + + public override Remito ObtenerPorId(int Tid) + { + throw new NotImplementedException(); + } + + public override void Add(Remito t) + { + throw new NotImplementedException(); + } + + public override void Del(Remito t) + { + throw new NotImplementedException(); + } + + public override void Mod(Remito t) + { + throw new NotImplementedException(); + } } }