Files
ShopSolution-backend/Webshop.Api/Controllers/Admin/AdminShippingMethodsController.cs
2025-09-25 14:45:30 +02:00

112 lines
4.9 KiB
C#

// src/Webshop.Api/Controllers/Admin/AdminShippingMethodsController.cs
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.Shipping;
using Webshop.Application.Services.Admin;
namespace Webshop.Api.Controllers.Admin
{
[ApiController]
[Route("api/v1/admin/[controller]")]
[Authorize(Roles = "Admin")]
public class AdminShippingMethodsController : ControllerBase
{
private readonly IAdminShippingMethodService _shippingMethodService;
public AdminShippingMethodsController(IAdminShippingMethodService shippingMethodService)
{
_shippingMethodService = shippingMethodService;
}
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<ShippingMethodDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllShippingMethods()
{
var result = await _shippingMethodService.GetAllAsync();
return Ok(result.Value);
}
[HttpGet("{id}")]
[ProducesResponseType(typeof(ShippingMethodDto), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetShippingMethodById(Guid id)
{
var result = await _shippingMethodService.GetByIdAsync(id);
return result.Type switch
{
ServiceResultType.Success => Ok(result.Value),
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
};
}
[HttpPost]
[ProducesResponseType(typeof(ShippingMethodDto), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)]
public async Task<IActionResult> CreateShippingMethod([FromBody] ShippingMethodDto shippingMethodDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var result = await _shippingMethodService.CreateAsync(shippingMethodDto);
return result.Type switch
{
ServiceResultType.Success => CreatedAtAction(nameof(GetShippingMethodById), new { id = result.Value!.Id }, result.Value),
ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }),
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
};
}
[HttpPut("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)]
public async Task<IActionResult> UpdateShippingMethod(Guid id, [FromBody] ShippingMethodDto shippingMethodDto)
{
if (id != shippingMethodDto.Id)
{
return BadRequest(new { Message = "ID in der URL und im Body stimmen nicht überein." });
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var result = await _shippingMethodService.UpdateAsync(shippingMethodDto);
return result.Type switch
{
ServiceResultType.Success => NoContent(),
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }),
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
};
}
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> DeleteShippingMethod(Guid id)
{
var result = await _shippingMethodService.DeleteAsync(id);
return result.Type switch
{
ServiceResultType.Success => NoContent(),
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
};
}
}
}