using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Security.Claims; using System.Threading.Tasks; using Webshop.Application; using Webshop.Application.DTOs.Customers; using Webshop.Application.DTOs.Shipping; using Webshop.Application.Services.Customers.Interfaces; namespace Webshop.Api.Controllers.Customer { [ApiController] [Route("api/v1/customer/[controller]")] [Authorize(Roles = "Customer")] public class CartController : ControllerBase { private readonly ICartService _cartService; public CartController(ICartService cartService) { _cartService = cartService; } [HttpGet] public async Task GetCart() { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var result = await _cartService.GetCartAsync(userId!); return Ok(result.Value); } [HttpPost("items")] public async Task AddToCart([FromBody] CartItemDto item) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var result = await _cartService.AddToCartAsync(userId!, item); return result.Type == ServiceResultType.Success ? Ok() : BadRequest(result.ErrorMessage); } [HttpDelete("items/{productId}")] public async Task RemoveItem(Guid productId) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); await _cartService.RemoveFromCartAsync(userId!, productId); return NoContent(); } } }