fix
All checks were successful
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Successful in 25s

This commit is contained in:
Tizian.Breuch
2025-11-26 17:31:28 +01:00
parent 9c48f62ff0
commit de67e01f2c
4 changed files with 28 additions and 12 deletions

View File

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

View File

@@ -1,6 +1,6 @@
using System; using System;
using System.Collections.Generic; 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 namespace Webshop.Application.DTOs.Customers
{ {
@@ -9,6 +9,8 @@ namespace Webshop.Application.DTOs.Customers
public Guid Id { get; set; } public Guid Id { get; set; }
public string UserId { get; set; } = string.Empty; public string UserId { get; set; } = string.Empty;
public List<CartItemDto> Items { get; set; } = new List<CartItemDto>(); 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.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application; using Webshop.Application;
using Webshop.Application.DTOs.Customers; using Webshop.Application.DTOs.Customers; // <--- DIESE ZEILE HAT GEFEHLT (für CartDto)
using Webshop.Application.DTOs.Shipping; using Webshop.Application.DTOs.Shipping; // (für CartItemDto)
using Webshop.Application.Services.Customers.Interfaces; using Webshop.Application.Services.Customers.Interfaces;
using Webshop.Domain.Entities; using Webshop.Domain.Entities;
using Webshop.Infrastructure.Data; using Webshop.Infrastructure.Data;
@@ -20,10 +20,16 @@ namespace Webshop.Application.Services.Customers
_context = context; _context = context;
} }
// Die Implementierung muss exakt so aussehen:
public async Task<ServiceResult<CartDto>> GetCartAsync(string userId) public async Task<ServiceResult<CartDto>> GetCartAsync(string userId)
{ {
var cart = await GetCartEntity(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 var dto = new CartDto
{ {
@@ -36,6 +42,7 @@ namespace Webshop.Application.Services.Customers
Quantity = i.Quantity Quantity = i.Quantity
}).ToList() }).ToList()
}; };
return ServiceResult.Ok(dto); return ServiceResult.Ok(dto);
} }
@@ -63,7 +70,8 @@ namespace Webshop.Application.Services.Customers
ProductVariantId = itemDto.ProductVariantId, ProductVariantId = itemDto.ProductVariantId,
Quantity = itemDto.Quantity 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); _context.CartItems.Add(newItem);
} }

View File

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