I am working in net core 3.1 with a database, and I want to obtain the products but with their respective category, subcategory and brand, in the database I only have the id obviously, that is, in the articles table I have idcategory idsubcategory and idbrand.
I have the following: IproductService interface:
using APIGESTION.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace APIGESTION.Services
{
public interface IProductoService
{
public void Add(ProductoRequest model)
{
}
List<Producto> GetProductos();
}
}
And its implementation with LINQ:
using APIGESTION.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace APIGESTION.Services
{
public class ProductoService : IProductoService
{
public void Add(ProductoRequest model)
{
using (gestionContext db = new gestionContext())
{
using (var transaction = db.Database.BeginTransaction())
try
{
var producto = new Producto();
producto.IdProducto = model.IdProducto;
producto.Precio = model.Precio;
producto.Stock = model.Stock;
producto.IdCategoria = model.idCategoria;
producto.nombreCategoria = model.nombreCategoria;
producto.idSubCategoria = model.idSubCategoria;
producto.nombreSubCategoria = model.nombreSubCategoria;
producto.idMarca = model.idMarca;
producto.nombreMarca = model.nombreMarca;
producto.CodAlfanumerico = model.CodAlfanumerico;
producto.CodBarra = model.CodBarra;
producto.Aliiva = model.Aliiva;
producto.Unidad = model.Unidad;
producto.Contenido = model.Contenido;
producto.costo = model.costo;
db.Productos.Add(producto);
db.SaveChanges();
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw new Exception("Ocurrio un error en la insercion");
}
}
}
public List<Producto> GetProductos()
{
using (gestionContext db = new gestionContext())
{
var productos=(from p in db.Productos join cat in db.Categoria on p.idCategoria equals cat.IdCategoria join
subcat in db.Subcategoria on p.idSubCategoria equals subcat.IdSubcategoria join m in db.Marcas on
p.idMarca equals m.IdMarca
select new
{
IdProducto = p.IdProducto,
Precio = p.Precio,
Stock = p.Stock,
IdCategoria = p.idCategoria,
IdSubcategoria = p.idSubCategoria,
IdMarca = p.idMarca,
CodAlfanumerico = p.CodAlfanumerico,
CodBarra = p.CodBarra,
Aliiva = p.Aliiva,
Unidad = p.Unidad,
Contenido = p.Contenido,
costo = p.costo,
NombreProducto = p.NombreProducto,
nombreCategoria = p.nombreCategoria,
nombreMarca = p.nombreMarca,
});
return (List<Producto>)productos;
}
}
}
}
Now, how do I call the method that obtains the list in the item or product controller?
namespace APIGESTION.Controllers
{
[Route("[controller]")]
[ApiController]
[Authorize]
public class ProductoController : ControllerBase
{
private readonly gestionContext _context;
private IProductoService _producto;
public ProductoController(gestionContext context)
{
_context = context;
}
// GET: api/Cliente
[HttpGet]
public IActionResult GetProducto()
{
var Producto = _producto.GetProductos();
return Producto;
}
..demas codigo
I don't know if it's okay like that. It does not give a compilation error but when I go to my client application and request the data it tells me:
System.NullReferenceException: 'Object reference not set to an instance of an object.' _product was null.
You are using dependency injection, but you are not passing the dependency to your controller, so _product is null.
You are missing in the constructor to add it, just as you added the context: