This commit is contained in:
Tizian.Breuch
2025-07-29 14:04:35 +02:00
parent 2d6eeb7a50
commit c1ee56c81c
19 changed files with 339 additions and 125 deletions

View File

@@ -1,15 +1,19 @@
// src/Webshop.Api/Controllers/Customer/ProfileController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using Webshop.Application.DTOs; // CustomerDto
using Webshop.Application.DTOs.Auth; // ChangePasswordRequestDto
using Webshop.Application.DTOs.Customers; // UpdateCustomerProfileDto
using Webshop.Application.Services;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Customers;
using Webshop.Application.Services.Customers.Interfaces;
using Webshop.Application.Services.Customers;
namespace Webshop.Api.Controllers.Customers
namespace Webshop.Api.Controllers.Customer
{
[ApiController]
[Route("api/v1/customer/profile")] // Eindeutige Route f<>r das Profil
[Authorize(Roles = "Customer")] // Nur f<>r eingeloggte Kunden!
[Route("api/v1/customer/[controller]")] // z.B. /api/v1/customer/profile
[Authorize(Roles = "Customer")]
public class ProfileController : ControllerBase
{
private readonly ICustomerService _customerService;
@@ -19,33 +23,49 @@ namespace Webshop.Api.Controllers.Customers
_customerService = customerService;
}
// Hilfsmethode, um die ID des eingeloggten Benutzers aus dem Token zu holen
private string GetUserId() => User.FindFirstValue(ClaimTypes.NameIdentifier)!;
[HttpGet("me")] // GET /api/v1/customer/profile/me
[HttpGet("me")] // /api/v1/customer/profile/me
public async Task<ActionResult<CustomerDto>> GetMyProfile()
{
var userId = GetUserId();
var profile = await _customerService.GetMyProfileAsync(userId);
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(userId)) return Unauthorized(new { Message = "Benutzer-ID nicht im Token gefunden." });
if (profile == null)
{
return NotFound("Kundenprofil nicht gefunden.");
}
return Ok(profile);
var customerProfile = await _customerService.GetMyProfileAsync(userId);
if (customerProfile == null) return NotFound(new { Message = "Kundenprofil nicht gefunden. Bitte erstellen Sie es." });
return Ok(customerProfile);
}
[HttpPut("me")] // PUT /api/v1/customer/profile/me
public async Task<IActionResult> UpdateMyProfile([FromBody] UpdateCustomerProfileDto profileDto)
[HttpPost("change-password")] // /api/v1/customer/profile/change-password
public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordRequestDto request)
{
var userId = GetUserId();
var success = await _customerService.UpdateMyProfileAsync(userId, profileDto);
if (!ModelState.IsValid) return BadRequest(ModelState);
if (!success)
{
return NotFound("Kundenprofil nicht gefunden.");
}
return NoContent(); // Standardantwort f<>r ein erfolgreiches Update
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(userId)) return Unauthorized(new { Message = "Benutzer-ID nicht im Token gefunden." });
var (success, errorMessage) = await _customerService.ChangePasswordAsync(userId, request);
if (!success) return BadRequest(new { Message = errorMessage });
return Ok(new { Message = "Passwort erfolgreich ge<67>ndert. Bitte melden Sie sich mit dem neuen Passwort an." });
}
// << NEUER/AKTUALISIERTER ENDPUNKT F<>R PROFIL-AKTUALISIERUNG >>
[HttpPost("update-profile")] // /api/v1/customer/profile/update-profile
public async Task<IActionResult> UpdateProfile([FromBody] UpdateCustomerProfileDto request)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(userId)) return Unauthorized(new { Message = "Benutzer-ID nicht im Token gefunden." });
var (success, errorMessage) = await _customerService.UpdateMyProfileAsync(userId, request);
if (!success) return BadRequest(new { Message = errorMessage });
return Ok(new { Message = "Profil erfolgreich aktualisiert." });
}
// << ENTFERNT: UpdateContactInfo Endpoint >>
}
}