test #2

Merged
admin merged 9 commits from test into develop 2025-12-04 11:54:18 +00:00
4 changed files with 28 additions and 12 deletions
Showing only changes of commit de67e01f2c - Show all commits

View File

@@ -5,8 +5,8 @@ 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.DTOs.Customers; // Für CartDto
using Webshop.Application.DTOs.Shipping; // Für CartItemDto
using Webshop.Application.Services.Customers.Interfaces;
namespace Webshop.Api.Controllers.Customer
@@ -14,7 +14,7 @@ namespace Webshop.Api.Controllers.Customer
[ApiController]
[Route("api/v1/customer/[controller]")]
[Authorize(Roles = "Customer")]
public class CartController : ControllerBase
public class CartController : ControllerBase // <--- WICHTIG: Muss public sein und erben
{
private readonly ICartService _cartService;
@@ -24,6 +24,7 @@ namespace Webshop.Api.Controllers.Customer
}
[HttpGet]
[ProducesResponseType(typeof(CartDto), StatusCodes.Status200OK)]
public async Task<IActionResult> GetCart()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
@@ -32,14 +33,17 @@ namespace Webshop.Api.Controllers.Customer
}
[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(result.ErrorMessage);
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);

View File

@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using Webshop.Application.DTOs.Shipping; // Oder wo CartItemDto liegt
using Webshop.Application.DTOs.Shipping; // Wichtig für CartItemDto
namespace Webshop.Application.DTOs.Customers
{
@@ -9,6 +9,8 @@ namespace Webshop.Application.DTOs.Customers
public Guid Id { get; set; }
public string UserId { get; set; } = string.Empty;
public List<CartItemDto> Items { get; set; } = new List<CartItemDto>();
public decimal TotalEstimatedPrice { get; set; } // Optional: Summe berechnet
// Optional: Gesamtsumme zur Anzeige im Frontend
public decimal TotalPrice { get; set; }
}
}

View File

@@ -3,8 +3,8 @@ using System;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Customers;
using Webshop.Application.DTOs.Shipping;
using Webshop.Application.DTOs.Customers; // <--- DIESE ZEILE HAT GEFEHLT (für CartDto)
using Webshop.Application.DTOs.Shipping; // (für CartItemDto)
using Webshop.Application.Services.Customers.Interfaces;
using Webshop.Domain.Entities;
using Webshop.Infrastructure.Data;
@@ -20,10 +20,16 @@ namespace Webshop.Application.Services.Customers
_context = context;
}
// Die Implementierung muss exakt so aussehen:
public async Task<ServiceResult<CartDto>> GetCartAsync(string userId)
{
var cart = await GetCartEntity(userId);
if (cart == null) return ServiceResult.Ok(new CartDto()); // Leerer Warenkorb
// Wenn kein Warenkorb da ist, geben wir einen leeren zurück (Success)
if (cart == null)
{
return ServiceResult.Ok(new CartDto { UserId = userId });
}
var dto = new CartDto
{
@@ -36,6 +42,7 @@ namespace Webshop.Application.Services.Customers
Quantity = i.Quantity
}).ToList()
};
return ServiceResult.Ok(dto);
}
@@ -63,7 +70,8 @@ namespace Webshop.Application.Services.Customers
ProductVariantId = itemDto.ProductVariantId,
Quantity = itemDto.Quantity
};
cart.Items.Add(newItem); // Wichtig: Muss in CartItems DbSet landen
// WICHTIG: Zur Collection hinzufügen UND dem Context Bescheid geben
cart.Items.Add(newItem);
_context.CartItems.Add(newItem);
}

View File

@@ -1,6 +1,8 @@
using System.Threading.Tasks;
using Webshop.Application.DTOs.Shipping; // Für CartItemDto
using Webshop.Application; // Für ServiceResult
using System; // Für Guid
using Webshop.Application.DTOs.Customers; // <--- WICHTIG für CartDto
using Webshop.Application.DTOs.Shipping;
using Webshop.Application;
namespace Webshop.Application.Services.Customers.Interfaces
{