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] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] public async Task GetAllAddresses() { var result = await _adminAddressService.GetAllAddressesAsync(); return Ok(result.Value); } [HttpGet("{id}")] [ProducesResponseType(typeof(AddressDto), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] public async Task GetAddressById(Guid id) { var result = await _adminAddressService.GetAddressByIdAsync(id); return result.Type switch { ServiceResultType.Success => Ok(result.Value), _ => NotFound(new { Message = result.ErrorMessage }) }; } [HttpPost("customer/{customerId}")] [ProducesResponseType(typeof(AddressDto), StatusCodes.Status201Created)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] public async Task CreateAddressForCustomer(Guid customerId, [FromBody] CreateAddressDto addressDto) { if (!ModelState.IsValid) return BadRequest(ModelState); var result = await _adminAddressService.CreateAddressForCustomerAsync(addressDto, customerId); return result.Type switch { ServiceResultType.Success => CreatedAtAction(nameof(GetAddressById), new { id = result.Value!.Id }, result.Value), ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }), _ => BadRequest(new { Message = result.ErrorMessage }) }; } [HttpPut("{id}")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] public async Task UpdateAddress(Guid id, [FromBody] UpdateAddressDto addressDto) { if (id != addressDto.Id) return BadRequest(new { Message = "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(), _ => NotFound(new { Message = result.ErrorMessage }) }; } [HttpDelete("{id}")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] public async Task DeleteAddress(Guid id) { var result = await _adminAddressService.DeleteAddressAsync(id); return result.Type switch { ServiceResultType.Success => NoContent(), _ => NotFound(new { Message = result.ErrorMessage }) }; } } }