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 ProductVariantsController : ControllerBase { private readonly IProductService _productService; public ProductVariantsController(IProductService productService) { _productService = productService; } [HttpGet("product/{productId}")] public async Task>> GetProductVariants(Guid productId) { var variants = await _productService.GetProductVariantsAsync(productId); return Ok(variants); } [HttpGet("{id}")] public async Task> GetProductVariant(Guid id) { var variant = await _productService.GetProductVariantByIdAsync(id); if (variant == null) return NotFound(); return Ok(variant); } [HttpPost] [Authorize(Roles = "Admin")] public async Task> CreateProductVariant(CreateProductVariantDto createVariantDto) { try { var variant = await _productService.CreateProductVariantAsync(createVariantDto); return CreatedAtAction(nameof(GetProductVariant), new { id = variant.Id }, variant); } catch (ArgumentException ex) { return BadRequest(ex.Message); } } [HttpPut("{id}")] [Authorize(Roles = "Admin")] public async Task UpdateProductVariant(Guid id, UpdateProductVariantDto updateVariantDto) { try { await _productService.UpdateProductVariantAsync(id, updateVariantDto); return NoContent(); } catch (ArgumentException ex) { return BadRequest(ex.Message); } } [HttpDelete("{id}")] [Authorize(Roles = "Admin")] public async Task DeleteProductVariant(Guid id) { try { await _productService.DeleteProductVariantAsync(id); return NoContent(); } catch (ArgumentException ex) { return BadRequest(ex.Message); } } }