Files
Tizian.Breuch de67e01f2c
All checks were successful
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Successful in 25s
fix
2025-11-26 17:31:28 +01:00

54 lines
2.0 KiB
C#

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; // Für CartDto
using Webshop.Application.DTOs.Shipping; // Für CartItemDto
using Webshop.Application.Services.Customers.Interfaces;
namespace Webshop.Api.Controllers.Customer
{
[ApiController]
[Route("api/v1/customer/[controller]")]
[Authorize(Roles = "Customer")]
public class CartController : ControllerBase // <--- WICHTIG: Muss public sein und erben
{
private readonly ICartService _cartService;
public CartController(ICartService cartService)
{
_cartService = cartService;
}
[HttpGet]
[ProducesResponseType(typeof(CartDto), StatusCodes.Status200OK)]
public async Task<IActionResult> GetCart()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var result = await _cartService.GetCartAsync(userId!);
return Ok(result.Value);
}
[HttpPost("items")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> AddToCart([FromBody] CartItemDto item)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var result = await _cartService.AddToCartAsync(userId!, item);
return result.Type == ServiceResultType.Success ? Ok() : BadRequest(new { Message = result.ErrorMessage });
}
[HttpDelete("items/{productId}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<IActionResult> RemoveItem(Guid productId)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
await _cartService.RemoveFromCartAsync(userId!, productId);
return NoContent();
}
}
}