Sky.Net/API/Controllers/ProductsController.cs
2022-05-11 12:04:56 -07:00

54 lines
1.9 KiB
C#

using Core.Entities;
using Microsoft.AspNetCore.Mvc;
using Core.Interfaces;
using Core.Specifications;
using API.Dtos;
using AutoMapper;
namespace API.Controllers
{
public class ProductsController : BaseApiController
{
private readonly IGenericRepository<Product> _productsRepo;
private readonly IGenericRepository<ProductBrand> _productBrandRepo;
private readonly IGenericRepository<ProductType> _productTypeRepo;
private readonly IMapper _mapper;
public ProductsController(IGenericRepository<Product> productsRepo, IGenericRepository<ProductBrand> productBrandRepo, IGenericRepository<ProductType> productTypeRepo, IMapper mapper)
{
_mapper = mapper;
_productTypeRepo = productTypeRepo;
_productBrandRepo = productBrandRepo;
_productsRepo = productsRepo;
}
[HttpGet]
public async Task<ActionResult<IReadOnlyList<ProductToReturnDto>>> GetProducts()
{
var spec = new ProductsWithTypesAndBrandsSpecification();
var products = await _productsRepo.ListAsync(spec);
return Ok(_mapper.Map<IReadOnlyList<Product>, IReadOnlyList<ProductToReturnDto>>(products));
}
[HttpGet("{id}")]
public async Task<ActionResult<ProductToReturnDto>> GetProduct(int id)
{
var spec = new ProductsWithTypesAndBrandsSpecification(id);
var product = await _productsRepo.GetEntityWithSpec(spec);
return _mapper.Map<Product, ProductToReturnDto>(product);
}
[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());
}
}
}