test #2

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

View File

@@ -0,0 +1,50 @@
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<IActionResult> GetCart()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var result = await _cartService.GetCartAsync(userId!);
return Ok(result.Value);
}
[HttpPost("items")]
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);
}
[HttpDelete("items/{productId}")]
public async Task<IActionResult> RemoveItem(Guid productId)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
await _cartService.RemoveFromCartAsync(userId!, productId);
return NoContent();
}
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using Webshop.Application.DTOs.Shipping; // Oder wo CartItemDto liegt
namespace Webshop.Application.DTOs.Customers
{
public class CartDto
{
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
}
}

View File

@@ -1,7 +1,12 @@
using Microsoft.EntityFrameworkCore;
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.Services.Customers.Interfaces;
using Webshop.Domain.Entities;
using Webshop.Infrastructure.Data;
namespace Webshop.Application.Services.Customers
@@ -15,24 +20,87 @@ namespace Webshop.Application.Services.Customers
_context = context;
}
public async Task<ServiceResult<CartDto>> GetCartAsync(string userId)
{
var cart = await GetCartEntity(userId);
if (cart == null) return ServiceResult.Ok(new CartDto()); // Leerer Warenkorb
var dto = new CartDto
{
Id = cart.Id,
UserId = userId,
Items = cart.Items.Select(i => new CartItemDto
{
ProductId = i.ProductId,
ProductVariantId = i.ProductVariantId,
Quantity = i.Quantity
}).ToList()
};
return ServiceResult.Ok(dto);
}
public async Task<ServiceResult> AddToCartAsync(string userId, CartItemDto itemDto)
{
var cart = await GetCartEntity(userId);
if (cart == null)
{
cart = new Cart { UserId = userId, Id = Guid.NewGuid() };
_context.Carts.Add(cart);
}
var existingItem = cart.Items.FirstOrDefault(i => i.ProductId == itemDto.ProductId && i.ProductVariantId == itemDto.ProductVariantId);
if (existingItem != null)
{
existingItem.Quantity += itemDto.Quantity;
}
else
{
var newItem = new CartItem
{
CartId = cart.Id,
ProductId = itemDto.ProductId,
ProductVariantId = itemDto.ProductVariantId,
Quantity = itemDto.Quantity
};
cart.Items.Add(newItem); // Wichtig: Muss in CartItems DbSet landen
_context.CartItems.Add(newItem);
}
cart.LastModified = DateTime.UtcNow;
await _context.SaveChangesAsync();
return ServiceResult.Ok();
}
public async Task<ServiceResult> RemoveFromCartAsync(string userId, Guid productId)
{
var cart = await GetCartEntity(userId);
if (cart == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Warenkorb leer.");
var item = cart.Items.FirstOrDefault(i => i.ProductId == productId);
if (item != null)
{
_context.CartItems.Remove(item);
await _context.SaveChangesAsync();
}
return ServiceResult.Ok();
}
public async Task ClearCartAsync(string userId)
{
// Wir suchen den Warenkorb des Users
// Annahme: Es gibt eine 'Carts' Tabelle und 'CartItems'
var cart = await _context.Carts
.Include(c => c.Items)
.FirstOrDefaultAsync(c => c.UserId == userId);
var cart = await GetCartEntity(userId);
if (cart != null && cart.Items.Any())
{
// Option A: Nur Items löschen, Warenkorb behalten
_context.CartItems.RemoveRange(cart.Items);
// Option B: Ganzen Warenkorb löschen (dann wird er beim nächsten AddToCart neu erstellt)
// _context.Carts.Remove(cart);
await _context.SaveChangesAsync();
}
}
private async Task<Cart?> GetCartEntity(string userId)
{
return await _context.Carts
.Include(c => c.Items)
.FirstOrDefaultAsync(c => c.UserId == userId);
}
}
}

View File

@@ -1,9 +1,14 @@
using System.Threading.Tasks;
using Webshop.Application.DTOs.Shipping; // Für CartItemDto
using Webshop.Application; // Für ServiceResult
namespace Webshop.Application.Services.Customers.Interfaces
{
public interface ICartService
{
Task<ServiceResult<CartDto>> GetCartAsync(string userId);
Task<ServiceResult> AddToCartAsync(string userId, CartItemDto item);
Task<ServiceResult> RemoveFromCartAsync(string userId, Guid productId);
Task ClearCartAsync(string userId);
}
}