// src/Webshop.Api/Controllers/Customer/CustomerController.cs using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Security.Claims; using System.Threading.Tasks; using Webshop.Application; using Webshop.Application.DTOs; using Webshop.Application.DTOs.Auth; using Webshop.Application.DTOs.Customers; using Webshop.Application.DTOs.Email; using Webshop.Application.Services.Customers; namespace Webshop.Api.Controllers.Customer { [ApiController] [Route("api/v1/customer")] // Route vereinfacht, da der Controllername "Customer" ist [Authorize(Roles = "Customer")] public class CustomerController : ControllerBase { private readonly ICustomerService _customerService; public CustomerController(ICustomerService customerService) { _customerService = customerService; } [HttpGet("profile")] // GET /api/v1/customer/profile [ProducesResponseType(typeof(CustomerDto), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] public async Task GetMyProfile() { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (string.IsNullOrEmpty(userId)) return Unauthorized(); var result = await _customerService.GetMyProfileAsync(userId); return result.Type switch { ServiceResultType.Success => Ok(result.Value), ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }), _ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.") }; } [HttpPut("profile")] // PUT /api/v1/customer/profile [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] public async Task UpdateProfile([FromBody] UpdateCustomerDto request) { if (!ModelState.IsValid) return BadRequest(ModelState); var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (string.IsNullOrEmpty(userId)) return Unauthorized(); var result = await _customerService.UpdateMyProfileAsync(userId, request); return result.Type switch { ServiceResultType.Success => NoContent(), ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }), ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }), _ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.") }; } [HttpPost("change-password")] // POST /api/v1/customer/change-password [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] public async Task ChangePassword([FromBody] ChangePasswordRequestDto request) { if (!ModelState.IsValid) return BadRequest(ModelState); var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (string.IsNullOrEmpty(userId)) return Unauthorized(); var result = await _customerService.ChangePasswordAsync(userId, request); return result.Type switch { ServiceResultType.Success => Ok(new { Message = "Passwort erfolgreich geändert." }), ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }), _ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.") }; } [HttpPost("change-email-request")] // POST /api/v1/customer/change-email-request [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)] public async Task ChangeEmailRequest([FromBody] ChangeEmailRequestDto request) { if (!ModelState.IsValid) return BadRequest(ModelState); var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (string.IsNullOrEmpty(userId)) return Unauthorized(); var result = await _customerService.ChangeEmailAsync(userId, request.NewEmail, request.CurrentPassword); return result.Type switch { ServiceResultType.Success => Ok(new { Message = result.ErrorMessage }), // Enthält die Erfolgsmeldung ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }), ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }), _ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.") }; } } }