147 lines
6.3 KiB
C#
147 lines
6.3 KiB
C#
// 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;
|
|
|
|
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]
|
|
[ProducesResponseType(typeof(IEnumerable<AddressDto>), StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> GetMyAddresses()
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
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]
|
|
[ProducesResponseType(typeof(AddressDto), StatusCodes.Status201Created)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> CreateAddress([FromBody] CreateAddressDto addressDto)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
var result = await _addressService.CreateAddressAsync(addressDto, userId!);
|
|
|
|
return result.Type switch
|
|
{
|
|
ServiceResultType.Success => CreatedAtAction(nameof(GetMyAddressById), new { id = result.Value!.Id }, result.Value),
|
|
_ => BadRequest(new { Message = result.ErrorMessage })
|
|
};
|
|
}
|
|
|
|
[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 result = await _addressService.DeleteAddressAsync(id, userId!);
|
|
|
|
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 result = await _addressService.SetDefaultShippingAddressAsync(id, userId!);
|
|
|
|
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 result = await _addressService.SetDefaultBillingAddressAsync(id, userId!);
|
|
|
|
return result.Type switch
|
|
{
|
|
ServiceResultType.Success => Ok(),
|
|
ServiceResultType.Forbidden => Forbid(),
|
|
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
|
_ => BadRequest(new { Message = result.ErrorMessage })
|
|
};
|
|
}
|
|
}
|
|
} |