using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Threading.Tasks; using Webshop.Application; using Webshop.Application.DTOs.Customers; using Webshop.Application.Services.Admin.Interfaces; namespace Webshop.Api.Controllers.Admin { [ApiController] [Route("api/v1/admin/[controller]")] [Authorize(Roles = "Admin")] public class AdminAddressesController : ControllerBase { private readonly IAdminAddressService _adminAddressService; public AdminAddressesController(IAdminAddressService adminAddressService) { _adminAddressService = adminAddressService; } [HttpGet] public async Task GetAllUnlinkedAddresses() { var result = await _adminAddressService.GetAllUnlinkedAddressesAsync(); return Ok(result.Value); } [HttpGet("{id}")] public async Task GetAddressById(Guid id) { var result = await _adminAddressService.GetAddressByIdAsync(id); return result.Type switch { ServiceResultType.Success => Ok(result.Value), ServiceResultType.Forbidden => Forbid(), _ => NotFound(new { Message = result.ErrorMessage }) }; } [HttpPost] // Route vereinfacht, da keine customerId mehr benötigt wird public async Task CreateAddress([FromBody] CreateAddressDto addressDto) { if (!ModelState.IsValid) return BadRequest(ModelState); var result = await _adminAddressService.CreateAddressAsync(addressDto); return result.Type switch { ServiceResultType.Success => CreatedAtAction(nameof(GetAddressById), new { id = result.Value!.Id }, result.Value), _ => BadRequest(new { Message = result.ErrorMessage }) }; } [HttpPut("{id}")] public async Task UpdateAddress(Guid id, [FromBody] UpdateAddressDto addressDto) { if (id != addressDto.Id) return BadRequest("ID in URL und Body stimmen nicht überein."); if (!ModelState.IsValid) return BadRequest(ModelState); var result = await _adminAddressService.UpdateAddressAsync(addressDto); return result.Type switch { ServiceResultType.Success => NoContent(), ServiceResultType.Forbidden => Forbid(), _ => NotFound(new { Message = result.ErrorMessage }) }; } [HttpDelete("{id}")] public async Task DeleteAddress(Guid id) { var result = await _adminAddressService.DeleteAddressAsync(id); return result.Type switch { ServiceResultType.Success => NoContent(), ServiceResultType.Forbidden => Forbid(), _ => NotFound(new { Message = result.ErrorMessage }) }; } } }