AdminShippingMethod

This commit is contained in:
Tizian.Breuch
2025-09-25 14:45:30 +02:00
parent f5543d35b4
commit 8f222ef4aa
3 changed files with 123 additions and 54 deletions

View File

@@ -1,9 +1,11 @@
// 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;
@@ -21,43 +23,90 @@ namespace Webshop.Api.Controllers.Admin
_shippingMethodService = shippingMethodService;
}
[HttpPost]
public async Task<ActionResult<ShippingMethodDto>> CreateShippingMethod([FromBody] ShippingMethodDto shippingMethodDto)
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<ShippingMethodDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllShippingMethods()
{
var createdMethod = await _shippingMethodService.CreateAsync(shippingMethodDto);
return CreatedAtAction(nameof(GetShippingMethodById), new { id = createdMethod.Id }, createdMethod);
var result = await _shippingMethodService.GetAllAsync();
return Ok(result.Value);
}
[HttpGet("{id}")]
public async Task<ActionResult<ShippingMethodDto>> GetShippingMethodById(Guid id)
[ProducesResponseType(typeof(ShippingMethodDto), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetShippingMethodById(Guid id)
{
var method = await _shippingMethodService.GetByIdAsync(id);
if (method == null) return NotFound();
return Ok(method);
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." })
};
}
[HttpGet]
public async Task<ActionResult<IEnumerable<ShippingMethodDto>>> GetAllShippingMethods()
[HttpPost]
[ProducesResponseType(typeof(ShippingMethodDto), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)]
public async Task<IActionResult> CreateShippingMethod([FromBody] ShippingMethodDto shippingMethodDto)
{
var methods = await _shippingMethodService.GetAllAsync();
return Ok(methods);
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();
var success = await _shippingMethodService.UpdateAsync(shippingMethodDto);
if (!success) return NotFound();
return NoContent();
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 success = await _shippingMethodService.DeleteAsync(id);
if (!success) return NotFound();
return NoContent();
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." })
};
}
}
}