customer
This commit is contained in:
@@ -1,21 +1,20 @@
|
||||
// src/Webshop.Api/Controllers/Customer/ProfileController.cs
|
||||
// src/Webshop.Api/Controllers/Customer/CustomerController.cs
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
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.DTOs.Email;
|
||||
using Webshop.Application.Services;
|
||||
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;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Webshop.Api.Controllers.Customer
|
||||
{
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/customer/[controller]")] // z.B. /api/v1/customer/profile
|
||||
[Route("api/v1/customer")] // Route vereinfacht, da der Controllername "Customer" ist
|
||||
[Authorize(Roles = "Customer")]
|
||||
public class CustomerController : ControllerBase
|
||||
{
|
||||
@@ -26,62 +25,89 @@ namespace Webshop.Api.Controllers.Customer
|
||||
_customerService = customerService;
|
||||
}
|
||||
|
||||
[HttpGet("me")] // /api/v1/customer/profile/me
|
||||
public async Task<ActionResult<CustomerDto>> GetMyProfile()
|
||||
[HttpGet("profile")] // GET /api/v1/customer/profile
|
||||
[ProducesResponseType(typeof(CustomerDto), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetMyProfile()
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (string.IsNullOrEmpty(userId)) return Unauthorized(new { Message = "Benutzer-ID nicht im Token gefunden." });
|
||||
if (string.IsNullOrEmpty(userId)) return Unauthorized();
|
||||
|
||||
var customerProfile = await _customerService.GetMyProfileAsync(userId);
|
||||
if (customerProfile == null) return NotFound(new { Message = "Kundenprofil nicht gefunden. Bitte erstellen Sie es." });
|
||||
var result = await _customerService.GetMyProfileAsync(userId);
|
||||
|
||||
return Ok(customerProfile);
|
||||
return result.Type switch
|
||||
{
|
||||
ServiceResultType.Success => Ok(result.Value),
|
||||
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||||
_ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.")
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPost("change-password")] // /api/v1/customer/profile/change-password
|
||||
public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordRequestDto 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.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
|
||||
[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<IActionResult> UpdateProfile([FromBody] UpdateCustomerDto 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." });
|
||||
if (string.IsNullOrEmpty(userId)) return Unauthorized();
|
||||
|
||||
var (success, errorMessage) = await _customerService.UpdateMyProfileAsync(userId, request);
|
||||
var result = await _customerService.UpdateMyProfileAsync(userId, request);
|
||||
|
||||
if (!success) return BadRequest(new { Message = errorMessage });
|
||||
|
||||
return Ok(new { Message = "Profil erfolgreich aktualisiert." });
|
||||
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-email-request")] // /api/v1/customer/profile/change-email-request
|
||||
public async Task<IActionResult> ChangeEmailRequest([FromBody] ChangeEmailRequestDto request) // DTO erstellen
|
||||
[HttpPost("change-password")] // POST /api/v1/customer/change-password
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordRequestDto 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." });
|
||||
if (string.IsNullOrEmpty(userId)) return Unauthorized();
|
||||
|
||||
var (success, errorMessage) = await _customerService.ChangeEmailAsync(userId, request.NewEmail, request.CurrentPassword);
|
||||
var result = await _customerService.ChangePasswordAsync(userId, request);
|
||||
|
||||
if (!success) return BadRequest(new { Message = errorMessage });
|
||||
return result.Type switch
|
||||
{
|
||||
ServiceResultType.Success => Ok(new { Message = "Passwort erfolgreich ge<67>ndert." }),
|
||||
ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
|
||||
_ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.")
|
||||
};
|
||||
}
|
||||
|
||||
return Ok(new { Message = errorMessage }); // Sendet Info, dass Mail gesendet wurde
|
||||
[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<IActionResult> 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<74>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.")
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user