littleshop/Controllers/CatalogController.cs
2025-08-20 13:20:19 +01:00

60 lines
1.7 KiB
C#

using Microsoft.AspNetCore.Mvc;
using LittleShop.DTOs;
using LittleShop.Services;
namespace LittleShop.Controllers;
[ApiController]
[Route("api/[controller]")]
public class CatalogController : ControllerBase
{
private readonly ICategoryService _categoryService;
private readonly IProductService _productService;
public CatalogController(ICategoryService categoryService, IProductService productService)
{
_categoryService = categoryService;
_productService = productService;
}
[HttpGet("categories")]
public async Task<ActionResult<IEnumerable<CategoryDto>>> GetCategories()
{
var categories = await _categoryService.GetAllCategoriesAsync();
return Ok(categories.Where(c => c.IsActive));
}
[HttpGet("categories/{id}")]
public async Task<ActionResult<CategoryDto>> GetCategory(Guid id)
{
var category = await _categoryService.GetCategoryByIdAsync(id);
if (category == null || !category.IsActive)
{
return NotFound();
}
return Ok(category);
}
[HttpGet("products")]
public async Task<ActionResult<IEnumerable<ProductDto>>> GetProducts([FromQuery] Guid? categoryId = null)
{
var products = categoryId.HasValue
? await _productService.GetProductsByCategoryAsync(categoryId.Value)
: await _productService.GetAllProductsAsync();
return Ok(products);
}
[HttpGet("products/{id}")]
public async Task<ActionResult<ProductDto>> GetProduct(Guid id)
{
var product = await _productService.GetProductByIdAsync(id);
if (product == null || !product.IsActive)
{
return NotFound();
}
return Ok(product);
}
}