From 39c4674280d5d2f05374cea4259fa1cb8dede076 Mon Sep 17 00:00:00 2001 From: Leidy Carolina Gomez Cala Date: Sat, 20 Jun 2020 18:41:24 -0500 Subject: [PATCH 1/4] cambios clase 11/06/2020 --- .../Controllers/CategoriaController.cs | 5 +- .../Dominio/Modelos/Producto.cs | 11 +++- .../Persistencia/SupermarketApiContext.cs | 55 +++++++++++++------ .../Dominio/Repositorios/CategoriaRepo.cs | 5 +- .../Dominio/Repositorios/ICategoriaRepo.cs | 2 +- 5 files changed, 54 insertions(+), 24 deletions(-) diff --git a/src/Supermarket.API/Controllers/CategoriaController.cs b/src/Supermarket.API/Controllers/CategoriaController.cs index 184e821..b69e2e7 100644 --- a/src/Supermarket.API/Controllers/CategoriaController.cs +++ b/src/Supermarket.API/Controllers/CategoriaController.cs @@ -50,9 +50,10 @@ public async Task> GetAsync() // GET api/values/5 [HttpGet("{id}")] - public ActionResult FindCategoriaById(int id) + public async Task HallarCategoriaById(int id) { - return "value"; + Categoria resultado = await context.FindCategoriaById(id); + return resultado; } } diff --git a/src/Supermarket.API/Dominio/Modelos/Producto.cs b/src/Supermarket.API/Dominio/Modelos/Producto.cs index 53d6add..0cab9d5 100644 --- a/src/Supermarket.API/Dominio/Modelos/Producto.cs +++ b/src/Supermarket.API/Dominio/Modelos/Producto.cs @@ -1,22 +1,29 @@ +using System; +using System.Net.Http.Headers; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; namespace Supermarket.API.Dominio.Modelos { public class Producto { + [Key] /// /// Contiene el id del producto /// /// public int id { get; set;} + + [Required] /// /// Contiene el nombre del producto /// /// - public string Nombre {get; set;} + public string nombre {get; set;} /// /// Contiene la cantidad de producto /// /// - public int CantidadxPaquete {get; set;} + public int cantidadxPaquete {get; set;} //public EUnidadDeMedida unidadDeMedida {get; set;} /// /// Contiene el Id de la categoria a la que pertenece el producto diff --git a/src/Supermarket.API/Dominio/Persistencia/SupermarketApiContext.cs b/src/Supermarket.API/Dominio/Persistencia/SupermarketApiContext.cs index cd82b33..bfaddd3 100644 --- a/src/Supermarket.API/Dominio/Persistencia/SupermarketApiContext.cs +++ b/src/Supermarket.API/Dominio/Persistencia/SupermarketApiContext.cs @@ -8,32 +8,53 @@ public class SupermarketApiContext : DbContext ///constructor public SupermarketApiContext(DbContextOptions options) : base(options) { - PoblarBase(); + //PoblarBase(); Database.EnsureCreated(); } //Tablas "props de la clase" public DbSet categorias {get; set;} - public DbSet productos {get; set;} + public DbSet productos {get; set;} - //Seed de la base(semillas de informacion) -- BD en memoria - public void PoblarBase() + /// + /// Metodo indicado para definir la estructura de la BD + /// + protected override void OnModelCreating(ModelBuilder builder) { - this.categorias.Add( - new Categoria{ - id = 1, - nombre = "Categoria 1", - } - ); + //Fluent API + builder.Entity().ToTable("Categorias"); + builder.Entity().HasKey(categoria => categoria.id); + builder.Entity().Property(categoria => categoria.id).ValueGeneratedOnAdd(); + builder.Entity().Property(categoria => categoria.nombre).HasColumnName("NombreCompleto"); + builder.Entity() + .Property(categorias => categorias.nombre) + .IsRequired() + .HasMaxLength(30); - this.categorias.Add( - new Categoria{ - id = 2, - nombre = "Categoria 2", - } + builder.Entity().HasData( + new Categoria(){ id =1, nombre = "Categoria 1" }, + new Categoria(){ id =2, nombre = "Categoria 2" }, + new Categoria(){ id =3, nombre = "Categoria 3" } ); - //commit - this.SaveChanges(); } + //Seed de la base(semillas de informacion) -- BD en memoria + //public void PoblarBase() + //{ + // this.categorias.Add( + // new Categoria{ + // id = 1, + // nombre = "Categoria 1", + //} + //); + + //this.categorias.Add( + //new Categoria{ + // id = 2, + // nombre = "Categoria 2", + //} + //); + //commit + //this.SaveChanges(); + //} } } \ No newline at end of file diff --git a/src/Supermarket.API/Dominio/Repositorios/CategoriaRepo.cs b/src/Supermarket.API/Dominio/Repositorios/CategoriaRepo.cs index 6b4e37c..934b7dc 100644 --- a/src/Supermarket.API/Dominio/Repositorios/CategoriaRepo.cs +++ b/src/Supermarket.API/Dominio/Repositorios/CategoriaRepo.cs @@ -46,9 +46,10 @@ public async Task> GetCategoriasAsync() return lista; } - public Categoria FindCategoriaById(int id) + public async Task FindCategoriaById(int id) { - throw new System.NotImplementedException(); + Categoria resultado = await db.categorias.FindAsync(id); + return resultado; } } } \ No newline at end of file diff --git a/src/Supermarket.API/Dominio/Repositorios/ICategoriaRepo.cs b/src/Supermarket.API/Dominio/Repositorios/ICategoriaRepo.cs index d7e6562..cb6517e 100644 --- a/src/Supermarket.API/Dominio/Repositorios/ICategoriaRepo.cs +++ b/src/Supermarket.API/Dominio/Repositorios/ICategoriaRepo.cs @@ -24,6 +24,6 @@ public interface ICategoriaRepo /// /// Identificador de la categoria /// - Categoria FindCategoriaById(int id); + Task FindCategoriaById(int id); } } \ No newline at end of file From ec4c849cd5d75fac6fded54f6a1663044d5eb65c Mon Sep 17 00:00:00 2001 From: Leidy Carolina Gomez Cala Date: Sat, 20 Jun 2020 22:11:00 -0500 Subject: [PATCH 2/4] ultimo avance --- .../Controllers/CategoriaController.cs | 42 ++++++++++++------- .../Dominio/Repositorios/CategoriaRepo.cs | 34 +++++++++------ .../Dominio/Repositorios/ICategoriaRepo.cs | 28 +++++++++---- 3 files changed, 68 insertions(+), 36 deletions(-) diff --git a/src/Supermarket.API/Controllers/CategoriaController.cs b/src/Supermarket.API/Controllers/CategoriaController.cs index b69e2e7..907e317 100644 --- a/src/Supermarket.API/Controllers/CategoriaController.cs +++ b/src/Supermarket.API/Controllers/CategoriaController.cs @@ -23,20 +23,6 @@ public CategoriaController(ICategoriaRepo CategoriaContexto) { context = CategoriaContexto; } - // GET api/values - [HttpGet] - - //Secuencial - public ActionResult> Get() - { - //return new string[] { "value1", "value2" }; - /// - /// Retorna lista de categoria - /// - /// - return context.GetCategorias().ToList(); - } - //Asincrona --> Usa paralelismo en el servidor public async Task> GetAsync() { @@ -52,9 +38,35 @@ public async Task> GetAsync() [HttpGet("{id}")] public async Task HallarCategoriaById(int id) { - Categoria resultado = await context.FindCategoriaById(id); + Categoria resultado = await context.GetCategoriasAsyncById(id); return resultado; } + [HttpPost] + public async Task crearCategoria([FromBody] Categoria categoria) + { + if(ModelState.IsValid) + { + return BadRequest(ModelState); + } + context.crearCategoria(categoria); + var guardadoOk = await context.guardarCategoria(categoria); + return Ok(); + } + + //DELETE api/categoria/1 + [HttpDelete("{id}")] + public async Task eliminarCategoria(int id) + { + Categoria existe = await context.GetCategoriasAsyncById(id); + if(existe == null) + { + return NotFound(); + } + context.eliminarCategoria(existe); + var guardadoOk = await context.guardarCategoria(existe); + return Ok(); + } + } } \ No newline at end of file diff --git a/src/Supermarket.API/Dominio/Repositorios/CategoriaRepo.cs b/src/Supermarket.API/Dominio/Repositorios/CategoriaRepo.cs index 934b7dc..c1e1d17 100644 --- a/src/Supermarket.API/Dominio/Repositorios/CategoriaRepo.cs +++ b/src/Supermarket.API/Dominio/Repositorios/CategoriaRepo.cs @@ -19,22 +19,29 @@ public CategoriaRepo(SupermarketApiContext apicontext) db = apicontext; } - /// - /// Excepcion de FinsCategoriaById - /// - /// - /// + public void crearCategoria(Categoria categoria) + { + db.categorias.AddAsync(categoria); + } - /// - /// Devuelve lista de Categorias - /// - /// - public IEnumerable GetCategorias() + public void editarCategoria(Categoria categoria) { - IEnumerable lista = db.categorias.ToList(); - return lista; + db.Entry(categoria).State = EntityState.Modified; + db.categorias.Update(categoria); } + public void eliminarCategoria(Categoria categoria) + { + db.categorias.Remove(categoria); + } + + public async Task guardarCategoria(Categoria categoria) + { + await db.SaveChangesAsync(); + return categoria; + } + + /// /// Metodo Asincrono /// Devuelve lista de Categorias @@ -46,10 +53,11 @@ public async Task> GetCategoriasAsync() return lista; } - public async Task FindCategoriaById(int id) + public async Task GetCategoriasAsyncById(int id) { Categoria resultado = await db.categorias.FindAsync(id); return resultado; } + } } \ No newline at end of file diff --git a/src/Supermarket.API/Dominio/Repositorios/ICategoriaRepo.cs b/src/Supermarket.API/Dominio/Repositorios/ICategoriaRepo.cs index cb6517e..02c0cce 100644 --- a/src/Supermarket.API/Dominio/Repositorios/ICategoriaRepo.cs +++ b/src/Supermarket.API/Dominio/Repositorios/ICategoriaRepo.cs @@ -6,13 +6,6 @@ namespace Supermarket.API.Dominio.Repositorios { public interface ICategoriaRepo { - /// - ///Metodo Sincrono - /// Devuelve la lista de categorias desde la base de datos - /// - /// - IEnumerable GetCategorias(); - /// /// Permite obtener lista categorias/Metodo Asincrono /// @@ -24,6 +17,25 @@ public interface ICategoriaRepo /// /// Identificador de la categoria /// - Task FindCategoriaById(int id); + Task GetCategoriasAsyncById(int id); + + /// + /// Metodo que permite la creacion de una categoria, recibe como parametro una instancia de tipo Categoria + /// + /// + void crearCategoria(Categoria categoria); + /// + /// Metodo para editar una categoria, recibe una Categoria como parametro + /// + /// + void editarCategoria(Categoria categoria); + /// + /// Metodo de eliminacion de una categoria, Recibe una instancia de + /// tipo Categoria como parametro + /// + /// + void eliminarCategoria(Categoria categoria); + + Task guardarCategoria(Categoria categoria); } } \ No newline at end of file From 285a64a5acbc8f884f873b6014f1d26f9355838e Mon Sep 17 00:00:00 2001 From: Leidy Carolina Gomez Cala Date: Sun, 21 Jun 2020 00:08:46 -0500 Subject: [PATCH 3/4] verificacion --- src/Supermarket.API/Controllers/CategoriaController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Supermarket.API/Controllers/CategoriaController.cs b/src/Supermarket.API/Controllers/CategoriaController.cs index 907e317..b8738c2 100644 --- a/src/Supermarket.API/Controllers/CategoriaController.cs +++ b/src/Supermarket.API/Controllers/CategoriaController.cs @@ -45,7 +45,7 @@ public async Task HallarCategoriaById(int id) [HttpPost] public async Task crearCategoria([FromBody] Categoria categoria) { - if(ModelState.IsValid) + if(!ModelState.IsValid) { return BadRequest(ModelState); } From 8dc85e989a12e234149b8fdca5b7bd9fde571cde Mon Sep 17 00:00:00 2001 From: Leidy Carolina Gomez Cala Date: Sun, 21 Jun 2020 01:02:37 -0500 Subject: [PATCH 4/4] correcciones --- src/Supermarket.API/Controllers/CategoriaController.cs | 5 +++-- .../Dominio/Persistencia/SupermarketApiContext.cs | 2 +- src/Supermarket.API/Dominio/Repositorios/CategoriaRepo.cs | 2 +- src/Supermarket.API/Dominio/Repositorios/ICategoriaRepo.cs | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Supermarket.API/Controllers/CategoriaController.cs b/src/Supermarket.API/Controllers/CategoriaController.cs index b8738c2..88eaab8 100644 --- a/src/Supermarket.API/Controllers/CategoriaController.cs +++ b/src/Supermarket.API/Controllers/CategoriaController.cs @@ -19,11 +19,12 @@ public class CategoriaController : ControllerBase /// Metodo contructor de la clase /// /// - public CategoriaController(ICategoriaRepo CategoriaContexto) + public CategoriaController(ICategoriaRepo CategoriaContext) { - context = CategoriaContexto; + context = CategoriaContext; } //Asincrona --> Usa paralelismo en el servidor + [HttpGet] public async Task> GetAsync() { diff --git a/src/Supermarket.API/Dominio/Persistencia/SupermarketApiContext.cs b/src/Supermarket.API/Dominio/Persistencia/SupermarketApiContext.cs index bfaddd3..442e45a 100644 --- a/src/Supermarket.API/Dominio/Persistencia/SupermarketApiContext.cs +++ b/src/Supermarket.API/Dominio/Persistencia/SupermarketApiContext.cs @@ -27,7 +27,7 @@ protected override void OnModelCreating(ModelBuilder builder) builder.Entity().Property(categoria => categoria.id).ValueGeneratedOnAdd(); builder.Entity().Property(categoria => categoria.nombre).HasColumnName("NombreCompleto"); builder.Entity() - .Property(categorias => categorias.nombre) + .Property(categoria => categoria.nombre) .IsRequired() .HasMaxLength(30); diff --git a/src/Supermarket.API/Dominio/Repositorios/CategoriaRepo.cs b/src/Supermarket.API/Dominio/Repositorios/CategoriaRepo.cs index c1e1d17..e99eeae 100644 --- a/src/Supermarket.API/Dominio/Repositorios/CategoriaRepo.cs +++ b/src/Supermarket.API/Dominio/Repositorios/CategoriaRepo.cs @@ -24,7 +24,7 @@ public void crearCategoria(Categoria categoria) db.categorias.AddAsync(categoria); } - public void editarCategoria(Categoria categoria) + public void editarCategoria(int id, Categoria categoria) { db.Entry(categoria).State = EntityState.Modified; db.categorias.Update(categoria); diff --git a/src/Supermarket.API/Dominio/Repositorios/ICategoriaRepo.cs b/src/Supermarket.API/Dominio/Repositorios/ICategoriaRepo.cs index 02c0cce..15895ea 100644 --- a/src/Supermarket.API/Dominio/Repositorios/ICategoriaRepo.cs +++ b/src/Supermarket.API/Dominio/Repositorios/ICategoriaRepo.cs @@ -28,7 +28,7 @@ public interface ICategoriaRepo /// Metodo para editar una categoria, recibe una Categoria como parametro /// /// - void editarCategoria(Categoria categoria); + void editarCategoria(int id, Categoria categoria); /// /// Metodo de eliminacion de una categoria, Recibe una instancia de /// tipo Categoria como parametro