Files
ShopSolution-backend/Webshop.Api/Controllers/Admin/AdminAddressesController.cs
Tizian.Breuch 38acb4bbbb adminaddre
2025-10-10 11:29:20 +02:00

82 lines
3.1 KiB
C#

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<IActionResult> GetAllUnlinkedAddresses()
{
var result = await _adminAddressService.GetAllUnlinkedAddressesAsync();
return Ok(result.Value);
}
[HttpGet("{id}")]
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> DeleteAddress(Guid id)
{
var result = await _adminAddressService.DeleteAddressAsync(id);
return result.Type switch
{
ServiceResultType.Success => NoContent(),
ServiceResultType.Forbidden => Forbid(),
_ => NotFound(new { Message = result.ErrorMessage })
};
}
}
}