Sky.Net/API/Controllers/ProductsController.cs
Charles Showalter 8bca3f1f9d Added Generics
2022-05-10 15:45:47 -07:00

51 lines
1.7 KiB
C#

using Core.Entities;
using Microsoft.AspNetCore.Mvc;
using Core.Interfaces;
using Core.Specifications;
namespace API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly IGenericRepository<Product> _productsRepo;
private readonly IGenericRepository<ProductBrand> _productBrandRepo;
private readonly IGenericRepository<ProductType> _productTypeRepo;
public ProductsController(IGenericRepository<Product> productsRepo, IGenericRepository<ProductBrand> productBrandRepo, IGenericRepository<ProductType> productTypeRepo)
{
_productTypeRepo = productTypeRepo;
_productBrandRepo = productBrandRepo;
_productsRepo = productsRepo;
}
[HttpGet]
public async Task<ActionResult<List<Product>>> GetProducts()
{
var spec = new ProductsWithTypesAndBrandsSpecification();
var products = await _productsRepo.ListAsync(spec);
return Ok(products);
}
[HttpGet("{id}")]
public async Task<ActionResult<Product>> GetProduct(int id)
{
var spec = new ProductsWithTypesAndBrandsSpecification(id);
return await _productsRepo.GetEntityWithSpec(spec);
}
[HttpGet("brands")]
public async Task<ActionResult<IReadOnlyList<ProductBrand>>> GetProductBrands()
{
return Ok(await _productBrandRepo.ListAllAsync());
}
[HttpGet("types")]
public async Task<ActionResult<IReadOnlyList<ProductType>>> GetProductTypes()
{
return Ok(await _productTypeRepo.ListAllAsync());
}
}
}