ShopOnlineSolution/ShopOnline.Api/Controllers/ProductController.cs

79 lines
2.6 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ShopOnline.Api.Extensions;
using ShopOnline.Api.Repositories.Contracts;
using ShopOnline.Models.Dtos;
namespace ShopOnline.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ProductController : ControllerBase
{
private readonly IProductRepository productRepository;
public ProductController(IProductRepository productRepository)
{
this.productRepository = productRepository;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<ProductDto>>> GetItems()
{
try
{
var products = await this.productRepository.GetItems();
//foreach (var product in products)
//{
// Console.WriteLine("Product [" + product.Id + "]: " + product.Price + " / " + product.Quantity);
//}
var productCategories = await this.productRepository.GetCategories();
if (products == null || productCategories == null)
{
return NotFound();
}
else
{
var productDtos = products.ConvertToDto(productCategories);
//foreach (var product in productDtos)
//{
// Console.WriteLine("ProductDTO [" + product.Id + "]: " + product.Price + " / " + product.Quantity);
//}
return Ok(productDtos);
}
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError,
"Error retrieving data from the database.");
}
}
[HttpGet("{id:int}")]
public async Task<ActionResult<ProductDto>> GetItem(int id)
{
try
{
var product = await this.productRepository.GetItem(id);
if (product == null)
{
return BadRequest();
}
else
{
var productCategory = await this.productRepository.GetCategory(product.CategoryId);
var productDto = product.ConvertToDto(productCategory);
return Ok(productDto);
}
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError,
"Error retrieving data from the database.");
}
}
}
}