This commit is contained in:
Tizian.Breuch
2025-09-25 16:11:46 +02:00
parent 36cc2d97a0
commit c80b5ccc22
3 changed files with 163 additions and 96 deletions

View File

@@ -1,13 +1,14 @@
// src/Webshop.Api/Controllers/Customer/AddressesController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Customers;
using Webshop.Application.Services.Customers;
using Webshop.Application.Services.Customers.Interfaces;
namespace Webshop.Api.Controllers.Customer
{
@@ -24,69 +25,123 @@ namespace Webshop.Api.Controllers.Customer
}
[HttpGet]
public async Task<ActionResult<IEnumerable<AddressDto>>> GetMyAddresses()
[ProducesResponseType(typeof(IEnumerable<AddressDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetMyAddresses()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var addresses = await _addressService.GetMyAddressesAsync(userId);
return Ok(addresses);
var result = await _addressService.GetMyAddressesAsync(userId!);
return Ok(result.Value);
}
[HttpGet("{id}")]
[ProducesResponseType(typeof(AddressDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetMyAddressById(Guid id)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var result = await _addressService.GetMyAddressByIdAsync(id, userId!);
return result.Type switch
{
ServiceResultType.Success => Ok(result.Value),
ServiceResultType.Forbidden => Forbid(),
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
_ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.")
};
}
[HttpPost]
public async Task<ActionResult<AddressDto>> CreateAddress([FromBody] CreateAddressDto addressDto)
[ProducesResponseType(typeof(AddressDto), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> 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 });
return CreatedAtAction(nameof(GetMyAddresses), new { id = createdAddress.Id }, createdAddress);
}
[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 userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var (success, errorMessage) = await _addressService.UpdateAddressAsync(addressDto, userId);
var result = await _addressService.CreateAddressAsync(addressDto, userId!);
if (!success) return BadRequest(new { Message = errorMessage });
return result.Type switch
{
ServiceResultType.Success => CreatedAtAction(nameof(GetMyAddressById), new { id = result.Value!.Id }, result.Value),
_ => BadRequest(new { Message = result.ErrorMessage })
};
}
return NoContent();
[HttpPut("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> UpdateAddress(Guid id, [FromBody] UpdateAddressDto addressDto)
{
if (id != addressDto.Id) return BadRequest(new { Message = "ID in der URL und im Body stimmen nicht überein." });
if (!ModelState.IsValid) return BadRequest(ModelState);
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var result = await _addressService.UpdateAddressAsync(addressDto, userId!);
return result.Type switch
{
ServiceResultType.Success => NoContent(),
ServiceResultType.Forbidden => Forbid(),
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
_ => BadRequest(new { Message = result.ErrorMessage })
};
}
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> DeleteAddress(Guid id)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var (success, errorMessage) = await _addressService.DeleteAddressAsync(id, userId);
var result = await _addressService.DeleteAddressAsync(id, userId!);
if (!success) return BadRequest(new { Message = errorMessage });
return NoContent();
return result.Type switch
{
ServiceResultType.Success => NoContent(),
ServiceResultType.Forbidden => Forbid(),
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
_ => BadRequest(new { Message = result.ErrorMessage })
};
}
[HttpPost("default-shipping/{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> SetDefaultShippingAddress(Guid id)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var (success, errorMessage) = await _addressService.SetDefaultShippingAddressAsync(id, userId);
var result = await _addressService.SetDefaultShippingAddressAsync(id, userId!);
if (!success) return BadRequest(new { Message = errorMessage });
return Ok();
return result.Type switch
{
ServiceResultType.Success => Ok(),
ServiceResultType.Forbidden => Forbid(),
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
_ => BadRequest(new { Message = result.ErrorMessage })
};
}
[HttpPost("default-billing/{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> SetDefaultBillingAddress(Guid id)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var (success, errorMessage) = await _addressService.SetDefaultBillingAddressAsync(id, userId);
var result = await _addressService.SetDefaultBillingAddressAsync(id, userId!);
if (!success) return BadRequest(new { Message = errorMessage });
return Ok();
return result.Type switch
{
ServiceResultType.Success => Ok(),
ServiceResultType.Forbidden => Forbid(),
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
_ => BadRequest(new { Message = result.ErrorMessage })
};
}
}
}