controller

This commit is contained in:
Tizian.Breuch
2025-10-10 11:18:33 +02:00
parent 5f8d3c167c
commit 48617f983a

View File

@@ -1,12 +1,95 @@
using Microsoft.AspNetCore.Mvc;
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
{
public class AdminAddressesController : Controller
[ApiController]
[Route("api/v1/admin/[controller]")]
[Authorize(Roles = "Admin")]
public class AdminAddressesController : ControllerBase
{
public IActionResult Index()
private readonly IAdminAddressService _adminAddressService;
public AdminAddressesController(IAdminAddressService adminAddressService)
{
return View();
_adminAddressService = adminAddressService;
}
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<AddressDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> DeleteAddress(Guid id)
{
var result = await _adminAddressService.DeleteAddressAsync(id);
return result.Type switch
{
ServiceResultType.Success => NoContent(),
_ => NotFound(new { Message = result.ErrorMessage })
};
}
}
}
}