using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using LittleShop.DTOs; using LittleShop.Services; namespace LittleShop.Controllers; [ApiController] [Route("api/[controller]")] [Authorize(AuthenticationSchemes = "Bearer")] 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>> GetCategories() { var categories = await _categoryService.GetAllCategoriesAsync(); return Ok(categories.Where(c => c.IsActive)); } [HttpGet("categories/{id}")] public async Task> GetCategory(Guid id) { var category = await _categoryService.GetCategoryByIdAsync(id); if (category == null || !category.IsActive) { return NotFound(); } return Ok(category); } [HttpGet("products")] public async Task>> 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> GetProduct(Guid id) { var product = await _productService.GetProductByIdAsync(id); if (product == null || !product.IsActive) { return NotFound(); } return Ok(product); } }