Files
ShopSolution-backend/Webshop.Api/Controllers/Customers/AddressesController.cs
2025-08-01 09:09:49 +02:00

45 lines
1.6 KiB
C#

// src/Webshop.Api/Controllers/Customer/AddressesController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Customers;
using Webshop.Application.Services.Customers;
namespace Webshop.Api.Controllers.Customer
{
[ApiController]
[Route("api/v1/customer/addresses")]
[Authorize(Roles = "Customer")]
public class AddressesController : ControllerBase
{
private readonly IAddressService _addressService;
public AddressesController(IAddressService addressService)
{
_addressService = addressService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<AddressDto>>> GetMyAddresses()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var addresses = await _addressService.GetMyAddressesAsync(userId);
return Ok(addresses);
}
[HttpPost]
public async Task<ActionResult<AddressDto>> CreateAddress([FromBody] CreateAddressDto addressDto)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var (createdAddress, errorMessage) = await _addressService.CreateAddressAsync(addressDto, userId);
if (createdAddress == null) return BadRequest(new { Message = errorMessage });
// Annahme: Es gibt eine GetMyAddressById-Methode
return CreatedAtAction(nameof(GetMyAddresses), new { id = createdAddress.Id }, createdAddress);
}
}
}