92 lines
3.4 KiB
C#
92 lines
3.4 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;
|
|
using Webshop.Application.Services.Customers.Interfaces;
|
|
|
|
namespace Webshop.Api.Controllers.Customer
|
|
{
|
|
[ApiController]
|
|
[Route("api/v1/customer/[controller]")]
|
|
[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 });
|
|
|
|
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);
|
|
|
|
if (!success) return BadRequest(new { Message = errorMessage });
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeleteAddress(Guid id)
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
var (success, errorMessage) = await _addressService.DeleteAddressAsync(id, userId);
|
|
|
|
if (!success) return BadRequest(new { Message = errorMessage });
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpPost("default-shipping/{id}")]
|
|
public async Task<IActionResult> SetDefaultShippingAddress(Guid id)
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
var (success, errorMessage) = await _addressService.SetDefaultShippingAddressAsync(id, userId);
|
|
|
|
if (!success) return BadRequest(new { Message = errorMessage });
|
|
|
|
return Ok();
|
|
}
|
|
|
|
[HttpPost("default-billing/{id}")]
|
|
public async Task<IActionResult> SetDefaultBillingAddress(Guid id)
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
var (success, errorMessage) = await _addressService.SetDefaultBillingAddressAsync(id, userId);
|
|
|
|
if (!success) return BadRequest(new { Message = errorMessage });
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
} |